Docsity

C++ Program for Singly Linked List Operations: Insertion, Deletion, and Display, Exercises of Data Structures and Algorithms

Toggle sidebar
bg1
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <process.h>
struct stu
{
int std_ID;
char name[20];
struct stu *next;
}
*first=0,*last=0;
void main()
{
clrscr();
int b,key;
while (1)
{
cout << "To insert values in end press 1:";
cout<<"\n To insert value at start press 2:";
cout<<"\n To insrt after key press 3:";
cout<<"\n To insert before key press 4:";
cout<<"\n To delete from start press 5:";
cout<<"\n To delete from end press 6:";
cout<<"\n To delete at specific key press 7";
cout<<"\nFor display press8";
cout<<"\nTo exit press9:" ;
cout<<"\n";
cin >> b;
switch (b)
{
//******insert in end*****
case 1:
{
struct stu *current;
current = new stu;
current -> next = 0;
if ( first== 0 )
{
first= current;
last = current;
}
else
{
last -> next = current;
last = current;
}
cout << "\nStudent ID: ";
cin >> current -> std_ID;
cout << "\nInput Name: ";
gets ( current -> name );
break;
}
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download C++ Program for Singly Linked List Operations: Insertion, Deletion, and Display and more Exercises Data Structures and Algorithms in PDF only on Docsity!

#include <iostream.h> #include <conio.h> #include <stdio.h> #include <process.h> struct stu { int std_ID; char name[20]; struct stu *next; } first=0,last=0; void main() { clrscr(); int b,key; while (1) { cout << "To insert values in end press 1:"; cout<<"\n To insert value at start press 2:"; cout<<"\n To insrt after key press 3:"; cout<<"\n To insert before key press 4:"; cout<<"\n To delete from start press 5:"; cout<<"\n To delete from end press 6:"; cout<<"\n To delete at specific key press 7"; cout<<"\nFor display press8"; cout<<"\nTo exit press9:" ; cout<<"\n"; cin >> b; switch (b) { //******insert in end***** case 1: { struct stu *current; current = new stu; current -> next = 0; if ( first== 0 ) { first= current; last = current; } else { last -> next = current; last = current; } cout << "\nStudent ID: "; cin >> current -> std_ID; cout << "\nInput Name: "; gets ( current -> name ); break; }

//********Insert value at start case 2: { struct stu *current; current=new stu; current->next=0; if(first==0) { first=last=current; } else { current->next=first; first=current; } cout << "\nStudent ID: "; cin >> current -> std_ID; cout << "\nInput Name: "; gets ( current -> name ); break; } //******Insrt after key case 3: { struct stu *p; p = first; struct stu *current; current= new stu; current -> next = 0; cout<<"enter key after which you want to insert" ; cin>>key; if(first==0) { cout<<"\no node persent\n"; } else { if(last->std_ID==key) { last->next=current; last=current; } else { while(p->std_ID!=key&&p->next->next!=0) { p=p->next; } if(p->std_ID!=key&&p->next->next==0) { cout<<"key not found\n\n"; break; }

break; } //******delete from start case 5: { struct stu *p; p=first; if(first==0) { cout<<"no node present\n"; } else { if(first->next==0) { delete(p); } else { p=p->next; first->next=0; delete(first); first=p; } } break; } //****delete from end case 6: { struct stu *p; p=first; if(first==0) { cout<<"no node present\n"; } //end if else { if(first->next==0) { delete(p); } // end if else { while(p->next->next!=0) { p=p->next; } // end while p->next=NULL; delete(last); last=p; } // end else

break; } //*****delete at specific key case 7: { struct stu *p; p=first; cout<<"enter the id, you want to delete\n"; cin>>key; if(first==0) { cout<<"there is no node present\n"; } if(first->std_ID==key) { p=p->next; first->next=0; delete(first); first=p; } else { while(p->next->std_ID!=key&&p->next->next!=0) { p=p->next; } if(p->next->next==0&&p->next->std_ID!=key) { cout<<"the specific id is absent \n"; } else { p->next=p->next->next; p->next->next=0; delete(p->next); } } //****display case 8: { struct stu *p; p = first; while ( p != 0 ) { cout << "\nStudent's ID: " << p -> std_ID << "\nStudent's Name: "; puts ( p -> name ); cout<<"\n"; p = p -> next; } break;