You are on page 1of 19

Experiment No.

Date of assignment:-

Date of submission:-

Aim of the experiment:Write a program in C++ to show the operations of a Linked List Algorithm:START is a pointer that holds the address of the first node. INFO is the data field of a node of the stack. LINK is the link field of a node of the stack that points to the neighboring node. ITEM is the element to insert into the linked list during inserting algorithms and the element deleted during deleting algorithms. AVAIL is a pointer to the first node in the free storage list(free pool). TEMP and SAVE are temporary pointers used during the traversing. LOC and NUM are counter variables used. INSERT_F (INFO, LINK, TOP, AVAIL, ITEM) This procedure inserts an element ITEM at the front of a linked list Step 1:- [Available space?] If AVAIL = NULL, then Print OVERFLOW and return Step 2:- [Remove first node from avail list] Set NEW AVAIL and AVAIL LINK[AVAIL] Step 3:- [Copies ITEM into new node] Set INFO[NEW] ITEM Step 4:- [New node points to the original top node in the stack] Set LINK[NEW] START Step 5:- [Reset TOP to point to the new node at the top of the stack] Set START NEW Step 6:- Return

Page No.

Program:#include<iostream.h> #include<conio.h> #include<process.h> struct node { int data; node *link; }; class S_LL { node *START; public: S_LL(){START=NULL;} void insert_f(); void insert_e(); void insert_a(); void remove_f(); void remove_e(); void remove_a(); void display(); void insert(); void remove(); ~S_LL(); }; void S_LL :: insert_f() { node *temp; temp=new node; int x; cout<<"\n\tEnter the data to insert: "; cin>>x; temp->data=x; if (START==NULL) { START=temp; temp->link=NULL; } Page No.

else { temp->link=START; START=temp; } } void S_LL :: insert_e() { node *temp; temp=new node; int x; cout<<"\n\tEnter the data to insert: "; cin>>x; temp->data=x; if (START==NULL) { START=temp; temp->link=NULL; } else { node *save; save=START; while (save->link!=NULL) { save=save->link; } save->link=temp; temp->link=NULL; } } void S_LL :: insert_a() { node *temp; temp=new node; int x,loc; cout<<"\n\tEnter the data to insert: "; cin>>x; temp->data=x; cout<<"\tEnter the location to insert: "; cin>>loc; Page No.

node *save; save=START; int num=1; if (loc==1 && START==NULL) { START=temp; temp->link=NULL; return; } if (START==NULL) { cout<<"\n\tInsertion failed: Given location not available"; return; } while (num<loc && save->link!=NULL) { save=save->link; num++; } if (loc!=num) cout<<"\n\tInsertion failed: Given location not available"; else if (num==loc && save->link==NULL) { save->link=temp; temp->link=NULL; } else { temp->link=save->link; save->link=temp; } } void S_LL :: remove_f() { node *temp; if (START->link==NULL) { temp=START; START=NULL; } else { Page No.

temp=START; START=START->link; } cout<<"\n\t"<<temp->data<<" is deleted."; delete temp; } void S_LL :: remove_e() { node *temp; if (START->link==NULL) { temp=START; START=NULL; } else { node *save,*pre; save=START; while (save->link!=NULL) { pre=save; save=save->link; } pre->link=NULL; temp=save; } cout<<"\n\t"<<temp->data<<" is deleted."; delete temp; } void S_LL :: remove_a() { int loc; cout<<"\n\tEnter the location to delete: "; cin>>loc; node *temp,*save,*pre; int num=1; save=START; if (loc==1 && START->link==NULL) { temp=START; START=NULL;

//List has only one data

Page No.

} else if (loc==1) //1st data to be deleted { temp=START; START=START->link; } else { while (num<loc && save->link!=NULL) { pre=save; save=save->link; num++; } if (num!=loc) { cout<<"\n\tDeletion failed: Given location not available"; return; } else if (num==loc && save->link==NULL) { pre->link=NULL; temp=save; } else { pre->link=save->link; temp=save; } } cout<<"\t"<<temp->data<<" is deleted."; delete temp; } void S_LL :: display () { if (START==NULL) { cout<<"\nNo data to display"; return; } node *save; save=START; Page No.

cout<<"\n\n"; while (save!=NULL) { cout<<save->data<<" "; save=save->link; } } S_LL :: ~S_LL() { node *temp; while(START!=NULL) { temp=START->link; delete START; START=temp; } } void S_LL :: insert() { node *temp; temp=new node; if (temp==NULL) { cout<<"\nInsertion failed: OVERFLOW"; return; } delete temp; char b; cout<<"\n\n\tWhere do you want to insert"; cout<<"\n\t1. Front"; cout<<"\n\t2. End";

cout<<"\n\t3. Any position"; cout<<"\n\tEnter appropriate numbers: "; b=getche(); switch (b) { case '1': insert_f(); break; Page No.

case '2': insert_e(); break; case '3': insert_a(); break; default: cout<<"\n\tYou have entered an invalid number."; break; } } void S_LL :: remove() { if (START==NULL) { cout<<"\nDeletion failed: UNDERFLOW"; return; } char b; cout<<"\n\n\tWhere do you want to delete"; cout<<"\n\t1. Front"; cout<<"\n\t2. End"; cout<<"\n\t3. Any position"; cout<<"\n\tEnter appropriate number: "; b=getche(); switch (b) { case '1': remove_f(); break; case '2': remove_e(); break; case '3': remove_a(); break; default: cout<<"\n\tYou have entered an invalid number."; break; } } Page No.

