Você está na página 1de 54

K.S.R COLLEGE OF ENGINEERING,TIRUCHENGODE-637215.

DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING.

EC 2209 - DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB MANUAL

YEAR: II

SEM:III

Prepared By : K.CHINNUSAMY A.VELLINGIRI B.VIJAY KUMAR K.KRISHNA KUMAR

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

LIST OF EXPERIMENTS CYCLE I EXP.NO 1 2 3 4 5 6 NAME OF THE EXPERIMENT Basic Programs for C++ Concepts Array implementation of List Abstract Data Type (ADT) Linked list implementation of List ADT Cursor implementation of List ADT Stack ADT - Array and linked list implementations Bubble sort CYCLE II EXP.NO 1 2 3 4 5 NAME OF THE EXPERIMENT Queue ADT Array and linked list implementations Search Tree ADT - Binary Search Tree Heap Sort Quick Sort Insertion sort The next two exercises are to be done by implementing the following source files a) Program source files for Stack Application 1 b) Array implementation of Stack ADT c) Linked list implementation of Stack ADT d) Program source files for Stack Application 2 An appropriate header file for the Stack ADT should be #included in (a) and (d) Implement any Stack Application using array implementation of Stack ADT (by implementing files (a) and (b) given above) and then using linked list implementation of Stack ADT (by using files (a) and implementing file (c))

WORK PLAN

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

EXPT. NO 1 2 3 4 5 6

NAME OF THE EXPERIMENT CYCLE I Basic Programs for C++ Concepts Array implementation of List Abstract Data Type (ADT) Linked list implementation of List ADT Cursor implementation of List ADT Stack ADT - Array and linked implementations Bubble sort list

STAFF INCHARGE

EXPT. NO 1 2 3 4 5

NAME OF THE EXPERIMENT CYCLE II Queue ADT Array and linked list implementations Search Tree ADT - Binary Search Tree Heap Sort Quick Sort Insertion sort The next two exercises are to be done by implementing the following source files a)Program source files for Stack Application 1 b) Array implementation of Stack ADT c)Linked list implementation of Stack ADT d) Program source files for Stack Application 2 An appropriate header file for the Stack ADT should be #included in (a) and (d) Implement any Stack Application using array implementation of Stack ADT (by implementing files (a) and (b) given above) and then using linked list implementation of Stack ADT (by using files (a) and implementing file (c))

STAFF INCHARGE

BATCH 1 (CYCLE I)

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

INTRODUCTION CLASS ON: DAY: DATE Proposed Actual PERIOD/TIME: SUB BATCH NUMBER AND EXPERIMENT NO 1 TO 36 1 2 3 4 5 6

INTRODUCTION CLASS ON: DAY: DATE Proposed PERIOD/TIME: SUB BATCH NUMBER AND EXPERIMENT NO Actual 1 TO 37 1 2 3 4 5 6

BATCH 1 (CYCLE II) DAY: PERIOD/TIME:

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

DATE Proposed Actual

SUB BATCH NUMBER AND EXPERIMENT NO

1 TO 36 1 2 3 4 5 6

BATCH 2 (CYCLE II) DAY: PERIOD/TIME:

DATE Proposed Actual

SUB BATCH NUMBER AND EXPERIMENT NO

1 TO 37 1 2 3 4 5 6 BASIC PROGRAMS FOR C++ CONCEPTS

Ex.No.: Aim To write a C++ programs for basic operations.

Date:

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

1.1.SWAP TWO INTEGER VALUES BY REFERENCE #include iostream.h> void swap(int *x,int *y) { Int t; t=*x; *x=*y; *y=t; } Void main() { Int a,b; Cout<<Enter the values; Cin>>a>>b; Cout<<the values before swap is:<<a<<b; Swap(&a,&b); Cout<<the values after swap is:<<a<<b; } Output: Enter the values : Values before swap Values after swap 10 10 20 20 20 10

1.2. CLASS AND OBJECT IMPLEMENTATION #include<iostream.h> #include<string.h> Class student { Int regno; Char name[20]; Public: Void setdata(int r, char*n); Void put data() { Cout<<reg.no<<regno; Cout<<Name<<name; } }; Void student::setdata(int r, char *n) { regno=r; strcpy(name,n); } Int main()

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

{ Stuident s1,s2; S1.setdata(100,ajay); S2.setdata(101,vijay); S1.putdata(); S2.putdata(); } Output: Reg no 100 name ajay reg no 101 name vijay 1.3. INLINE FUNCTIONS #include<iostream.h> Inline int square (int num) { Return num*num; } int main() { Float n; cout<<enter the values: cin>>n; cout<<square is :<<square(n); } Output: Enter the values 4 Square is: 16

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

1.4. INHERITENCE (MULTIPLE INHERITANCE) #include<iostream.h> Class student{ Protected: int rollno; Public: void getnumber(int a) { rollno=a; } Void putnumber() { Cout<<rollno; } }; Class test: public student { Protected: float sub1,sub2; Public: void getmarks (float x, float y) { Sub1=x; Sub2=y; } Void putmarks() { Cout<<sub1<<sub2; } }; Class result: public test { Float total; Public: void display () { Total=Sub1+Sub2; Putnumber(); Putmarks(); Cout<<total; } }; Int main() { result stud1; Sud1.getnumber(100); Sud1.getmarks(85,75); Sud1.diaplay(); } Output: 100 85 75 160

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

