Ds&Oops Lab Manual-ece

23
www.Vidyarthiplus.com www.Vidyarthiplus.com Page 1 www.Vidyarthiplus.com EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB MANUAL PREPARED BY S.JOHN JOSEPH,AP/IT

Transcript of Ds&Oops Lab Manual-ece

Page 1: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 1

www.Vidyarthiplus.com

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED

PROGRAMMING LAB MANUAL

PREPARED BY

S.JOHN JOSEPH,AP/IT

Page 2: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 2

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

List Of Experiments

1. Basic Programs for C++ Concepts

2. Array implementation of List Abstract Data Type (ADT)

3. Linked list implementation of List ADT

4. Cursor implementation of List ADT

5. Stack ADT - Array and linked list implementations

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)

6. 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))

7. Queue ADT – Array and linked list implementations

8. Search Tree ADT - Binary Search Tree

9. Heap Sort

10. Quick Sort

Page 3: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 3

DEFAULT ARGUMENTS Aim: To implement function with default arguments. Algorithm: 1. Declare the default function. 2. Invoke the default function. 3. Display the result Program: #include<iostream.h> void printLine(char =’_’,int =70); void main() { printLine(); printLine(‘/’); printLine(‘*’,40); printLine(‘R’,55); } void printLine(char ch, int Repeatcount) { int i;cout<<endl;for(i=0;i<Repeatcount;i++)cout<<ch; } Output: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * R R R R R R R R R R R R R R R R R R R R R R R

Page 4: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 4

CLASS WITH STATIC DATA MEMBER Aim: To implement static data member in class. Algorithm: 1. Create class ITEM with static data member as count. 2. Create a member function to increment the count. 3. Declare the static datamember using scope resolution operator. 4. Display the count value. Program: #include<iostream.h> class item { static int count;int num; public:void getdata(int a) { num=a;count++;cout<<”Number”<<num; } void showcount() cout<<”count”;cout<<count<<”\n”; } }; int item::count;int main() { item a,b,c; a.showcount(); b.showcount(); c.showcount(); a.getdata(20); b.getdata(30); c.getdata(40); a.showcount(); b.showcount(); c.showcount(); }

Page 5: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 5

Output: count 0count 0count 0 Number 20 Number 30 Number 40 count 3count 3count 3

CLASS AND OBJECTS Aim: To write a c++ program for employee wages calculation using class and objects. Algorithm: 1.Employee class contains name and wage variable and member function a putname(), putwage(),getwage() and getname(). 2.Putname: Assign the valve for the character array name. 3 .Getname: Ret r i eves the va lue of Var iab le name. 4 .Putwage: Ass ign the va lve for wage var iab le . 5 .Getwage: Ret r i eves the va lue of wage va r iab le . 6 . Im main( ) Put and d i sp lay both name and wages Program: #include <iostream.h> using namespace std;class employee { char name[80]; // private by default public:void putname(char *n); // these are publicvoid getname(char *n); private:double wage; // now, private again public:void putwage(double w); // back to public double getwage(); }; void employee::putname(char *n) { strcpy(name, n); } void employee::getname(char *n) { strcpy(n, name); } void employee::putwage(double w) {

Page 6: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 6

wage = w; } double employee::getwage() { return wage; } int main() { employee ted;char name[80]; ted.putname("Ted Jones"); ted.putwage(75000); ted.getname(name); cout << name << " makes $";cout << ted.getwage() << " per year.";return 0; } Output: Ted Jones makes $75000 per year.

STACK USING ARRAY IMPLEMENTATION

Aim: To write a program for stack using array implementation. Algorithm : Step1:Define a array which stores stack elements.. Step 2: The operations on the stack area)PUSH data into the stack b)POP data out of stack Step 3: PUSH DATA INTO STACK 3a.Enter the data to be inserted into stack. 3b.If TOP is NULLthe input data is the first node in stack.the link of the node is NULL.TOP points to that node. 3c.If TOP is NOT NULLthe link of TOP points to the new node.TOP points to that node. Step 4: POP DATA FROM STACK 4a.If TOP is NULLthe stack is empty 4b.If TOP is NOT NULLthe link of TOP is the current TOP.the pervious TOP is popped from stack. Step 5. The stack represented by linked list is traversed to display its content. PROGRAM: #include<stdio.h> #include<conio.h> #define SIZE 5 int stack[SIZE], top=-1; void push(); void pop(); void display(); void main() { int choice; int isempty();

