Cursor Implementation of List Adt

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

CURSOR IMPLEMENTATION OF LIST ADT #include<iostream.h> #include<conio.h> #include<stdlib.

h> #define MAX 20 int list[MAX]; void main() { int T,ch,len,pos; int create(); char ans; void display(int); int search(int); void delet(int); do { T: clrscr(); cout<<"\n\tCursor Implementation of List ADT"; cout<<"\n main menu";

cout<<"\n1.Create\n2.Display\n3.Search for a number\n4.Delete\n5.Quit"; cout<<"\n Enter ur choice:"; cin>>ch; switch(ch) { case 1: len=create(); break; case 2: display(len); break; case 3: pos=search(len); break; case 4: delet(len); break; case 5: cout<<"\nDo u want to exit(Y/N):"; ans=getch(); if(ans=='y'||ans=='Y')

exit(0); else goto T; default: clrscr(); cout<<"\nInvalid choice,try again"; getch(); }} while(ch!=5); } int create() { int n,i; clrscr(); cout<<"\nHow many elements u want in the list:"; cin>>n; if(n>MAX) { cout<<"\nError no. of elements exceed the limit"; getch(); }

if(n<MAX) { for(i=0;i<n;i++) { cout<<"\nEnter the element number:"<<i+1<<" "; cin>>list[i]; } cout<<"\n The list is created"; getch(); } return(n); } void display(int n) { int i; clrscr(); cout<<"\nThe list is:"; for(i=0;i<n;i++) { if(list[i]!=-1) cout<<list[i]<<"--";

} cout<<"\n\nPress any key to continue:"; getch(); } int search(int n) { int i,key; clrscr(); cout<<"\nEnter the number u want to search:"; cin>>key; for(i=0;i<n;i++) { if(list[i]==key) { cout<<"\nThe given no. is ay position:"<<i+1; getch(); return(i); }} cout<<"\n The given number is not present in the list\n"; getch(); return(i);

} void delet(int n) { int i; i=search(n); list[i]=-1; cout<<"\nThe element is now deleted"; getch(); }

OUTPUT: Cursor Implementation of List ADT Main menu 1.Create 2.Display 3.Search for a number 4.Delete 5.Quit Enter ur choice: 1 How many elements u want in the list:4 Enter the element number:1 1 Enter the element number:2 2 Enter the element number:3 3 Enter the element number:4 4 The list is created Cursor Implementation of List ADT Main menu 1.Create 2.Display

3.Search 4.Delete 5.Quit Enter ur choice:2 The list is:1- -2- -3- -4- Press any key to continue: Cursor Implementation of List ADT Main menu 1.Create 2.Display 3.Search 4.Delete 5.Quit Enter ur choice:3 Enter the number u want to search:3 The given no. is in position:3

Cursor Implementation of List ADT Main menu 1.Create 2.Display 3.Search 4.Delete 5.Quit Enter ur choice:4 Enter the number u want to search:3 The given no. is in position:3 The element is now deleted Cursor Implementation of List ADT Main menu 1.Create 2.Display 3.Search 4.Delete

5.Quit Enter ur choice:2 The list is:1- -2- -4- Press any key to continue: Cursor Implementation of List ADT Main menu 1.Create 2.Display 3.Search 4.Delete 5.Quit Enter ur choice:5 Do u want to exit(Y/N):Y

You might also like