1.5. POLYMORPHIC FUNCTION #include<iostream.h> Class base { Private : int x; Public: base() { x=5;} Virtual void print() { cout<<x=<<x;} }; Class derived : public base { Private : int y; Public: derived() { y=10;} Void print() { cout<<y=<<y;} }; Int main() { Base *bptr; Base obj1; Derived obj2; Obj1.print(); Obj2.print(); Bptr=&obj1; Bptr->print(); Bptr=&obj2; Bptr->print(); } Output: X=5 y=10 x=5 y=10

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

ARRAY IMPLEMENTATION OF LIST ABSTRACT DATA TYPE (ADT) Ex.No.: Aim: To write a C++ program to do the list operations. Algorithm: 1. Initialize the list with n items. 2. Get the choice of operation either insertion or deletion. 3. For insertion, get the item to be inserted and its position. Then position to last position move the items in the list one step forward, then insert the new item in its corresponding position. 4. For deletion get the item to be deleted and find its position then move the items in the list one step backward from last position to position found. Program: #include<iostream.h> #include<conio.h> int a[20],n; int main() { int i, ch,p,v; cout<<" Enter the No of Element in the list : "; cin>>n; cout<<" Enter the List of Element one by one : "; for (i=0;i<n;i++) cin>>a[i]; while(1) { cout<<"\n 1. Insert.... 2,Delete... 3. Exit... :"; cin>>ch; switch(ch) { case 1: cout<<"\n Enter the Position & Element : " ; cin>>p>>v; for(i=n;i>=p;i--) a[i]=a[i-1]; a[p]=v; n++; break; case 2: cout<<"\n Enter the Deleting Element : " ; cin>>v; p=0; for(i=0;i<n;i++) { Date:

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

if (a[i]==v) p=1; a[i]=a[i+p]; } n--; break; case 3: return(1); } cout<<"\n the List of Element : " ; for(i=0;i<n;i++) cout<<a[i]<<"\t"; } getch(); } Output: Enter the no of elements in the list : 5 Enter the List of Element one by one : 10 20 30 40 50 1.Insert.... 2,Delete... 3. Exit... : 1 Enter the Position & Element: 2 15 List of the Elements : 10 15 20 30 40 50 1.Insert.... 2,Delete... 3. Exit... : 2 Enter the Deleting Element : 20 List of the Elements : 10 15 30 40 50 1.Insert.... 2,Delete... 3. Exit... : 2 Exit

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

LINKED LIST IMPLEMENTATION OF LIST ADT Ex.No.: Linked list implementation of List ADT- Singly Linked List Linked list implementation of List ADT- Doubly Linked List Linked list implementation of List ADT- Circular Linked List Date:

I. LINKED LIST IMPLEMENTATION OF LIST ADT- SINGLY LINKED LIST Aim: To write a C++ program to implement the single linked list Algorithm: 1. 2. 3. 4. 5. 6. 7. 8. Start the program Create a menu in main program Get the choice If the choice is one create a new node If choice is two then insert into the node If the choice is three Delete from the node else come out of the menu program Stop

Application: This program teaches with how to create space for a node and also how inset and delete in dynamic memory allocation schemes Program: #include<iostream.h> #include<conio.h> class Node { public : int data; Node *next; void getdata() { cout<<"Enter the Data"; cin>>data; } void putdata() { cout<<data<<"\t"; } };

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

int main() { Node *a,*tmp,*tmp1; int ch,i,j; a = NULL; while(1) { cout<<"\n1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit.. \nSelect... : "; cin>>ch; switch(ch) { case 1: tmp= (Node*)new(Node); tmp->getdata(); tmp->next=a; a= tmp; break; case 2: a->putdata(); a=a->next; break; case 3: cout<<"Give the Insertion Point Value and L/R (0/1) :"; cin>>i>>j; tmp=a; while((j==1)?(tmp->data!=i):(tmp->next->data!=i)) { tmp=tmp->next; } tmp1= (Node*)new(Node); tmp1->getdata(); tmp1->next=tmp->next; tmp->next=tmp1; break; case 4: cout<<"Give the Deletion Point Value :"; cin>>i; tmp=a; while(tmp->next->data!=i) { tmp=tmp->next; } if(tmp!=NULL) {tmp->next->putdata(); tmp->next=tmp->next->next; } break; case 5: cout<<"Give the Modify Point Value & New Value :"; cin>>i>>j; tmp=a; while(tmp->data!=i)

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

tmp=tmp->next; } if(tmp!=NULL) { tmp->data=j; tmp->putdata(); } break;

case 6: cout<<"The Elements are....:"; tmp=a; while(tmp!=NULL) { tmp->putdata(); tmp=tmp->next; } break; case 7: return(1); } getch(); }

} Output : 1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit.. Select... : 1 Enter the Data 10 1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit.. Select... : 1 Enter the Data 20 1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit.. Select... : 1 Enter the Data 30 1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit.. Select... : 1 Enter the Data 40 1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit.. Select... : 6 The Elements are .. : 40 30 20 10

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit.. Select... : 2 40 1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit.. Select... : 6 The Elements are .. : 30 20 10 1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit.. Select... : 3 Give the Insertion Point Value and F/B (0/1) : 20 0 Enter the Data 25 1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit.. Select... :6 The Elements are .. : 30 25 20 10 1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit.. Select... : 3 Give the Insertion Point Value and F/B (0/1) : 20 1 Enter the Data 15 1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit.. Select... :6 The Elements are .. : 30 25 20 15 10 1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit.. Select... : 5 Give the Modify Point Value & New Value : 25 27 27 1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit.. Select... : 6 The Elements are .. : 30 27 20 15 10 1. In.. 2. Out... 3.Insert.. 4.Delete.. 5. Modify.. 6. Display.. 7 Exit.. Select... : 7 Exit

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

