linked list (searching)


#include<iostream>
#include<conio.h>

using namespace std;

typedef struct node
{
int data; // will store information
node *next; // the reference to the next node
};


int main()
{
node *head = NULL; //empty linked list
int info = 0, node_number = 0, counter = 0, value=0;
char choice;

do{
cout<<"***********************************************" << endl;
cout<<"* 1.Insert at first *" << endl;
cout<<"* 2.Insert at last *" << endl;
cout<<"* 3.Insert after specified number of node *" << endl;
cout<<"* 4.Search value *" << endl;
cout<<"* 5.Display data *" << endl;
cout<<"* 0.Exit *" << endl;
cout<<"***********************************************";

cout<<"\nEnter your choice: ";
cin>>choice;

switch(choice)
{
case '0':{
return 0;
break;
}

case '1':{ cout<<"ENTER ANY NUMBER:";
cin>>info;
cout<<"Input data: "<<info<<" Node No: "<<++counter<<endl;
node *temp;
temp = (node*)malloc(sizeof(node));
temp->data = info;
temp->next=head;
head = temp;

cout<<"Lists of your number : " <<endl;
for(temp = head ; temp!=NULL ; temp = temp->next)
{
cout<<temp->data<<" ";
}

cout<<endl;
cout<<endl;
cin.get();
break;
}

case '2':{
if(head==NULL)
{
cout<<"ENTER ANY NUMBER:";
cin>>info;
cout<<"Input data: "<<info;

node *temp;
temp = (node*)malloc(sizeof(node));
temp->data = info;
temp->next = NULL;
head = temp;
counter++;
}

else
{
cout<<"ENTER ANY NUMBER:";
cin>>info;
cout<<"Input data: "<<info;
node *temp1;
temp1=(node*)malloc(sizeof(node));
temp1 = head;
while(temp1->next!=NULL)
temp1 = temp1->next;

node *temp;
temp = (node*)malloc(sizeof(node));
temp->data = info;
temp->next = NULL;
temp1->next = temp;
break;
}
}


case '3':{

if(head==NULL)
{
cout<<"The Linked List is empty"<<endl;
break;
}

cout<<"ENTER ANY NUMBER:";
cin>>info;
cout<<"Input data: "<<info<<endl;
cout<<"ENTER THE NODE NUMBER:";
cin>>node_number;
node *temp1;
temp1 = (node*)malloc(sizeof(node));
temp1 = head;

for( int i = 1 ; i < node_number ; i++ )
{
temp1 = temp1->next;

if( temp1 == NULL )
{
cout<<node_number<<" node is not exist"<< endl;
break;
}
}

node *temp;
temp = (node*)malloc(sizeof(node));
temp->data = info;
temp->next = temp1->next;
temp1->next = temp;
counter++;
break;
}
case '4':{
node *temp1;
temp1 = (node*)malloc(sizeof(node));
temp1 = head;
bool find = false;

cout<<"Enter the value";
cin>>value;

while(temp1!=NULL)
{
if (temp1->data==value)
find = true;
temp1=temp1->data;
}
return find;
break;
}


case'6':{
node *temp1;
temp1 = (node*)malloc(sizeof(node));
node *temp2;
temp2 = (node*)malloc(sizeof(node));
int temp = 0;
for( temp1 = head ; temp1!=NULL ; temp1 = temp1->next )
{
for( temp2 = temp1->next ; temp2!=NULL ; temp2 = temp2->next )
{
if( temp1->data > temp2->data )
{
temp = temp1->data;
temp1->data = temp2->data;
temp2->data = temp;
}
}
}

cout<<"Lists of your number "<<endl;
for(temp1 = head ; temp1!=NULL ; temp1 = temp1->next)
{
cout<<temp1->data<<" ";
}

cout<<endl;
cin.get();
break;
}

}

}while(choice!='0');


return 0;
}


when i compile this code. there is error at searching. it says something like invalid int to node. can anyone suggest a better solution that the code i made because if i remove the searching part, the code is okay.
I didn't use class because i cant fully understand it.
In "Search" block,

There is "temp1=temp1->data;". "temp1->data" is "int" type, "temp1" is "node*" type. What's the sentence for?

I guess you want" temp1=temp1->next;"

By the way, there is no cleaning up for the memery allocated to the list when exiting.
Last edited on
Topic archived. No new replies allowed.