Linked Lists

Hello Everyone!

I am currently working on a problem that requires me to search a user entered linked list for a value that the user asks for. So far I have written most of the code but I am having trouble figuring out how to write the code for the function that searches the linked list for the value and prints out its position in the list. If anyone could help me with this that would be great! Thanks! Here is what I have so far...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// This program allows the user to enter data into a linked list
// then calls a function that searches the linked list for a number
// that the user wants to find. If it is in linked the list the function 
// then returns a number indicating its position in the list. If it isn't found
// then the function just returns a 0.

#include <iostream>
using namespace std;

struct ListNode {
	float value;
	ListNode *next;
};
ListNode *head = NULL;

int main()
{
	int num, value;
	char answer;
	do {
		cout << "Please enter a value to put in the list --> ";
		cin >> num;
		insertNode(num);
		cout << endl;
		cout << "Would you like to put another value into your list? ";
		cin >> answer;
	} while(toupper(answer)=='Y');

	do {
		cout << "Please enter the value you would like to search for in the list--> ";
		cin >> value;
		searchNodes(num);
		cout << endl;
		cout << "Would you like to search for another value? ";
		cin >> answer;
	} while(toupper(answer)=='Y');

}

int insertNode(float num)
{
	struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL;

	newNode = new ListNode;
	if(newNode == NULL) {
		cout << "Error allocating memory for new list member!\n";
		return 1;
	}
	newNode->value = num;
	newNode->next = NULL;
	if(head==NULL) {
		cout << "List was empty - " << newNode->value;
		cout << " is part of list's first node.\n";
		head = newNode;
	}
	else {
		while((nodePtr != NULL) && (nodePtr->value < num)) {
			prevNodePtr = nodePtr;
			nodePtr = nodePtr->next;
		}
		if(prevNodePtr==NULL) {
			newNode->next = head;
			head = newNode;
		}
		else {
			newNode->next = nodePtr; 
			prevNodePtr->next = newNode;
		}
	}
	return 0;
}

int searchNodes(int num)
{
	// No idea...
}
Last edited on
Maybe you could use the find(start,stop,t) function in the C++ Standard Library File <algo>
Sorry for the old style method,but this one will work just arrange the codes the way you want it!


#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
struct node *head,*curr,*tail;

struct node
{
int data;
struct node *next;
};

Search_Node(int search)
{
int ctr=0,f=0;
curr=head;

while(curr->next!=NULL)
{
ctr++;
if(curr->data==search)
{
cout<<"Value Found !,and is located at position "<<ctr<<" ...";
f=1;
}
curr=curr->next;
}
if(f==0)
{
cout<<"\nNumber not Found!";
}
return 0;
}

Add_Node(int value)
{
curr->data=value;
tail=(struct node*)malloc(sizeof(struct node));
tail->next=NULL;
curr->next=tail;
curr=tail;
return 0;
}

main()
{
int ctr=0,search,value;
char ans;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
curr=head;

do
{
cout<<"\nEnter node value "<<(ctr+1)<<" : ";
cin>>value;
ctr++;
Add_Node(value);
cout<<"Enter another value? [y/n]";
ans=getch();
}while(ans=='y'||ans=='Y');

clrscr();
do
{
cout<<"\nEnter Number to search : ";
cin>>search;
Search_Node(search);
cout<<"\nSearch Again? [y/n]";
ans=getch();
clrscr();
}while(ans=='y'||ans=='Y');
}
thanks qtpan...i was thinking about it today and your search function is exactly what i was thinking about trying and doing thanks for the help!
closed account (S6k9GNh0)
1) The STL has an already implemented doubly-linked list with full C++ capabilities. This, by chance, is called the std::list.
2) All C++ headers do not use an extension. The header names you used qtpan, are wrong.
3) If you are set to implementing your own list, I suggest you look up templates since they are very effective here.
4) I really don't suggest you use conio.h. It's platform specific. At that point, I would switch to pdcurses or something similar that has multiplatform capabilities.
5) I would suggest you read this article based on how to properly take in different variables using streams: http://cplusplus.com/forum/articles/6046/

EDIT 6) It's not healthy to use using namespace std;. Instead, either use the std:: label or specify specific parts of the std namespace that you use, not all of it.
Last edited on
Topic archived. No new replies allowed.