LINKED LIST IMPLEMENTATION OF LIST ADT- DOUBLY LINKED LIST Ex.No.: Date:

Aim: To write a C++ program for doubly linked list Algorithm: 1. Start the program 2. Create a menu in main program 3. Get the choice 4. If the choice is one create a new node which has double link 5. If choice is two then insert into the node ,change the appropriate link 6. If the choice is three Delete from the node, remove the link and join it 7. else come out of the menu program 8. Stop Application: This program helps the student how to create a double linked list. This dynamic memory allocation sees how the link is established between the two nodes and how to navigate through the double linked list. Program: #include<iostream.h> #include<conio.h> class Node { public : int data; Node *next, *pre; void getdata() { cout<<"Enter the Data"; cin>>data; } void putdata() { cout<<data<<"\t"; } }; int main() { Node *h,*tmp,*tmp1; int ch,i,j;

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

h = NULL; while(1) { cout<<"\n1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. \nSelect... : "; cin>>ch; switch(ch) { case 1: tmp= (Node*)new(Node); tmp->getdata(); tmp->pre=h; h->next=tmp; tmp->next=NULL; h=tmp; break; case 2: cout<<"Give the Insertion Point Value and L/R (0/1) :"; cin>>i>>j; tmp=h; while(tmp->data!=i) { tmp=tmp->pre; } tmp1= (Node*)new(Node); tmp1->getdata(); if(j==0)tmp=tmp->next; tmp1->pre=tmp->pre; tmp->pre->next=tmp1; tmp1->next=tmp; tmp->pre=tmp1; break; case 3: cout<<"Give the Deletion Point Value :"; cin>>i; tmp=h; while(tmp->data!=i) { tmp=tmp->pre; } if(tmp!=NULL) {tmp->putdata(); tmp->pre->next=tmp->next; tmp->next->pre=tmp->pre; } break; case 4: cout<<"Give the Modify Point Value & New Value :"; cin>>i>>j; tmp=h; while(tmp->data!=i && tmp!=NULL) { tmp=tmp->pre;

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

} if(tmp!=NULL) { tmp->data=j; tmp->putdata(); } break; case 5: cout<<"The Elements are....:"; tmp=h; while(tmp!=NULL) { tmp->putdata(); tmp=tmp->pre; } break; case 6: return(1); } getch(); } } Output: 1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 1 Enter the Data 10 1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 1 Enter the Data 20 1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 1 Enter the Data 30 1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 1 Enter the Data 40 1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 5 The Elements are .. : 40 30 20 10 1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 3 40

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 5 The Elements are .. : 30 20 10 1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 2 Give the Insertion Point Value and F/B (0/1) : 20 0 Enter the Data 25 1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... :5 The Elements are .. : 30 25 20 10 1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 2 Give the Insertion Point Value and F/B (0/1) : 20 1 Enter the Data 15 11. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... :5 The Elements are .. : 30 25 20 15 10 1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 4 Give the Modify Point Value & New Value : 25 27 27 1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 4 The Elements are .. : 30 27 20 15 10 1. Add.. 2.Insert.. 3.Delete.. 4. Modify.. 5. Display.. 6 Exit.. Select... : 6 Exit

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

III. LINKED LIST IMPLEMENTATION OF LIST ADT- CIRCULAR LINKED LIST Aim: To write a C++ program to create and manipulate the circular linked list Algorithm: 1. Start the program 2. Create a menu in main program 3. Get the choice 4. If the choice is one create a new node for circular linked list 5. If choice is two then insert into the node, and change the appropriate link 6. If the choice is three Delete from the node, change the link before deletion 7. else come out of the menu program 8. Stop Application: This program helps the student how to create a circular linked list and how to navigate, display, insert and delete them. Program: #include<iostream.h> #include<conio.h> template <class T> class Linklist { int n,i,j,ins_ctr; T *a,temp,y; class node { friend class Linklist<T>; T data; node *link; } *head,*p,*q,*tail; public: Linklist() { head=NULL; tail=NULL; Menu(); } void insert(T x) { node *n_node; p=NULL; n_node=new node(); n_node->data=x; n_node->link=NULL;

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