Page 7: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 7

int length(); clrscr(); while(1) { printf(“\n 1.Push”); printf(“\n 2. POP”); printf(“\n 3.Display”); printf(“\n 4. Length ”); printf(“\n 5.Quit”); printf(“\n Enter the choice:”); scanf(“\n %d”,&choice); switch(choice) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: printf(“\n No. of elements in the stack is %d”,length()); break; case 5: exit(0); break; default: printf(“\n Invalid choice”); } } } void push() { int n; if(top==SIZE-1) printf(“\n Stack is full”); else

{

printf(“\nEnter the no.”);

scanf(“%d”,&n);

top++;

stack[top]=n;

}

}

void pop()

Page 8: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 8

{

int n;

if(isempty())

{

printf(“\nStack is empty”);

top=-1;

}

Else

{

n=stack[top];

printf(“\n %d is popped from the stack \n”,n);--top;

}

}

void display()

{

int i,temp=top;

if(isempty())

{

printf(“\n Stack Empty”);

return;

}

printf(“%d \n”,stack[i]);

}

int isempty()

{

return (top==-1);

}

int length()

{

return (top+1);

} Output: 1.Push2. POP3.Display4. Length5.Quit Enter the choice: 1 Enter the no. 10 1.Push2. POP3.Display4. Length5. Quit Enter the choice: 1 Enter the no. 20 1.Push2. POP3.Display4. Length5. Quit Enter the choice: 1 Enter the no. 30

Page 9: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 9

1.Push2. POP3.Display4. Length5. Quit Enter the choice: 1 Enter the no. 40 1.Push2. POP3.Display 4. Length5. Quit Enter the choice: 3 Elements in the stack:40302010 1.Push2. POP3.Display4. Length5. Quit Enter the choice: 2 40 is popped from the stack 1.Push2. POP3.Display4. Length5. Quit Enter the choice: 4 Number of elements in the stack is 3 1.Push2. POP3.Display4. Length5. Quit Enter the choice: 5

STACK USING LINKED LIST

Aim: To write a program for stack using linked list implementation. Algorithm : Step1:Define a C-struct for each node in the stack. Each node in the stack contains data and link to the next node. TOP pointer points to lastnode inserted in the stack. Step 2: The operations on the stack area)PUSH data into the stack b)POP data out of stack Step 3: PUSH DATA INTO STACK 3a.Enter the data to be inserted into stack. 3b.If TOP is NULLthe input data is the first node in stack.the link of the node is NULL.TOP points to that node. 3c.If TOP is NOT NULLthe link of TOP points to the new node.TOP points to that node. Step 4: POP DATA FROM STACK 4a.If TOP is NULLthe stack is empty 4b.If TOP is NOT NULLthe link of TOP is the current TOP.the pervious TOP is popped from stack. Step 5. The stack represented by linked list is traversed to display its content.

Page 10: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 10

PROGRAM: # include<stdio.h> # include<conio.h> struct node { int info;struct node *link; } *top=NULL;main() { int choice; while(1) { printf("1.Push\n"); printf("2.Pop\n"); printf("3.Display\n"); printf("4.Quit\n"); printf("Enter your choice : ") ; scanf("%d", &choice); switch(choice) { case 1: push(); break;case 2: pop(); break;case 3:display(); break;case 4:exit(1); default : printf("Wrong choice\n"); }/*End of switch */}/*End of while */}/*End of main() */ push() { struct node *tmp;int pushed_item;tmp = (struct node *)malloc(sizeof(struct node)); printf("Input the new value to be pushed on the stack : "); scanf("%d",&pushed_item);tmp->info=pushed_item;tmp->link=top;top=tmp;}/*End of push()*/ pop() {struct node *tmp; if(top == NULL) printf("Stack is empty\n"); else { tmp=top; printf("Popped item is %d\n",tmp->info); top=top->link;free(tmp);}}/*End of pop()*/display() { struct node *ptr; ptr=top;if(top==NULL) printf("Stack is empty\n");else{ printf("Stack elements :\n");while(ptr!= NULL) { printf("%d\n",ptr->info); ptr = ptr->link;

