3rd sem II ECE

download 3rd sem II ECE

of 63

  • date post

    02-Dec-2014
  • Category

    Documents

  • view

    214
  • download

    3

Embed Size (px)

Transcript of 3rd sem II ECE

J.K.K.MUNIRAJAH COLLEGE OF TECHNOLOGY,T.N.PALAYAM638506 DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING. EC 2209 - DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB MANUAL

YEAR: II

SEM:III

LIST OF EXPERIMENTSEXP 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 The next two exercises are to be done by implementing the following source files a) b) c) d) Program source files for Stack Application 1 Array implementation of Stack ADT Linked list implementation of Stack ADT 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) 7 8 9 10 and implementing file (c)) Queue ADT Array and linked list implementations Search Tree ADT - Binary Search Tree Heap Sort Quick Sort

CLASS AND OBJECT IMPLEMENTATIONEx.No.: 1a Aim To write a C++ programs for basic operations. Date:

Algorithm: 1. 2. 3. 4. 5. 6. 7. Start the program Create a class Declare the variables and function Create an object for class name Calling a function through an object Execute the program Stop

PROGRAM #include #include Class student { Int regno;

Char name[20]; Public: Void setdata(int r, char*n); Void put data() { Coutnext!=NULL) { printf("\t %d",t->no); t=t->next; } printf("\t %d \t",t->no);} OUTPUT:Enter the number of nodes: 4 Enter the elements: 10 Enter the elements: 20

Enter the element: 30 Enter the element: 40 Enter your option: 1.Insert 2.Delete 3.Display 4.Exit List elements are 10 20 30 40 Enter your option 1 Enter the elements to be inserted 3 Enter your option: 3 List elements are: 10 20 50 30 Enter your option 2 Enter the position to be deleted 3 Enter your option 3 List elements are 10 20 30 40 Enter your option 4

RESULT: Thus the c program singly linked list was executed successfully. LINKED LIST IMPLEMENTATION OF LIST ADT- DOUBLY LINKED LIST Ex.No.: 2 Aim: To write a C++ program for doubly linked list Algorithm: Date:

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 which has double link If choice is two then insert into the node ,change the appropriate link If the choice is three Delete from the node, remove the link and join it else come out of the menu program Stop

PROGRAM: #include #include typedef struct s1 { int val; struct s1*next;

struct s1*prev; }node; node*head; node*getnode() { node*temp; temp=(node*)malloc(sizeof(node)); temp->next=NULL; temp->prev=NULL; return temp; } void create() { node*t,*temp; int n,f=0; char ch='y'; while(ch=='Y'||ch=='y') { printf ("\n Enter the Element:"); scanf ("%d",&n); t=getnode(); t->val=n; t->next=NULL; if(f==0) { t->prev=NULL; head=t; f=1; } else {temp=head; while(temp->next) temp=temp->next; t->prev=temp; temp->next=t; } printf("Doyou want to add one more Element(y/n):"); fflush(stdin); scanf("%c",&ch); } } void display() {node*t; int n=0,c=1; while(c!=3) { printf("\n 1.Forward\n 2.In Reverse\n 3.exit"); scanf("%d",&c); switch(c) {

case 1: n=0; printf("\n The Element are:"); t=head; while(t) { n=n+1; printf("\n%d",t->val); t=t->next; } printf("\n Total Number of Elements:%d",n); break; case 2: n=0; printf("\n In Reverse order"); t=head; while(t->next) t=t->next; while(t>0) {n=n+1; printf("\n%d",t->val); t=t->prev; } printf("\n Total Number of Element:%d",n+1); break; } }} void del(int pos) { node *t,*p,*nt; int n=0; t=p=head; n=0; if(pos==0) { p=head->next; p->prev=NULL; head=head->next; free(t); }else { while(nnext; n++; } p=t->next; t->next=t->next->next; nt=p->next; nt->prev=t; free(p);

} printf("\n Node Deleted"); } void insert(int val, int pos) { int n=0; node*t,*temp,*p; temp=getnode(); temp->val=val; if(pos==0) { temp->next=head; temp->prev=NULL; temp->prev=temp; head=temp; } else { t=head; while(nnext; n++;} p=t->next; temp->next=t->next; temp->prev=t; t->next=temp; p->prev=temp; } printf("\n Value Inserted"); } /*void search(int pos) { int n=0; node*t; t=head; while(nnext; n++; } printf("\n Value in Position %d is:%d",pos,t->val); } */ void main() { int v,p,ch=1; clrscr(); while(ch!=0) { printf("\n1.Create\n2.Display\n3.Insert\n4.Delete\n0.Exit\nEnter your choice:"); scanf("%d",&ch);

switch(ch) { case 1: create(); break; case 2: display(); break; case 3: printf("\n Enter Value to insert and position to insert:"); scanf("%d%d",&v,&p); insert(v,p); break; case 4: printf("\n Enter position to delete:"); scanf("%d",&p); del(p); break; /*case 5: printf("\n Enter Position to find:"); scanf("%d",&p); search(p); break;*/ } } }