if(!head) { head=n_node; tail=n_node; n_node->link=head; } else if(head->data >x) { n_node->link=head; head=n_node; } else { for(p=head;(p!=tail) && (p->link->data <x);p=p->link); if(p!=tail) { n_node->link=p->link; p->link=n_node; } else { p->link=n_node; n_node->link=head; tail=n_node; } } } void display() { for(p=head;p!=tail;p=p->link) cout<<p->data<<"\t"; cout<<p->data<<"\n"; } void deletion(T x) { if(!head) cout<<"List is empty\n"; if(head->data==x) { q=head; head=head->link; delete q; } else { for(p=head;(p->link!=tail) && (p->link->data !=x);p=p->link); if(p->link!=tail) { q=p->link;

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

p->link=p->link->link; delete q; } else if(p->link->data==x) { q=p->link; p->link=p->link->link; tail=p; } else cout<<"Element Not in the List\n"; } } void Menu() { int ch,flag=1,x,ctr; cout<<"Circular linked list\n"; cout<<"1.Add\n2.Delete\n3.Display\n4.Exit\n\n"; while(flag) { cout<<"Enter your choice:\n"; cin>>ch; switch(ch) { case 1:cout<<"Enter the new element:"; cin>>x; insert(x); display(); break; case 2: cout<<"Enter the element to be deleted\n"; cin>>x; deletion(x); display(); break; case 3: display(); break; case 4: flag=0; } } getch(); } }; void main() { clrscr();

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Linklist <int> l1; } Output : 1. 2. 3. 4. Add Delete Display Exit

Enter your choice: 1 Enter the new element : 12 Enter your choice: 1 Enter the new element : 5 Enter your choice: 1 Enter the new element : 6 Enter your choice: 3 5 6 12 Enter the your choice: 2 Enter the element to be deleted 6 5 12 Enter your choice: 4 Exit

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

CURSOR IMPLEMENTATION OF LIST ADT Ex.No.: Aim: To write a C++ program to implement the list using cursor. Algorithm:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. Start the program Create a menu in main program Get the choice The choice is one create a new The choice is two then insertion. The choice is three delete a element. The choice is four search The choice is five search The choice six exit Stop

Date:

Program:
#include<iostream.h> #include <conio.h> int main() { int a[20], i,j, ch,n,x; while (1) { clrscr (); cout<<"\n1.Create.. 2. insertion... 3.Delete.. 4.Display.. 5.Search.. 6.Exit..\n select :"; cin>>ch; switch(ch) { case 1: cout<<"Enter the Total no of value"; cin>>n; for(i=0;i<n;i++) { cout<<"Enter the "<<i+1<<" value"; cin>>a[i]; } break; case 2: cout<<" Enter the Insert Position"; cin>>i; for(j=n;j>i-1;j--) a[j]=a[j-1]; cout<<" Enter the Insert Value"; cin>>a[i-1]; n++; break;

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

case 3: if(n<0) cout<<"The List is empty"; else { cout<<" Enter the Deleting Position"; cin>>j; cout<<"The Delete element"<< a[j]; for(i=j-1;i<n;i++) a[i]=a[i+1]; n--; } break; case 4: for(i=0;i<n;i++) cout<<a[i]<<"\t"; break; case 5: if(n<0) cout<<"The List is empty"; else { cout<<" Enter the Searching element"; cin>>x; for(i=0;i<n;i++) if ( a[i]==x) { cout<<" the Given Element Found in " <<i+1 <<" Position"; goto aa; } cout<<" the Given Element Not Found "; aa: break; case 6: return (0); } getch(); } } } Output : 1.Create.. 2. insertion... 3.Delete.. 4.Display.. 5.Search.. 6.Exit select : 1 Enter the Total no of value 5 Enter the value 11 Enter the value 22 Enter the value 33 Enter the value 44 Enter the value 55 11 20 22 33 44 55

1.Create.. 2. insertion... 3.Delete.. 4.Display.. 5.Search.. 6.Exit select : 4 11 22 33 44 55

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

1.Create.. 2. insertion... 3.Delete.. 4.Display.. 5.Search.. 6.Exit select : 2 Enter the Insert Position 2 Enter the Insert Value 20 1.Create.. 2. insertion... 3.Delete.. 4.Display.. 5.Search.. 6.Exit select : 4 11 20 22 33 44 55

1.Create.. 2. insertion... 3.Delete.. 4.Display.. 5.Search.. 6.Exit select : 3 Enter the Deleting Position2 1.Create.. 2. insertion... 3.Delete.. 4.Display.. 5.Search.. 6.Exit select : 4 11 22 33 44 55 1.Create.. 2. insertion... 3.Delete.. 4.Display.. 5.Search.. 6.Exit select : 5 Enter the Searching element 55 The Given Element Found in Position 5 1.Create.. 2. insertion... 3.Delete.. 4.Display.. 5.Search.. 6.Exit select : 6 Exit

Result: Thus the cursor implementatiton using list has been successfully executed.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