Page 11: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 11

}/*End of while */}/*End of else*/}/*End of display()*/ Output: 1.Push2. POP3.Display5.Quit Enter the choice: 1 Input the new value to be pushed on the stack :. 10 1.Push2. POP3.Display5.Quit Enter the choice: 1 Input the new value to be pushed on the stack : 20 1.Push2. POP3.Display5.Quit Enter the choice: 1 Input the new value to be pushed on the stack : 30 1.Push2. POP3.Display5.Quit Enter the choice: 1 Input the new value to be pushed on the stack : 40 1.Push2. POP3.Display5.Quit Enter the choice: Elements in the stack:40 30 20 10

1.Push2. POP3.Display5.Quit

Enter the choice: 2

40 is popped from the stack 1.Push2. POP3.Display5.Quit

Enter the choice: 5

QUEUE USING ARRAY Aim: To write a program for Queue using array implementation. Algorithm : Step1:Define a array which stores queue elements. Step 2: The operations on the queue are a)INSERT data into the queue b)DELETE data out of queue Step 3: INSERT DATA INTO queue 3a.Enter the data to be inserted into queue.

Page 12: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 12

3b.If TOP is NULLthe input data is the first node in queue.the link of the node is NULL.TOP points to that node. 3c.If TOP is NOT NULLthe link of TOP points to the new node.TOP points to that node. Step 4: DELETE DATA FROM queue 4a.If TOP is NULLthe queue is empty 4b.If TOP is NOT NULLthe link of TOP is the current TOP.the pervious TOP is popped from queue.Step 5. The queue represented by linked list is traversed to display its conten PROGRAM: # include<stdio.h> # define MAX 5 int queue_arr[MAX]; int rear = -1; int front = -1; main() { int choice; while(1) { printf("1.Insert\n"); printf("2.Delete\n"); printf("3.Display\n"); printf("4.Quit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1 : insert(); break; case 2 : del(); break; case 3: display(); break; case 4: exit(1); default: printf("Wrong choice\n"); }/*End of switch*/}/* End of while*/}/*End of main()*/ insert() { int added_item;if (rear==MAX-1) printf("Queue Overflow\n");

Page 13: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 13

else{if (front==-1) /*If queue is initially empty */ front=0; printf("Input the element for adding in queue : "); scanf("%d", &added_item); rear=rear+1; queue_arr[rear] = added_item ; } }/*End of insert()*/ del() {if (front == -1 || front > rear) { printf("Queue Underflow\n"); return ;} else { printf("Element deleted from queue is : %d\n", queue_arr[front]);front=front+1; } }/*End of del() */display() { int i; if (front == -1) printf("Queue is empty\n"); else { printf("Queue is :\n"); for(i=front;i<= rear;i++) printf("%d ",queue_arr[i]); printf("\n"); } }/*End of display() */

Output: 1.Insert 2.Delete 3.Display

Page 14: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 14

4.Quit Enter your choice:1 Input the element for adding in queue :10 1.Insert 2.Delete 3.Display 4.Quit Enter your choice:1 Input the element for adding in queue :20 1.Insert2.Delete3.Display4.Quit Enter your choice:1 Input the element for adding in queue :30 1.Insert2.Delete3.Display4.Quit Enter your choice:1 Input the element for adding in queue :40 1.Insert2.Delete3.Display4.Quit Enter your choice:3 Queue is :40302010 1.Insert2.Delete3.Display4.Quit Enter your choice:2 Element deleted from queue is :10 1.Insert2.Delete3.Display4.Quit Enter your choice:3 Queue is :403020 1.Insert2.Delete3.Display4.Quit Enter your choice:4