OUTPUT: Enter the number of nodes: 4 Enter the elements: 10 Enter the elements: 20 Enter the element:

30 Enter the element: 40 Enter your option: 1.Insert 2.Delete 3.Display 4.Exit List elements are 10 20 30 40 Enter your option 1 Enter the elements to be inserted 3 Enter your option: 3 List elements are: 10 20 50 30 Enter your option 2 Enter the position to be deleted 3 Enter your option 3 List elements are 10 20 30 40 Enter your option 4

RESULT: Thus the c program doubly linked list was executed successfully.

CURSOR IMPLEMENTATION OF LIST ADT Ex. No.: 4 Aim: To write a C++ program to implement the list using cursor. Date:

Algorithm: 1. Start the program 2. Create a menu in main program 3. Get the choice 4. The choice is one create a new 5. The choice is two then insertion. 6. The choice is three delete a element. 7. The choice is four search 8. The choice is five search 9. The choice six exit 10. Stop

PROGRAM: #include #include #include"curslist.h" void main() {

LIST L=-1; POSITION P; int choice,place,x; clrscr(); printf("\n1.Create\n2.Insert\n3.Delete\n4.MakeEmpty\n5.Display\n6.Find\n7.Exit"); A: printf("\nEnter ur choice:\t"); scanf("%d",&choice); switch(choice) { case 1: if(L==-1) { InitializeCursor(); L=CursorAlloc(); } else printf("\nList is already created"); break; case 2: if(L==-1) printf("\nList is not yet initialized"); else { printf("\nWhere u want to insert?"); scanf("%d",&place); printf("\nEnter the element to insert"); scanf("%d",&x); Insert(x,place); } break; case 3: if(L==-1) printf("\nList is not yet initialized"); else { printf("\nWhich element you want to delete?"); scanf("%d",&x); Delete(x,L); } break; case 4: if(L==-1) printf("\nList is not yet initialized"); else MakeEmpty(L); break; case 5: if(L==-1) printf("\nList is not yet initialized");

else Display(); break; case 6: if(L==-1) printf("\nList is not yet initialized"); else { printf("\nWhich element you want to search?"); scanf("%d",&x); P=Find(x,L); printf("\nThe element is at %d",P); } break; case 7: exit(0); default: printf("\n *******WRONG ENTRY*******"); } goto A; }

OUTPUT 1.Create 2.Insert 3.Delet 4.Make empty

5.Display 6.Find 7.Exit Enter ur choice: 1 Enter ur choice: 2 Where you want to insert ?5 Enter the element to insert 10 Position is not in the list Enter your choice 5 0 0 3 1 -1 -1 2 -1 -1 3 0 -1 4 0 4 5 0 5 6 0 6 7 7 8 8 9 9 Enter your choice:6 Which element you want to search ?6 The element is at -1 Enter ur choice:6 Which elements you want to search?-1 The element is at -1 Enter ur choice:7 Exit

RESULT: Thus the c program cursor linked list was executed successfully.

STACK ADT USING ARRAY IMPLEMENTATION Ex.No.: 5 a Date:

Aim:

To implement stack using arrays Algorithm:1. 2. 3. 4. Declare an array sizeN. Assign TOP as a pointer to denote the top element in the stack Get the new element Y to be added in to the stack. 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 #include int main() { clrscr (); int a[20], top=-1, i, ch;

while (1) { coutch; switch(ch) { case 1: if(top>=19) cout