0% found this document useful (0 votes)
22 views3 pages

Linked List

This document contains C++ code to implement a linked list data structure with operations to create, search, display, and delete nodes from the list. The code defines a struct called node with integer data (marks) and a pointer to the next node. Functions are included to create a linked list by adding nodes to the front, search for a node by its data, display all nodes by traversing the list, and delete a node by searching for its data and adjusting the pointers. A menu is provided to prompt the user to select an operation and the code uses a switch statement to call the corresponding function.

Uploaded by

Omaisa Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

Linked List

This document contains C++ code to implement a linked list data structure with operations to create, search, display, and delete nodes from the list. The code defines a struct called node with integer data (marks) and a pointer to the next node. Functions are included to create a linked list by adding nodes to the front, search for a node by its data, display all nodes by traversing the list, and delete a node by searching for its data and adjusting the pointers. A menu is provided to prompt the user to select an operation and the code uses a switch statement to call the corresponding function.

Uploaded by

Omaisa Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include"conio.

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();

cout<<"1. Create List";


cout<<"\n2. Search an Item";
cout<<"\n3. Display List";
cout<<"\n4. Delete an Item";
cout<<"\n5. Exit";
cout<<"\nYour Choice :: ";
ch=getch();

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;

temp = pre = start;

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

You might also like