BINARY SEARCH TREE Aim: To write a c program for binary search tree. Algorithm:

Page 15: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 15

1.Declare function add(),search(),findmin().find(),findmax(),Display(). 2.Create a structure for a tree contains left pointer and right pointer. 3.Insert an element is by checking the top node and the leaf node and the operationwill be performed. 4.Deleting an element contains searching the tree and deleting the item.5 . d i s p l a y t h e T r e e e l e m e n t s . Program: #include<stdio.h> #include<stdlib.h> #include<conio.h> struct searchtree { int element;struct searchtree *left,*right; } *root; typedef struct searchtree *node; typedef int ElementType; node insert(ElementType, node); node delete(ElementType, node); void makeempty(); node findmin(node); node findmax(node); node find(ElementType, node); void display(node, int); void main() { int ch;ElementType a; node temp;makeempty(); while(1) { printf("\n1. Insert\n2. Delete\n3. Find min\n4. Find max\n5. Find\n6.Display\n7. Exit\nEnter Your Choice : "); scanf("%d",&ch);switch(ch) { case 1: printf("Enter an element : "); scanf("%d", &a);root = insert(a, root);break; case 2:printf("\nEnter the element to delete : "); scanf("%d",&a); root = delet(a, root); break;case 3: printf("\nEnter the element to search : "); scanf("%d",&a); temp = find(a, root); if (temp != NULL)

Page 16: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 16

printf("Element found"); elseprintf("Element not found"); break; case 4: temp = findmin(root); if(temp==NULL) printf("\nEmpty tree"); elseprintf("\nMinimum element : %d", temp->element); break; case 5: temp = findmax(root);if(temp==NULL)printf("\nEmpty tree");elseprintf("\nMaximum element : %d", temp->element);break; case 6:if(root==NULL) printf("\nEmpty tree"); elsedisplay(root, 1); break;case 7:exit(0); default printf("Invalid Choice");}}} node insert(ElementType x,node t) { if(t==NULL) { t = (node)malloc(sizeof(node));t->element = x;t->left = t->right = NULL; } Else {if(x < t->element)t->left = insert(x, t->left);else if(x > t->element)t->right = insert(x, t->right); } return t; } node delet(ElementType x,node t) {node temp;if(t == NULL) printf("\nElement not found"); else{if(x < t->element)t->left = delet(x, t->left); else if(x > t->element)t->right = delet(x, t->right); else {if(t->left && t->right){temp = findmin(t->right);t->element = temp->element;t->right = delet(t->element,t->right);}else if(t->left == NULL)t=t->right; else t=t->left; } } return t; } void makeempty() {root = NULL;}

Page 17: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 17

node findmin(node temp) {if(temp == NULL || temp->left == NULL) return temp; return findmin(temp->left); } node findmax(node temp){if(temp==NULL || temp->right==NULL)return temp;return findmin(temp->right); } node find(ElementType x, node t) {if(t==NULL) return NULL;if(x<t->element) return find(x,t->left);if(x>t->element) return find(x,t->right);return t; } void display(node t,int level){int i;if(t){display(t->right, level+1);printf(“\n”);for(i=0;i<level;i++)printf(" "); printf("%d", t->element);display(t->left, level+1); }}

Output:

1. Insert 2. Delete 3. Find 4. Find Min 5. Find Max 6. Display 7. Exit

Enter your Choice : 1 Enter an element : 10 1. Insert 2. Delete 3. Find 4. Find Min 5. Find Max 6. Display 7. Exit Enter your Choice : 1Enter an element : 20 1. Insert2. Delete3. Find4. Find Min5. Find Max6. Display7. Exit

Enter your Choice : 1Enter an element : 5 1. Insert2. Delete3. Find4. Find Min5. Find Max6. Display7. Exit Enter your Choice : 4 The smallest Number is 5 1. Insert2. Delete