STACK ADT USING ARRAY AND LINKED IMPLEMENTATION


I.

STACK ADT USING ARRAY IMPLEMENTATION Date:

Ex.No.:
Aim: To implement stack using arrays Objective:

To represent stack using an array and to perform In and Out operations in the stack.
Algorithm: 1. Declare an array sizeN. 2. Assign TOP as a pointer to denote the top element in the stack 3. Get the new element Y to be added in to the stack. 4. If TOP is greater than or equal to N then display stack over flow; otherwise set TOP=TOP+1. 5. Set S [TOP] = Y. 6. To delete top element from the stack check if TOP =0,the display stack underflow, otherwise decrement TOP by one, and display S [TOP+1]. 7. Display the stack S from 1 to TOP. Program : #include<iostream.h> #include <conio.h> int main() { clrscr (); int a[20], top=-1, i, ch; while (1) { cout<<"\n 1.Push.. 2.Pop.. 3.List.. 4. Exit..\n select :"; cin>>ch; switch(ch) { case 1: if(top>=19) cout<<"The stake is full"; else { cout<<"\n Enter the value"; cin>>a[++top]; } break; case 2: if(top<0) cout<<"\n The stake is empty"; else { cout<<"\n The Out element"<< a[top--]; } break; case 3: for(i=0;i<=top;i++) cout<<a[i]<<"\t"; break; case 4: return (0);

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

} getch(); } }

Output :
1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :1 Enter the value 10 1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :1 Enter the value 20 1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :1 Enter the value 30 1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :1 Enter the value 40 1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :3 10 20 30 40 1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :2 10 20 30 1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :2 10 20 1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :2 10 2 1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :2 10 1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :2 1.Push.. 2.Pop.. 3.List.. 4. Exit.. select :2 10 20 1.Push.. 2.Pop.. 3.List.. 4. Exit.. select : 4 Exit

RESULT:

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Thus the Stack ADT using array implementation has been successfully executed.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

II. STACK ADT USING LINKED IMPLEMENTATION


Aim: To implement stack using Link Algorithm:

1. Start the program 2. Create a menu in main program 3. Get the choice 4. If the choice is one Push 5. If choice is two Pop 6. If the choice is three Display list 7. else come out of the menu program 8. Stop Program : #include<iostream.h> #include<conio.h> class Node { public : int data; Node *next; void getdata() { cout<<"Enter the Data"; cin>>data; } void putdata() { cout<<data<<"\t"; } }; int main() { Node *a,*tmp; int ch; a = NULL; while(1) { cout<<"\n1. Push... 2. Pop... 3. Display... 4. Exit... \nSelect... : "; cin>>ch; switch(ch) { case 1: tmp=(Node*)new(Node); tmp->getdata(); tmp->next=a; a= tmp; break;

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

case 2: if(a==NULL) { cout<<" Stack is Empty"; } else { a->putdata(); a=a->next; } break; case 3: cout<<"The Elements are....:"; tmp=a; while(tmp!=NULL) { tmp->putdata(); tmp=tmp->next; } break; case 4: return(1); } getch(); }} Output : 1. Push... 2. Pop... 3. Display... 4. Exit... Select... :1 Enter the Data 10 1. Push... 2. Pop... 3. Display... 4. Exit... Select... :1 Enter the Data 20 1. Push... 2. Pop... 3. Display... 4. Exit... Select... :1 Enter the Data 30 1. Push... 2. Pop... 3. Display... 4. Exit... Select... :1 Enter the Data 40 1. Push... 2. Pop... 3. Display... 4. Exit... Select... :3 40 30 20 10 Push... 2. Pop... 3. Display... 4. Exit... Select... :2 30 20 10 Push... 2. Pop... 3. Display... 4. Exit... Select... :3 Exit RESULT:

1.

1.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Thus the Stack ADT using linked list implementation has been successfully executed.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

QUEUE ADT ARRAY AND LINKED LIST IMPLEMENTATIONS


I. QUEUE ADT ARRAY LIST IMPLEMENTATIONS

Ex.No.: Aim:
To write a C++ program to do the Queue operations.

Date:

Algorithm:
1. Start the Program. 2. Assign top= -1 in the main function. 3. Create a main program. 4. Get the choice. 5. If the choice 1 is In. 6. If the choice 2 is Out. 7. If the choice 3 is List. 8. If the choice 4 is come out of the main program. 9. Stop Program :

#include<iostream.h> #include <conio.h> int main() { clrscr (); int a[20], top=-1, i, ch; while (1) { cout<<"\n 1.Push.. 2.Pop.. 3.List.. 4. Exit..\n select :"; cin>>ch; switch(ch) { case 1: if(top>=19) cout<<"The stake is full"; else { cout<<"\n Enter the value"; cin>>a[++top]; } break; case 2: if(top<0) cout<<"\n The stake is empty"; else { cout<<"\n The Pop element"<< a[0]; for (i=1;i<=top;i++) a[i-1]=a[i]; top--; }

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

