You are on page 1of 3

Lecture Notes: 5

Introduction A list is a sequential data structure, i.e. a collection of items accessible one after another beginning at the head and ending at the tail. It is a widely used data structure for applications which do not need random access. List is a data structures in that additions and removals can be made at any position. Operations The basic operations in a list are Insert- to insert a value in a given position, Delete to delete a value from the list, Update - to update a value in a location, Display to display the values in a list. Storing a list in a static data structure (Array List) The below given implementation stores the list in an array. The Array List has the following properties: The position of each element is given by an index from 0 to n-1, where n is the number of elements. Given any index, the element with that index can be accessed in constant time i.e. the time to access does not depend on the size of the list. To add an element at the end of the list, the time taken does not depend on the size of the list. However, the time taken to add an element at any other point in the list does depend on the size of the list, as all subsequent elements must be shifted up. Additions near the start of the list take longer than additions near the middle or end. When an element is removed, subsequent elements must be shifted down, so removals near the start of the list take longer than removals near the middle or end. Implementation #include<iostream.h> #include<conio.h> class List { private: int Array[100]; int Max; public: List() { Max = 0; } void Insert(int Pos, int Data) { if(Pos<=Max && Pos >= 0) { for(int i = Max-1 ; i >= Pos ; i--) { Array[i+1] = Array[i]; }

Notes Compiled for III Sem B.Tech CSE C & D batch

Page 1

Lecture Notes: 5

Array[Pos] = Data; Max++; } else { cout<<"\n\tError in position...\n\tPosition must be between 0 & "<<Max<<" ..."; getch(); } } void Delete(int Pos) { for(int i = Pos; i < Max-1; i++) { Array[i] = Array[i+1]; } Max--; } void Update(int Pos, int Data) { if(Max <= 0) { cout<<"\n\tList empty...can't Update empty list..."; getch(); } else if(Pos<=Max && Pos >= 0) { Array[Pos] = Data; } else { cout<<"\n\tError in position...\n\tPosition must be between 0 & "<<Max<<" ..."; getch(); } } void Display() { if(Max == 0 ) cout<<"\nList is empty now.."; else { cout<<"\nThe list is: \n"; for(int i = 0 ; i < Max; i++) cout<<Array[i]<<" "; } } };

Notes Compiled for III Sem B.Tech CSE C & D batch

Page 2

Lecture Notes: 5

void main() { clrscr(); List a; int Choice, Data, Position; do { clrscr(); cout<<"Menu\n~~~~\n1.Insert\n2.Delete\n3.Update\n4.Display\n5.Exit"; cout<<"\n\tYour choice: "; cin>>Choice; switch(Choice) { case 1: cout<<"\nEnter the data to insert: "; cin>>Data; cout<<"\nEnter the position to insert "<<Data<<": "; cin>>Position; a.Insert(Position,Data); break; case 2: cout<<"\nEnter the Position of value to be deleted: "; cin>>Position; a.Delete(Position); break; case 3: cout<<"\nEnter the data to Update: "; cin>>Data; cout<<"\nEnter the position to Update with "<<Data<<": "; cin>>Position; a.Update(Position,Data); break; case 4: a.Display(); getch(); break; case 5: cout<<"\n\tApplication closing.. \n\tPress any key to exit..."; break; default: cout<<"\n\tError in choice...\n\tEnter correct choice.."; getch(); break; } }while(Choice!=5); getch(); }

Notes Compiled for III Sem B.Tech CSE C & D batch

Page 3