Page 18: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 18

2. Find4. Find Min5. Find Max6. Display7. Exit Enter your Choice : 3 Enter an element : 100 Element not Found 1. Insert2. Delete3. Find4. Find Min5. Find Max6. Display7. Exit

Enter your Choice : 2 Enter an element : 20 1. Insert2. Delete3. Find4. Find Min5. Find Max6. Display7. Exit Enter your Choice : 6510 1. Insert2. 2. Delete3. 3. Find 4. Find Min 5. Find Max 6. Display 7. Exit Enter your Choice 7

Page 19: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 19

QUICK SORT

Aim: To write a c program to perform quick sort. Algorithm: 1 .Get the va lue of how many no . to be sor t ed . 2 .Get the e lements f rom the user . 3.Two function qsort() and swap(). Quick sort to perform sorting. 4 .Swap( ) i s j us t to rear range the va lues . 5.Quick sort algorithm works by partitioning the array to be sorted, then recursivelysorting each partition. 6 . D i s p l a y t h e s o r t e d v a l u e . Program: #include<stdio.h> #include<conio.h> int i,j,n,pi,a[20]; void qsort(int a[],int ft,int lt); void swap(in a[],int i,int j); void main(){int n,i,a[20]; clrscr(); printf(“\nEnter the size:”); scanf(“%d”,&n); printf(“Enter the elements:”); for(i=0;i<n;i++)scanf(“%d”,&a[i]);qsort(a,0,n-1); printf(“\nSorted List:”); for(i=0;i<n;i++) printf(“\n%d\t”,a[i]); getch(); } void qsort(int a[],int lb,int ub)

Page 20: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 20

{ if(lb<ub) { pi=a[lb];i=lb; j=ub;while(i<j) { while(a[i]<=pi && i<ub) i++; while(a[i]>=pi && j>lb) j--;if(i<j) swap(a,i,j); }swap(a,lb,j); qsort(a,lb,j-1); qsort(a,j+1,ub); } } void swap(int a[],int i,int j) { int t;t=a[i]; a[i]=a[j];a[j]=t;} Output: Enter the size:5 Enter the elements: 85 32 46 04 96 Sorted list: 04 32 46 85 96

Page 21: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 21

HEAP SORT

Aim: To write a c program to perform heap sort. Algorithm: 1 .Get the s i ze of the a r ray f rom the user . 2 . G e t t h e e l e m e n t s t o b e s o r t e d . 3.Sorting is performed when we call the heap sort function. 4 .Now the a r ray i s conta ined wi th sor ted e l ements . 5 .Disp lay the sor ted e lements . Program: #include<stdio.h> #include<conio.h> void heapsort(int x[],int); void main() { int x[100],i,n;clrscr(); printf(“\nEnter the size:”); scanf(“%d”,&n); printf(“Enter %d elements:”,n); for(i=0;i<n;i++) { scanf(“%d”,&x[i]);}for(i=n;i>1;i--)

Page 22: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 22

{ heapsort(x,i-1); } printf(“\nSorted elements are:”); for(i=0;i<n;i++) { printf(“-->%d\n”,x[i]); }getch(); }void heapsort(int x[],int arr) { int i,m;int lc,rc,mc,rt,temp;rt=(arr-1)/2; for(m=rt;m>=0;m--) { for(i=rt;i>=0;i--) { lc=(2*i)+1;rc=(2*i)+2; if((lc<=arr)&&(rc<=arr)) { if(x[rc]>=x[lc]) mc=rc;elsemc=lc; } else { if(rc>arr)mc=lc; Else mc=rc; } if(x[i]<x[mc]) { temp=x[i]; x[i]=x[mc]; x[mc]=temp; } } } temp=x[0]; x[0]=x[arr]; x[arr]=temp; return;} Output: Enter the size:5 Enter 5 elements:90 67 12 75 01

Page 23: Ds&Oops Lab Manual-ece

www.Vidyarthiplus.com

www.Vidyarthiplus.com Page 23

sorted elements:1 12 67 75 9 0