main () { clrscr(); char a; S_LL list; while(1) { cout<<"\n\nWhat do you want to do"; cout<<"\n1. Insert"; cout<<"\n2. Delete"; cout<<"\n3. Display"; cout<<"\n4. Exit"; cout<<"\nEnter appropriate numbers: "; switch (a=getche()) { case '1': list.insert(); break; case '2': list.remove(); break; case '3': list.display(); break; case '4': exit(0); default: cout<<"\nYou have entered an invalid number. Try again"; break; } } return 0; }

Page No.

Output:Where do you want to delete 1. Front 2. End 3. Any position Enter appropriate number: 3 Enter the location to delete: 1 10 is deleted. What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 3 No data to display What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 1 Where do you want to insert 1. Front 2. End 3. Any position Enter appropriate numbers: 1 Enter the data to insert: 10

What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 3 10 What do you want to do Page No.

1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 2 Where do you want to delete 1. Front 2. End 3. Any position Enter appropriate number: 2 10 is deleted. What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 3 No data to display What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 1 Where do you want to insert 1. Front 2. End 3. Any position Enter appropriate numbers: 2 Enter the data to insert: 10

What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 3 Page No.

10 What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 1 Where do you want to insert 1. Front 2. End 3. Any position Enter appropriate numbers: 1 Enter the data to insert: 20

What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 3 20 10 What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 1 Where do you want to insert 1. Front 2. End 3. Any position Enter appropriate numbers: 3 Enter the data to insert: 30 Enter the location to insert: 2

What do you want to do Page No.

1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 3 20 10 30 What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 2 Where do you want to delete 1. Front 2. End 3. Any position Enter appropriate number: 2 30 is deleted. What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 3 20 10 What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 1 Where do you want to insert 1. Front 2. End 3. Any position Enter appropriate numbers: 3 Page No.

Enter the data to insert: 40 Enter the location to insert: 1

What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 3 20 40 10 What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 2 Where do you want to delete 1. Front 2. End 3. Any position Enter appropriate number: 3 Enter the location to delete: 2 40 is deleted. What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 3 20 10 What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 2 Page No.

Where do you want to delete 1. Front 2. End 3. Any position Enter appropriate number: 1 20 is deleted. What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 3 10 What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 1 Where do you want to insert 1. Front 2. End 3. Any position Enter appropriate numbers: 2 Enter the data to insert: 50

What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 3 10 50 What do you want to do 1. Insert Page No.

2. Delete 3. Display 4. Exit Enter appropriate numbers: 1 Where do you want to insert 1. Front 2. End 3. Any position Enter appropriate numbers: 3 Enter the data to insert: 5 Enter the location to insert: 5 Insertion failed: Given location not available What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 1 Where do you want to insert 1. Front 2. End 3. Any position Enter appropriate numbers: 2 Enter the data to insert: 60

What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 3 10 50 60 What do you want to do 1. Insert 2. Delete 3. Display Page No.

4. Exit Enter appropriate numbers: 2 Where do you want to delete 1. Front 2. End 3. Any position Enter appropriate number: 1 10 is deleted. What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 3 50 60 What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 1 Where do you want to insert 1. Front 2. End 3. Any position Enter appropriate numbers: 6 You have entered an invalid number. What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 2 Where do you want to delete 1. Front 2. End Page No.

3. Any position Enter appropriate number: 6 You have entered an invalid number. What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 6 You have entered an invalid number. Try again What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 1 Where do you want to insert 1. Front 2. End 3. Any position Enter appropriate numbers: 3 Enter the data to insert: 70 Enter the location to insert: 3 Insertion failed: Given location not available What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 2 Where do you want to delete 1. Front 2. End 3. Any position Enter appropriate number: 3 Enter the location to delete: 3 Page No.

Deletion failed: Given location not available What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 3 50 60 What do you want to do 1. Insert 2. Delete 3. Display 4. Exit Enter appropriate numbers: 4

Page No.

You might also like