Linked List
Linked List
h"
#include"iostream.h"
#include"malloc.h"
struct node
{
int marks;
node *next;
}*start,*cur,*temp,*pre;
void main()
{
int m,found=0;
start=cur=temp = NULL;
clrscr();
char ch='y';
while(ch!='5')
{
clrscr();
switch(ch)
{
case '1':
clrscr();
char c = 'y';
while(c!='n')
{
temp = (node *)(malloc(sizeof(node)));
cout<<"Enter marks :: ";
cin>>temp->marks;
temp->next = NULL;
if(start==NULL)
{
start =cur= temp;
}
else
{
cur->next = temp;
cur=temp;
}
cout<<"\nCreate more node ? [y/n] ";
c = getch();
} // end of linked list creation loop
break;
case '2':
clrscr();
found=0;
cout<<"Enter marks to search : ";
cin>>m;
temp = start;
while(temp!=NULL)
{
if(m == temp->marks)
{
cout<<"Node found";
found =1;
break;
}
temp = temp->next;
} // end of search while
if(found==0)
cout<<"Node not found";
getch();
break;
////////////////////////////
// DISPLAY LINKED LIST CASE
////////////////////////////
case '3':
temp = start;
while(temp!=NULL)
{
cout<<"\n\nMarks :: "<<temp->marks;
temp = temp->next;
}// end of display while
getch();
break;
case '4':
clrscr();
found=0;
cout<<"Enter marks to delete node : ";
cin>>m;
while(temp!=NULL)
{
if(m == temp->marks)
{
found=1;
if(temp==start)
{
start=start->next;
delete(temp);
}
else
{
pre->next = temp->next;
delete(temp);
}
cout<<"\nNode has been deleted !";
getch();
break;
}
pre=temp;
temp = temp->next;
} // end of search while
if(found==0)
cout<<"Node not found";
break;
case '5':
break;
default:
{}
} // end of switch
} // end of while
getch();
} // end of main