break; case 3: for(i=0;i<=top;i++) cout<<a[i]<<"\t"; break; case 4: return (0); } getch(); } } Output :
1.In.. 2.Out.. 3.List.. 4. Exit.. select :1` Enter the value 10 1.In.. 2.Out.. 3.List.. 4. Exit.. select :1 Enter the value 20 1.In.. 2.Out.. 3.List.. 4. Exit.. select :1 Enter the value 30 1.In.. 2.Out.. 3.List.. 4. Exit.. select :1 Enter the value 40 1.In.. 2.Out.. 3.List.. 4. Exit.. select :3 10 20 30 40 1.In.. 2.Out.. 3.List.. 4. Exit.. select :2 The Out Element is 10 1.In.. 2.Out.. 3.List.. 4. Exit.. select :3 20 30 40 1.In.. 2.Out.. 3.List.. 4. Exit.. select : 4 Exit

RESULT: Thus the queue ADT array list implementation has been successfully executed.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

II. QUEUE ADT LINKED LIST IMPLEMENTATIONS

Aim:
To write a C++ program to do the Queue operations.

Algorithm: 1. Start the program 2. Create a menu in main program 3. Get the choice 4. If the choice is one In 5. If choice is two Out 6. If the choice is three Display list 7. else come out of the menu program 8. Stop Program: #include<iostream.h> #include<conio.h> class Node { public : int data; Node *next; void getdata() { cout<<"Enter the Data"; cin>>data; } void putdata() { cout<<data<<"\t"; } }; int main() { Node *a,*h,*tmp; int ch; a = NULL; h = a; while(1) { cout<<"\n1. In... 2. Out... 3. Display... 4. Exit... \nSelect... : "; cin>>ch; switch(ch) { case 1:tmp= (Node*)new(Node); tmp->getdata(); tmp->next=a; a= tmp; break;

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

case 2: if(a==NULL) cout<<"Queue is Empty"; else if (a->next==NULL) { a->putdata(); a=NULL;} else { tmp=a; while (tmp->next->next!=NULL) tmp=tmp->next; tmp->next->putdata(); tmp->next=NULL; }break; case 3: cout<<"The Elements are....:"; tmp=a; while(tmp!=NULL) { tmp->putdata(); tmp=tmp->next; } break; case 4: return(1); } getch(); } } Output : 1.In... 2. Out... 3. Display... 4. Exit... Select... : 1 Enter the Data 10 1.In... 2. Out... 3. Display... 4. Exit... Select... : 1 Enter the Data 20 1.In... 2. Out... 3. Display... 4. Exit... Select... : 1 Enter the Data 30 1.In... 2. Out... 3. Display... 4. Exit... Select... : 1 Enter the Data 40 1.In... 2. Out... 3. Display... 4. Exit... Select... : 3 The Elements are....40 30 20 1.In... 2. Out... 3. Display... 4. Exit... Select... : 2 10 1.In... 2. Out... 3. Display... 4. Exit... Select... : 3 The Elements are....40 30 20 1.In... 2. Out... 3. Display... 4. Exit... Select... : 4 Exit

10

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Result: Thus the queue ADT linked list implementation has been successfully executed.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

QUICK SORT
Ex. No: Aim : To write a C++ program for Quick sort. Algorithm: 1. Start the program 2. Read the number of elements to be sorted 3. Initialize a pivot (center) , left and right positions 4. Check whether the center element is less than the left element. Then swap left and center values 5. Check whether the right element is less than the left element. Then swap right and left values 6. Check whether the right element is less than the center element. Then swap center and right 7. Finally the element in the left of pivot will be less than the pivot Element and the element to the right of the pivot will be greater Than the pivot element . 8. Repeat the steps until the given elements are arranged in a sorted Manner 9. Stop the program. PROGRAM: #include<process.h> #include<iostream.h> #include<conio.h> #include<stdlib.h> int Partition(int low,int high,int arr[]); void Quick_sort(int low,int high,int arr[]); void main() { int *a,n,low,high,i; clrscr(); cout<<"Enter number of elements:"; cin>>n; a=new int[n]; cout<<"enter the elements:"; for(i=0;i<n;i++) cin>>a[i]; cout<<"Initial Order of elements"; for(i=0;i<n;i++) cout<<a[i]<<" "; cout<<" "; high=n-1; low=0; Quick_sort(low,high,a); cout<<"Final Array After Sorting:"; Date:

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

for(i=0;i<n;i++) cout<<a[i]<<" "; getch(); } /*Function for partitioning the array*/ int Partition(int low,int high,int arr[]) { int i,high_vac,low_vac,pivot/*,itr*/; pivot=arr[low]; while(high>low) { high_vac=arr[high]; while(pivot<high_vac) { if(high<=low) break; high--; high_vac=arr[high]; } arr[low]=high_vac; low_vac=arr[low]; while(pivot>low_vac) { if(high<=low) break; low++; low_vac=arr[low]; } arr[high]=low_vac; } arr[low]=pivot; return low; } void Quick_sort(int low,int high,int arr[]) { int Piv_index,i; if(low<high) { Piv_index=Partition(low,high,arr); Quick_sort(low,Piv_index-1,arr); Quick_sort(Piv_index+1,high,arr); } } Output : Initial order of elements: Final array after sorting: 02 22 12 12 22 66 55 55 66 02

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Result: Thus the Quick sort has been successfully executed.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

INSERTION SORT
Ex. No: Aim : To write a C++ program for insertion sort. Algorithm: 1. Start the program 2.enter the size of array and elements. 3.compare the adjecent elements if it is not in correct order just swap the values. 4.from next pass depends on elements it will be swapped 5.in last pass, elements will be in order 6.stop the program. Program: #include <iostream.h> #include<conio.h> void insertion sort(int x[],int length) { int key,i; for(int j=1;j<length;j++) { key=x[j]; i=j-1; while(x[i]>key && i>=0) { x[i+1]=x[i]; i--; } x[i+1]=key; } } int main() { int A[25]; int size,i; int x; clrscr(); cout<<"Enter size of list"; cin>>size; cout<<"Enter numbers"; for(x=0;x<size;x++) { cin>>A[x]; } cout<<"NON SORTED LIST:"<<endl; for(x=0;x<size;x++) { Date:

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

cout<<A[x]<<endl; } insertion sort(A,size); cout<<endl<<"SORTED LIST"<<endl; for(x=0;x<size;x++) { cout<<A[x]<<endl; } getch(); return 0; } Output: Enter size of list: 5 Enter numbers:12 Enter numbers:22 Enter numbers:02 Enter numbers:15 Enter numbers:36 NON SORTED LIST: 12 22 02 15 36 SORTED LIST: 02 12 15 22 36

Result: Thus the insertion sort has been successfully executed.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

BUBBLE SORT
Ex. No: Aim : To write a C++ program for bubble sort. Algorithm: 1. Start the program. 2. Enter the array of elements. 3. Compare the adjacent elements if it is in wrong order. The adjacent element will be swapped. 4.The pass through the list is repeated until no swaps required 5.Stop the program. Program: #include <stdio.h> #include <iostream.h> #include<conio.h> void bubbleSort(int *array,int length) //Bubble sort function { int i,j; for(i=0;i<5;i++) { for(j=0;j<i;j++) { if(array[i]>array[j]) { int temp=array[i]; //swap array[i]=array[j]; array[j]=temp; } } } } void printElements(int *array,int length) //print array elements { int i=0; printf("after sorting:\n"); for(i=0;i<5;i++) cout<<array[i]<<endl; } void main() { Date:

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

int a[5]; clrscr(); printf("enter the elements:\n"); for(int i=0;i<5;i++) cin>>a[i]; // array elements printf("before sorting:\n"); for(i=0;i<5;i++) cout<<a[i]<<endl; bubbleSort(a,5); //call to bubble sort printElements(a,5); // print elements getch(); } Output: Enter numbers:12 Enter numbers:22 Enter numbers:02 Enter numbers:15 Enter numbers:36 Before sorting: 12 22 02 15 36 after sorting: 02 12 15 22 36

Result: Thus the bubble sort has been successfully executed.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

HEAP SORT
Ex. No: Aim : To write a C++ program for Heap sort. Algorithm: 1. Start the program 2. Construct a min heap, such that the value of the root node should Be less than the left key value & right key value. 3. Use an empty array for storing the sorted elements 4. Delete the root value (minimum element) and store it as a first element Of the array. 5. Delete the last value of the root and store it as a root 6. Reconstruct the min heap 7. Repeat these steps until we get the sorted array and display it. 8. Stop the program. PROGRAM: #include <stdio.h> #include<conio.h> #include<iostream.h> void fnSortHeap(int[], int); //prototype void main() //main { int i, arr_num_items; int arr[6]; clrscr(); cout<<"Enter the elements:"; for(i=0;i<6;i++) cin>>arr[i]; //total number of items in array. arr_num_items = 6; //call fnSortHeap function for(arr_num_items - 2) times. for(i=arr_num_items; i>1; i--) { fnSortHeap(arr, i - 1); } cout<<"The Sorted Array\n"; //print the sorted array for (i = 0; i < arr_num_items; i++) cout<<arr[i]<<"\n"; getch(); return; } //sort heap void fnSortHeap(int arr[], int arr_ubound) int i,o; int lChild, rChild, mChild, root, temp; //find the root element of the current element { Date:

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

root = (arr_ubound-1)/2; //creating the heap for(o=root;o>=0;o--) { for(i=root;i>=0;i--) { lChild = (2*i)+1; rChild = (2*i)+2; if ((lChild <= arr_ubound) && (rChild <= arr_ubound)) { if(arr[rChild] >= arr[lChild]) mChild = rChild; else mChild = lChild; } else { if(rChild > arr_ubound) mChild = lChild; else mChild = rChild; } if (arr[i] < arr[mChild]) //swap elements { temp = arr[i]; arr[i] = arr[mChild]; arr[mChild] = temp; } } } temp = arr[0]; //move the max element to the end of the array arr[0] = arr[arr_ubound]; arr[arr_ubound] = temp; return; } Output: Enter the elements : The sorted array : 21 02 12 05 02 11 30 12 32 21 11 25 05 30 25 32

Result: Thus the heap sort has been successfully executed.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

BINARY SEARCH TREE TRAVERSING


Ex. No: Aim: To write a C++ program for Binary search tree traversing. Algorithm: 1. Start the program 2. Select the menu choice 3. If the choice is 1 insert a node to binary tree 4. If the choice is 2 the node will be in Pre order traversal 5. If the choice is 3 the node will be in In order traversal 6 If the choice is 4 the node will be in Post order traversal 7. If the choice is 5 it exits the program 8. Stop the program. PROGRAM: #include<iostream.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> struct node { int data; node *left; node *right; }; node *tree=NULL; node *insert(node *tree,int ele); void preorder(node *tree); void inorder(node *tree); void postorder(node *tree); int count=1; void main() { int ch,ele; clrscr(); do { clrscr(); cout<<"\n\t\a\a1----INSERT A NODE IN A BINARY TREE.\a\a"; cout<<"\n\t\a\a2----PRE-ORDER TRAVERSAL.\a\a"; cout<<"\n\t\a\a3----IN-ORDER TRAVERSAL.\a\a"; cout<<"\n\t\a\a4----POST-ORDER TRAVERSAL.\a\a"; cout<<"\n\t\a\a5----EXIT.\a\a"; cout<<"\n\t\a\aENTER CHOICE::\a\a"; cin>>ch; switch(ch) { Date:

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

case 1: cout<<"\n\t\a\aENTER THE ELEMENT::\a\a"; cin>>ele; tree=insert(tree,ele); break; case 2: cout<<"\n\t\a\a****PRE-ORDER TRAVERSAL OF A TREE****\a\a"; preorder(tree); break; case 3: cout<<"\n\t\a\a****IN-ORDER TRAVERSAL OF A TREE****\a\a"; inorder(tree); break; case 4: cout<<"\n\t\a\a***POST-ORDER TRAVERSAL OF A TREE***\a\a"; postorder(tree); break; case 5: exit(0); } }while(ch!=5); } node *insert(node *tree,int ele) { if(tree==NULL) { tree=new node; tree->left=tree->right=NULL; tree->data=ele; count++; } else if(count%2==0) tree->left=insert(tree->left,ele); else tree->right=insert(tree->right,ele); return(tree); } void preorder(node *tree) { if(tree!=NULL) { cout<<tree->data; preorder(tree->left); preorder(tree->right);

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

getch(); } } void inorder(node *tree) { if(tree!=NULL) { inorder(tree->left); cout<<tree->data; inorder(tree->right); getch(); } } void postorder(node *tree) { if(tree!=NULL) { postorder(tree->left); postorder(tree->right); cout<<tree->data; getch(); } } Output: 1.INSERT ANODE IN A BINARY TREE 2. PRE ORDER TRAVEL 3. IN-ORDER ORDER TRAVEL 4.POST ORDER TRAVEL 5.EXIT ENTER THE CHOICE 1 11 ENTER THE CHOICE 1 22 ENTER THE CHOICE 1 33 ENTER THE CHOICE 2 11 22 33 ENTER THE CHOICE 3 22 11 33 ENTER THE CHOICE 4 22 33 11 ENTER THE CHOICE 5 EXIT

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Result: Thus the Binary search tree traversing has been successfully executed.

STACK APPLICATION CONVERSION OF INFIX TO POSTFIX


Ex. No: Aim: To write a C++ program for conversion of infix to postfix. Algorithm: 1. Start the program 2. Enter the equation 3.CONVERT THE infix expressio to ppostfix using post function 4.print the values of infix and postfix expression 5. Stop the program. PROGRAM: #include<iostream.h> #include<conio.h> class stack { char ch[20],eq[20],st[20]; char top; int x,y,z; public: stack() { top='+'; x=y=0; z=-1; } void get(); void post(); int prcd(char,char); }; void stack::get() { cout<<"enter any equation"; cin>>st; cout<<"\n"<<st; } void stack::post() { while(st[x]!='\0') { if((st[x]>='A'&&st[x]<='Z')||(st[x]>='a'&&st[x]<='z')) ch[y++]=st[x]; else if(st[x]==')') { Date:

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

while(eq[z]!=')') ch[y++]=eq[z--]; top=eq[--z]; } else { while(z>=0&&prcd(top,st[x]==1)) { ch[y++]=top; top=eq[--z]; } eq[++z]=st[x]; top=st[x]; } x++; } while(z>=0) { if(eq[z]=='(') z--; else ch[y++]=eq[z--]; } ch[y]='\0'; cout<<"\n"<<ch; } int stack::prcd(char g,char r) { if(r=='('||r=='$') return 0; else if(g=='$'||g=='*'||g=='/') return 1; else if(r=='*' || g=='9'||r=='/') return 0; else return 1; } void main() { stack s; clrscr(); s.get(); s.post(); getch(); } Output: Enter any equation a+b

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

a+b ab+ Result: Thus the infix to postfix conversion has been successfully executed.

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

Você também pode gostar