data structures lab manual

36
1 M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI 621 105. Department of Computer Science and Engineering LABORATORY MANUAL CS 2208 DATA STRUCTURES LABORATORY EX: NO: 1 (a) SINGLY LINKED LIST AIM: To write a Program to implement a single linked list ALGORITHM: Step 1: Start Step 2:Read the value of ch Step 3:If ch=1 call create list functions Step 4:If ch=2 call insert list functions Step 5:If ch=3 call delete list functions Step 6:If ch=4 call view list functions Step 7:Repeat while (ch!=5) Step 8:Stop ALGORITHM FOR CREATE LIST Step 1: Read value of item Step 2:Allocate the memory far new node Step 3:Assign values to new node data part Step 4:Assign new node address part as null ALGORITHM FOR INSERT LIST Step 1: Read the value of item Step 2:Allocate the memory far new node Step 3:Assign values of data field as null else make link fields of all new nodes to point Step 4:starting node to new node Step 5:Set the external pointer from the starting node to new node ALGORITHM FOR DELETE LIST Step 1:If the link is empty then return else check whether the list contains more than one element Step 2:Move the start pointer to the next node Step 3:Free the first node ALGORITHM FOR VIEW LIST Step 1:Using the for loop i Step 2:Print the linked list M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI 621 105. Department of Computer Science and Engineering LABORATORY MANUAL CS 2208 DATA STRUCTURES LABORATORY Step 3:Stop PROGRAM : #include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int item; struct node *link; }; typedef struct node NODE; NODE *head; NODE *getnode(); void readnode(NODE *newnode); void createlist(); void insertfirst(); void deletefirst(); void viewlist(); void main() { int ch; clrscr(); printf("\n\n\t\t SINGLY LINKEDLIST\n"); printf("\t\t *****************\n"); do { printf("\n 1.CREATE LIST"); printf("\n 2.INSERT FIRST"); printf("\n 3.DELETE FIRST"); printf("\n 4.VIEW LIST"); printf("\n 5.EXIT"); printf("\n\nEnter Your Choice:"); scanf("%d",&ch); switch(ch) { case 1: createlist();

Upload: karal-marx-s

Post on 11-Nov-2014

75 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Data Structures Lab manual

1

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

EX: NO: 1 (a) SINGLY LINKED LIST

AIM: To write a Program to implement a single linked list

ALGORITHM:

Step 1: Start Step 2:Read the value of ch Step 3:If ch=1 call create list functions Step 4:If ch=2 call insert list functions Step 5:If ch=3 call delete list functions Step 6:If ch=4 call view list functions Step 7:Repeat while (ch!=5) Step 8:Stop

ALGORITHM FOR CREATE LIST

Step 1: Read value of item Step 2:Allocate the memory far new node Step 3:Assign values to new node data part Step 4:Assign new node address part as null

ALGORITHM FOR INSERT LIST

Step 1: Read the value of item Step 2:Allocate the memory far new node Step 3:Assign values of data field as null else make link fields of all new nodes to point Step 4:starting node to new node Step 5:Set the external pointer from the starting node to new node

ALGORITHM FOR DELETE LIST

Step 1:If the link is empty then return else check whether the list contains more than one element Step 2:Move the start pointer to the next node Step 3:Free the first node

ALGORITHM FOR VIEW LIST

Step 1:Using the for loop i Step 2:Print the linked list

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

Step 3:Stop

PROGRAM :

#include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int item; struct node *link; }; typedef struct node NODE; NODE *head; NODE *getnode(); void readnode(NODE *newnode); void createlist(); void insertfirst(); void deletefirst(); void viewlist(); void main() { int ch; clrscr(); printf("\n\n\t\t SINGLY LINKEDLIST\n"); printf("\t\t *****************\n"); do { printf("\n 1.CREATE LIST"); printf("\n 2.INSERT FIRST"); printf("\n 3.DELETE FIRST"); printf("\n 4.VIEW LIST"); printf("\n 5.EXIT"); printf("\n\nEnter Your Choice:"); scanf("%d",&ch); switch(ch) { case 1: createlist();

Page 2: Data Structures Lab manual

2

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

break; case 2: insertfirst(); break; case 3: deletefirst(); break; case 4: viewlist(); break; case 5: exit(0); } } while(ch<=5); } NODE *getnode() { NODE *newnode; newnode=(NODE*)malloc(sizeof (NODE)); return(newnode); } void readnode(NODE *newnode) { printf("\nEnter the item:"); scanf("%d",&newnode->item); fflush(stdin); newnode->link=NULL; } void createlist() { NODE *prev,*newnode; char ch; head = NULL; do { newnode=getnode(); readnode(newnode);

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

if(head==NULL) { head=newnode; prev=head; } else { prev->link =newnode; prev=newnode; } printf("\nDo You Want To Continue:"); scanf("%c",&ch); } while(ch=='y'); } void insertfirst() { NODE *newnode; newnode=getnode(); readnode(newnode); if(head==NULL) { head=newnode; } else { newnode->link=head; head=newnode; } } void deletefirst() { NODE *prev; if(head==NULL) return; else {

Page 3: Data Structures Lab manual

3

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

prev=head; head=head->link; free(prev); } } void viewlist() { NODE *loc; printf("\nELEMENTS\n"); loc=head; do { printf("\t%d->",loc->item); loc=loc->link; } while(loc!=NULL); printf("NULL"); } OUT PUT:

1.CREATE LIST 2.INSERT FIRST 3.DELETE FIRST 4.VIEW LIST 5.EXIT

Enter Your Choice:1 Enter the item:2 Do You Want To Continue:y Enter the item:3 Do You Want To Continue:n 1.CREATE LIST 2.INSERT FIRST 3.DELETE FIRST 4.VIEW LIST 5.EXIT Enter Your Choice:4 ELEMENTS 2-> 3->NULL 1.CREATE LIST 2.INSERT FIRST

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

3.DELETE FIRST 4.VIEW LIST 5.EXIT Enter Your Choice:2 Enter the item:1 1.CREATE LIST 2.INSERT FIRST 3.DELETE FIRST 4.VIEW LIST 5.EXIT Enter Your Choice:4 ELEMENTS 1-> 2-> 3->NULL 1.CREATE LIST 2.INSERT FIRST 3.DELETE FIRST 4.VIEW LIST 5.EXIT Enter Your Choice:3 1.CREATE LIST 2.INSERT FIRST 3.DELETE FIRST 4.VIEW LIST 5.EXIT Enter Your Choice:4 ELEMENTS 2-> 3->NULL 1.CREATE LIST 2.INSERT FIRST 3.DELETE FIRST 4.VIEW LIST 5.EXIT Enter Your Choice:5 RESULT: The above program has been executed successfully and the out put has been Verified.

Page 4: Data Structures Lab manual

4

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

Viva Questions

1. What is a computer Program? 2. Define Programming 3. What are the steps needed to develop a program

in any programming language? 4. What are the descriptions that should be included

before developing a program? 5. What is mean by Debugging and testing? 6. What are the three types of error that may occur

while running a program? 7. What are the steps involved in correcting a

program? 8. What is mean by Top down design approach? 9. What the steps are in involved in Top down design

approach? 10. Define Algorithm. 11. Mention the steps involved in developing an

algorithm. 12. Define efficiency of an algorithm. 13. What is meant by time complexity of an algorithm? 14. What are the 2 types of time complexity? 15. Define Compilation time. 16. Define Runtime. 17. Define worst case of an algorithm. 18. Define best case of an algorithm. 19. Define average case of an algorithm. 20. What is meant by space complexity of an

algorithm? EX: NO: 1 (b) DOUBLE LINKED LIST

AIM: To write a program to implement a Double linked list.

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

ALGORITHM:

Step 1:Start Step 2:Read the value of ch Step 3:If ch=1 call create list functions Step 4:If ch=2 call insert list functions Step 5:If ch=3 call delete list functions Step 6:If ch=4 call view list functions Step 7:If ch=5 call the exit functions Step 8:Repeat while (ch!=5) Step 9:Stop

ALGORITHM FOR CREATE LIST

Step 1:Read the value of item Step 2:Allocate the memory far newly assigned nodes Step 3:Assign the data to the data field Step 4:Assign forward and backward link as Null

ALGORITHM FOR INSERT LIST

Step 1:Read the value of item Step 2:Allocate a new node and assign the item to the data part Step 3:If head is NULL return Step 4:Assign link of new node to head and blink of new node to NULL link of head node Step 5:To new node change head ptr to print the new node

ALGORITHM FOR DELETE LIST

Step 1:Check the head node as null return empty Step 2:Else change the head pointer to the head pointer link Step 3:Change the new head ptr blink as null

ALGORITHM FOR VIEW LIST

Step 1:Using the for loop Step 2:Print the required list Step 3:Stop

Page 5: Data Structures Lab manual

5

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

PROGRAM

#include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int item; struct node *plink,*nlink; }; typedef struct node NODE; NODE *head; NODE *getnode(); void readnode(NODE *newnode); void createlist(); void insertfirst(); void deletefirst(); void viewlist(); void main() { int ch; clrscr(); printf("\n\n\t DOUBLY LINKEDLIST\n"); printf("\t *****************\n"); do { printf("\n 1.CREATE LIST"); printf("\n 2.INSERT FIRST"); printf("\n 3.DELETE FIRST"); printf("\n 4.VIEW LIST"); printf("\n 5.EXIT"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: createlist(); break;

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

case 2: insertfirst(); break; case 3: deletefirst(); break; case 4: viewlist(); break; case 5: exit(0); } }while(ch<=5); } NODE *getnode() { NODE *newnode; newnode=(NODE*)malloc(sizeof(NODE)); return(newnode); } void readnode(NODE *newnode) { printf("\nEnter the item:"); scanf("%d",&newnode->item); fflush(stdin); newnode->plink=NULL; newnode->nlink=NULL; } void createlist() { NODE *prev,*newnode; char ch; head=NULL; do { newnode=getnode(); readnode(newnode); if(head==NULL)

Page 6: Data Structures Lab manual

6

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

{ head=newnode; prev=head; } else { prev->nlink=newnode; newnode->plink=prev; prev=newnode; } printf("\nDo you want to continue:"); scanf("%c",&ch); } while(ch=='y'); } void insertfirst() { NODE *newnode; newnode=getnode(); readnode(newnode); if(head==NULL) { head=newnode; } else { newnode->nlink=head; head->plink=newnode; head=newnode; } } void deletefirst() { NODE *prev; if(head==NULL) return; else {

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

prev=head; head=head->nlink; head->plink=NULL; free(prev); } } void viewlist() { NODE *loc; printf("\nELEMENTS\n"); loc=head; do { printf("%2d<->",loc->item); loc=loc->nlink; } while(loc!=NULL); printf("NULL"); }

OUT PUT 1.CREATE LIST 2.INSERT FIRST 3.DELETE FIRST 4.VIEW LIST 5.EXIT Enter Your Choice:1 Enter the item:2 Do You Want To Continue:y Enter the item:3 Do You Want To Continue:n 1.CREATE LIST 2.INSERT FIRST 3.DELETE FIRST 4.VIEW LIST 5.EXIT Enter Your Choice:4 ELEMENTS

Page 7: Data Structures Lab manual

7

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

2<-> 3<->NULL 1.CREATE LIST 2.INSERT FIRST 3.DELETE FIRST 4.VIEW LIST 5.EXIT Enter Your Choice:2 Enter the item:1 1.CREATE LIST 2.INSERT FIRST 3.DELETE FIRST 4.VIEW LIST 5.EXIT Enter Your Choice:4 ELEMENTS 1<-> 2<-> 3<->NULL 1.CREATE LIST 2.INSERT FIRST 3.DELETE FIRST 4.VIEW LIST 5.EXIT Enter Your Choice:3 1.CREATE LIST 2.INSERT FIRST 3.DELETE FIRST 4.VIEW LIST 5.EXIT Enter Your Choice:4 ELEMENTS 2<-> 3<->NULL 1.CREATE LIST 2.INSERT FIRST 3.DELETE FIRST 4.VIEW LIST 5.EXIT Enter Your Choice:5

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

RESULT: The above program has been executed successfully and the out put has been verified. Viva Questions

1. What are the 3 different space needed for determining the amount of memory used?

2. What is an asymptotic notation? Mention its types. 3. Define Big Oh notation. 4. Define Omega notation. 5. Define Theta notation. 6. Define Little Oh notation. 7. Define Dynamic programming. 8. Define Back Tracking. 9. Define Divide and conquer algorithm. 10. Define Brute force algorithm. 11. What is an array? 12. What is meant, by zero origin subscripting? 13. What are the subscripted Variable? 14. How will you declare multidimensional array? 15. Give an example for multidimensional array?

EX: NO: 2 POLYNOMIAL ADDITIONS AIM: Represent a polynomial as a linked list and write functions for polynomial addition. ALGORITHM:

Step 1:while p and q are not null repat step 2 Step 2:if power of the two term are equal then if the term do not cancel then insert num of terms into the sum polynomial advance p , advance q, else if the power first polynom ial-> power of the

Page 8: Data Structures Lab manual

8

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

second then insert the term from second polynomial in to sum polynomial advance q

Step 3:copy remaining terms from the non empty polynomial in to the sum polynomial

PROGRAM : #include<stdio.h> #include<malloc.h> #include<conio.h> struct link { int coeff; int pow; struct link *next; }; struct link *poly1=NULL,*poly2=NULL,*poly=NULL; void create(struct link *node) { char ch; do { printf("\n enter coeff:"); scanf("%d",&node->coeff); printf("\n enter power:"); scanf("%d",&node->pow); node->next=(struct link*)malloc(sizeof(struct link)); node=node->next; node->next=NULL; printf("\n continue(y/n):"); ch=getch(); } while(ch=='y' || ch=='Y'); } void show(struct link *node) { while(node->next!=NULL) {

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

printf("%dx^%d",node->coeff,node->pow); node=node->next; if(node->next!=NULL) printf("+"); } } void polyadd(struct link *poly1,struct link *poly2,struct link *poly) { while(poly1->next && poly2->next) { if(poly1->pow>poly2->pow) { poly->pow=poly1->pow; poly->coeff=poly1->coeff; poly1=poly1->next; } else if(poly1->pow<poly2->pow) { poly->pow=poly2->pow; poly->coeff=poly2->coeff; poly2=poly2->next; } else { poly->pow=poly1->pow; poly->coeff=poly1->coeff+poly2->coeff; poly1=poly1->next; poly2=poly2->next; } poly->next=(struct link *)malloc(sizeof(struct link)); poly=poly->next; poly->next=NULL; } while(poly1->next || poly2->next) { if(poly1->next) {

Page 9: Data Structures Lab manual

9

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

poly->pow=poly1->pow; poly->coeff=poly1->coeff; poly1=poly1->next; } if(poly2->next) { poly->pow=poly2->pow; poly->coeff=poly2->coeff; poly2=poly2->next; } poly->next=(struct link *)malloc(sizeof(struct link)); poly=poly->next; poly->next=NULL; } } main() { char ch; do { poly1=(struct link *)malloc(sizeof(struct link)); poly2=(struct link *)malloc(sizeof(struct link)); poly=(struct link *)malloc(sizeof(struct link)); printf("\nenter 1st number:"); create(poly1); printf("\nenter 2nd number:"); create(poly2); printf("\n1st Number:"); show(poly1); printf("\n2nd Number:"); show(poly2); polyadd(poly1,poly2,poly); printf("\nAdded polynomial:"); show(poly); printf("\n add two more numbers:"); ch=getch(); } while(ch=='y' || ch=='Y');

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

return(0); }

OUT PUT:

enter 1st number: enter coeff:3 enter power:4 continue(y/n): enter coeff:6 enter power:2 continue(y/n): enter coeff:4 enter power:1 continue(y/n): enter coeff:5 enter power:0 continue(y/n): enter 2nd number: enter coeff:2 enter power:3 continue(y/n): enter coeff:3 enter power:1 continue(y/n): enter coeff:7 enter power:0 continue(y/n): 1st Number:3x^4+6x^2+4x^1+5x^0 2nd Number:2x^3+3x^1+7x^0 Added polynomial:3x^4+2x^3+6x^2+7x^1+12x^0 add two more numbers: RESULT: Thus the program for implementing polynomial addition is executed.

Page 10: Data Structures Lab manual

10

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

Viva Questions 1. What is a string? 2. How will you declare a string variable? Give an

example. 3. How will you concatenate two strings? 4. How will you find the length of a string? 5. How will you find compare two strings? 6. How will you convert a string to its lower case? 7. How will you convert a string to its upper case? 8. Give general format for strnicmp() function. 9. Give general format for strncpy () function. 10. Give general format for strchr() function. 11. Give general format for strstr() function. 12. What is meant by array if strings? 13. What is a pointer? 14. What is the use of pointer? 15. How will you declare a pointer?

EX: NO: 3 CONVERSION OF INFIX TO POST FIX

EXPRESSION AIM: To convert the infix to post fix expression using the concept of linked list

ALGORITHM:

Step 1: Include the header files Step 2:Allocate the memory for linked list Step 3:Delete the structure for the node Step 4:Read the infix expression and find the length of the expression Step 5:If the current character is open parenthesis ‘(‘ then push it into the stack Step 6:When the current character is an operand then add it to the

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

result Step 7:When the current character is operator check the priority of scanned operator with top of the stack. If the priority of the scanned character is greater than top of the stack Step 8: If the element of the current operator is less than or equal to the top of the stack then

i) pop the top character from the stack ii) push the scanned operator into the stack

Step 9 When the current character is closing parenthesis then pop all the characters above opening parenthesis if any from the stack Step10 Return the result Step 11 End

PROGRAM

#include<stdio.h> #include<conio.h> #include<string.h> #include<math.h> #include<ctype.h> char pop(void); void push(char); int priority(char); int top; char s[80],result[80]; void main() { int len,i,j; char a[80]; clrscr(); printf("Enter the expression"); scanf("%s",a); len=strlen(a); a[len]=')'; a[len+1]='\0'; push('('); i=0;j=0; while(a[i]) { if(isalpha(a[i]))

Page 11: Data Structures Lab manual

11

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

result[j++]=a[i]; else { if(a[i]=='(') push('('); else { if(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/') { if(priority(a[i])>priority(s[top])) push(a[i]); else { while(priority(a[i])<priority(s[top])) result[j++]=pop(); if (priority(a[i])==priority(s[top])) result[j++]=pop(); push(a[i]); } } else { if (a[i]==')') { while(priority(a[i])<priority(s[top])) result[j++]=pop(); pop(); }}}} i++; } result[j]='\0'; printf("Postfix expression is %s",result); getch(); } char pop() { return(s[top--]);

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

} void push(char ele) { s[++top]=ele; } int priority(char ch) { switch(ch) { case '+':return(4); case '-':return(4); case '*':return(5); case '/':return(5); case '(':return(0); case ')':return(0); } }

OUT PUT

enter the infix expression is: a+b*c-d/e postfix expression is: abc*+de/- RESULT: Thus the infix expression is converted into postfix using linked list. Viva Questions

1. How will you access a pointer variable? 2. What is use of size of () operator? 3. What is meant by pointer to an array? 4. What is meant by pointer to pointers? 5. What is meant by function pointers? 6. What is use of malloc() function? 7. What is use of calloc() function? 8. What is use of free() function? 9. What is a structure?

Page 12: Data Structures Lab manual

12

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

10. What is tag? 11. What is use of dot(.) operator? 12. What is meant by nested structure? 13. What is meant by array of structures? 14. What is meant by array within structures? 15. Difference between array and structures. 16. How will declare a pointer to a structure? 17. What is a Union? Give some examples. 18. What is meant by a bit field? 19. Difference between Union and structures 20. Define Data Structure.

EX: NO: 4 DOUBLE ENDED QUEUE AIM: Implement a double-ended queue (de queue) where insertion and deletion operations are possible at both the ends.

ALGORITHM: ROUTINE TO INSERT AN ELEMENT

Step 1: Start Step 2: If (front==0) && (rear==size-1) Then Write “queue over flow’ Exit Step 3: If ( side==”front”) If front>0 Then Front <-front – 1 Q[front]=x; Else Write “ no space in front “ Exit

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

Step 4: If (side==”rear”) Then Rear<-rear+1; Q[rear]=x; Exit; Step 5: End; ROUTINE TO DELETE ELEMENT Step 1: start Step 2:If(front> rear) Then Write “queue underflow” Return NULL; Else If(front==rear) Then Item=q[rear]; Front=0; Step 3: Rear=-1; Else If(ride=”front”) Then Item<-q[front] Front=front+1; Else If(side =”rear”) Then Item=q[rear]; Rear=rear-1; Step 4:Return item Step 5:End

PROGRAM

# include <conio.h> #include<stdio.h> # define MAX 5 input_que();

Page 13: Data Structures Lab manual

13

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

output_que(); insert_right(); delete_left(); delete_right(); display_queue(); insert_left(); int deque_arr[MAX]; int left = -1; int right = -1; main() { int choice; printf("1.Input restricted dequeue\n"); printf("2.Output restricted dequeue\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1 : input_que(); break; case 2: output_que(); break; default: printf("Wrong choice\n"); } return(choice); } input_que() { int choice; while(1) { printf("1.Insert at right\n"); printf("2.Delete from left\n"); printf("3.Delete from right\n"); printf("4.Display\n");

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

printf("5.Quit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: insert_right(); break; case 2: delete_left(); break; case 3: delete_right(); break; case 4: display_queue(); break; default: printf("Wrong choice\n"); } } output_que() { int choice; while(1) { printf("1.Insert at right\n"); printf("2.Insert at left\n"); printf("3.Delete from left\n"); printf("4.Display\n"); printf("5.Quit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: insert_right();

Page 14: Data Structures Lab manual

14

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

break; case 2: insert_left(); break; case 3: delete_left(); break; case 4: display_queue(); break; default: printf("Wrong choice\n"); } } } insert_right() { int added_item; if((left == 0 && right == MAX-1) || (left == right+1)) { printf("Queue Overflow\n"); } if (left == -1) { left = 0; right = 0; } else if(right == MAX-1) right = 0; else right = right+1; printf("Input the element for adding in queue : "); scanf("%d", &added_item); deque_arr[right] = added_item ; return(0); }

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

insert_left() { int added_item; if((left == 0 && right == MAX-1) || (left == right+1)) { printf("Queue Overflow \n"); return(0); } if (left == -1) { left = 0; right = 0; } else if(left== 0) left=MAX-1; else left=left-1; printf("Input the element for adding in queue : "); scanf("%d", &added_item); deque_arr[left] = added_item ; return(0); } delete_left() { if (left == -1) { printf("Queue Underflow\n"); return(0); } printf("Element deleted from queue is : %d\n",deque_arr[left]); if(left == right) /*Queue has only one element */ { left = -1; right=-1; } Else

Page 15: Data Structures Lab manual

15

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

if(left == MAX-1) left = 0; else left = left+1; return(0); } delete_right() { if (left == -1) { printf("Queue Underflow\n"); return(0); } printf("Element deleted from queue is : %d\n",deque_arr[right]); if(left == right) /*queue has only one element*/ { left = -1; right=-1; } else if(right == 0) right=MAX-1; else right=right-1; return(0); } display_queue() { int front_pos = left,rear_pos = right; if(left == -1) { printf("Queue is empty\n"); return(0); } printf("Queue elements :\n"); if( front_pos <= rear_pos )

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

{ while(front_pos <= rear_pos) { printf("%d ",deque_arr[front_pos]); front_pos++; } } else { while(front_pos <= MAX-1) { printf("%d ",deque_arr[front_pos]); front_pos++; } front_pos = 0; while(front_pos <= rear_pos) { printf("%d ",deque_arr[front_pos]); front_pos++; } } printf("\n"); return(0); } OUT PUT

1.Input restricted dequeue 2.Output restricted dequeue Enter your choice : 1 1.Insert at right 2.Delete from left 3.Delete from right 4.Display 5.Quit Enter your choice : 1 Input the element for adding in queue : 2 1.Insert at right

Page 16: Data Structures Lab manual

16

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

2.Delete from left 3.Delete from right 4.Display 5.Quit Enter your choice : 1 Input the element for adding in queue : 3 1.Insert at right 2.Delete from left 3.Delete from right 4.Display 5.Quit Enter your choice : 1 Input the element for adding in queue : 4 1.Insert at right 2.Delete from left 3.Delete from right 4.Display 5.Quit Enter your choice : 1 Input the element for adding in queue : 6 1.Insert at right 2.Delete from left 3.Delete from right 4.Display 5.Quit Enter your choice : 4 Queue elements : 2 3 4 6 1.Insert at right 2.Delete from left 3.Delete from right 4.Display 5.Quit Enter your choice : 2 Element deleted from queue is : 2 1.Insert at right 2.Delete from left 3.Delete from right

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

4.Display 5.Quit Enter your choice : 4 Queue elements : 3 4 6 1.Insert at right 2.Delete from left 3.Delete from right 4.Display 5.Quit Enter your choice : 3 Element deleted from queue is : 6 1.Insert at right 2.Delete from left 3.Delete from right 4.Display 5.Quit Enter your choice : 4 Queue elements : 3 4 RESULT: Thus the above program has been executed successfully. Viva Questions

1. What are the 2 types of Data Structure? 2. What is ADT? 3. What is meant by abstraction? 4. Give the general classification of data structures. 5. What is meant by linear data structure? 6. How can you classify linear data structure? 7. What is meant by non - linear data structure? 8. Give the classification of non - linear data

structure? 9. What are the types of linear linked data structure?

Page 17: Data Structures Lab manual

17

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

10. What is meant by linked list? 11. What are the advantages of using linked list? 12. Give the classification of linked list. 13. What is meant by single linked list? 14. What are the advantages of using Single linked

list? 15. What are the disadvantages of using Single linked

list? 16. What are the advantages of using Double linked

list?

EX: NO: 5 BINARY TREE TRAVERSAL

AIM: To write a program to implement a binary tree traversal

ALGORITHM:

Step 1: Start Step 2:Read the value of ch Step 3:If ch=1 read the values as numbers Step 4:While (num!=0) call insert (tree, num) Step 5:If ch=2 call the inorder to prefer inorder traversal Step 6:If ch=3 call the post order to prefer post order traversal Step 7:If ch=4 call the pre order to prefer pre order traversal Step 8:If ch=5 exit the operations Step 9:Repeat while ch!=5) Step10:End

ALGORITHM FOR INSERT

Step 1:Start Step 2:If ( tree –null) allocate memory space to tree Step 3:Iassign num to true ->item Step 4:Set null to tree ->child and tree->rchild Step 5:If( num < tree -> item) insert the num on the left side of node by replacing the step

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

Step 6:Else (num > tree -> item) insert the num on the right side of node and repeat the step 5 Step 7:Else write duplicate value Step 8:Return(tee) Step 9:Stop

ALGORITHM FOR IN ORDER

Step 1:Start Step 2:If ( tree!= null) Step 3:Visit the left child Step 4:Write tree ->item Step 5:Visit the right child Step 6:Stop

ALGORITHM FOR PRE - ORDER

Step 1: Start Step 2:If true!=null Step 3:Write tree ->item Step 4:Visit the left child Step 5:Visit the right child Step 6:Stop

ALGORITHM FOR POST ORDER

Step 1:Start Step 2:If (true !=null) Step 3Visit the left child Step 4:Visit the right child Step 5:Write tree -> item Step 6:Stop

PROGRAM

#include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int val;

Page 18: Data Structures Lab manual

18

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

struct node *lptr,*rptr; }; struct node*str; int ch; void main() { void create(void); void display(void); str=NULL; clrscr(); do { printf("\n\t BINARY TREE TRAVERSAL\n"); printf("\t *********************\n"); printf("\n 1.CREATE \n"); printf("\n 2.DISPLAY\n"); printf("\n Enter ur choice:"); scanf("%d",&ch); switch(ch) { case 1: create(); break; case 2: display(); break; } } while(ch!=2); } void create() { struct node *temp,*prev; int c,n; printf("\n Enter the number of elements:"); scanf("%d",&n); printf("\n Enter the node elements:\n");

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

do { scanf("%d",&c); temp=str; if(temp==NULL) { str=(struct node*)malloc(sizeof(struct node)); temp=str; } else { while(temp!=NULL) { prev=temp; if(c<temp->val)

temp=temp->lptr; else temp=temp->rptr; } temp=(struct node*)malloc(sizeof(struct node)); if(c<prev->val) prev->lptr=temp; else prev->rptr=temp; } temp->val=c; temp->lptr=NULL; temp->rptr=NULL; n--; }while(n>0); } void in(struct node *str) { if(str!=NULL) {

Page 19: Data Structures Lab manual

19

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

in(str->lptr); printf("\t%d",str->val); in(str->rptr); } } void pre(struct node *str) { if(str!=NULL) { printf("\t%d",str->val); pre(str->lptr); pre(str->rptr); } } void post(struct node *str) { if(str!=NULL) { post(str->lptr); post(str->rptr); printf("\t%d",str->val); } } void display() { void in(struct node *p); void pre(struct node *p); void post(struct node *p); do { printf("\n\n 1.INORDER \n"); printf("\n 2.PREORDER\n"); printf("\n 3.POSTORDER\n"); printf("\n 4.EXIT\n"); printf("\n Enter ur choice:"); scanf("%d",&ch); switch(ch)

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

{ case 1: in(str); break; case 2: pre(str); break; case 3: post(str); break; case 4: exit(0); } }while(ch!=4); } OUT PUT 1.CREATE 2.DISPLAY Enter ur choice:1 Enter the number of elements:5 Enter the node elements: 10 5 3 9 23 BINARY TREE TRAVERSAL ********************* 1.CREATE 2.DISPLAY Enter ur choice:2 1.INORDER 2.PREORDER 3.POSTORDER 4.EXIT Enter ur choice:1 3 5 9 10 23 1.INORDER

Page 20: Data Structures Lab manual

20

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

2.PREORDER 3.POSTORDER 4.EXIT Enter ur choice:2 10 5 3 9 23 1.INORDER 2.PREORDER 3.POSTORDER 4.EXIT Enter ur choice:3 3 9 5 23 10 1.INORDER 2.PREORDER 3.POSTORDER 4.EXIT Enter ur choice:4 RESULT: The above program has been executed successfully and the out put has been verified. Viva Questions

1. What are the disadvantages of using Single linked list?

2. Any 2 difference of Single and Double linked list. 3. What are the 2 types of header linked list? 4. What do you mean by grounded header linked

list? 5. What do you mean by Circular header linked list? 6. What do you mean by grounded header linked

list? 7. What are the applications of linked list? 8. Define a stack. 9. What are the 2 implementations of stack? 10. What are the operations that can be performed on

a stack?

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

11. What are the applications of stack? 12. What is meant by infix notation? Give an example. 13. What is meant by prefix notation? Give an

example. 14. What is meant by postfix notation? Give an example.

EX: NO: 6 BINARY SEARCH TREE

AIM: To write a program to implement a binary search tree ALGORITHM: Step1 : Start Step2 : Read the value of ch Step3 : If ch=1 insert function should be called Step 4: If ch=2 delete function should be called Step 5:If ch=3 view function should be called Step 6: If ch=4 exit insert function should be called Step 7 ; Return while ch!4 Step 8: Stop

ALGORITHM FOR INSERT

Step 1:If (tree =null), empty allocate memory space to tree Step 2: Assign num into tree -> Lchild item Step 3: Set null to tree ->Lchild item and free -> rchild Step 4: If (num < tree -> item) insert num on the Lchild of the node by repeating the step 4. Step 5: Else ( num > tree -> item ) insert the num on rchild of the node by repeating step 5 else Step 6: Return tree

ALGORITHM FOR DELETE

Step Start Step Assign p=tree and v=null Step Search for the element to be deleted Step Check if P-> Lchild=NULL , P->rchild inplacing node Step Check if P=NULL, condition tree print key don’t exist

Page 21: Data Structures Lab manual

21

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

Step Else check if p->rchild=null tree->lchild is replaced node Step Check if (f1=P) assign rp->rchild to f->Lchild and p->rchild to rp->rchild Step Assign p->lchild to rp->lchild Step Check q =null and assign replacing node is root node Step Else if p=q ->lchild attach the rp node as the last if q, other wise attach rp nodes on right of q ALGORITHM FOR INSERT

Step start Step Check if (true!=NULL) Step Execute visit left child Step Print tree -> item Step Stop PROGRAM

#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<alloc.h> typedef struct node { int data; struct node *left,*right; }tree; void displaymenu(); void readnode(tree *); void releasenode(tree *head); tree *getnode(); tree *createbtree(); tree *insertnode(tree *btree,tree *temp); tree *deletenode(int digit,tree *btree); tree *searchnode(tree *btree,int key); void view(tree *btree,int level); void main() {

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

int choice,key; tree *btree=NULL,*temp; clrscr(); printf("\n\n\t\tIMPLEMENTATION OF BINARY SEARCH TREE\n"); printf("\t\t************************************\n"); displaymenu(); while(1) { printf("\n Enter ur choice:"); scanf("%d",&choice); switch(choice) { case 0: displaymenu(); break; case 1: btree=NULL; printf("\n CREATE A NEW BINARY TREE\n"); btree=createbtree(); break; case 2: printf("\n INSERT THE NODE IN THE TREE\n"); temp=getnode(); readnode(temp); btree=insertnode(btree,temp); break; case 3: if(btree==NULL) printf("\n BINARY TREE IS EMPTY\n"); else { printf("\n DELETE THE NODE FROM THE TREE\n"); printf("\n ENTER THE NODE FOR DELETING :"); scanf("%d",&key); btree=deletenode(key,btree); } break;

Page 22: Data Structures Lab manual

22

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

case 4: if(btree==NULL) printf("\n BINARY TREE IS EMPTY\n"); else { printf("\n SEARCH THE NODE IN THE TREE\n\n"); printf("\n ENTER THE SEARCHING ELEMENT:"); scanf("%d",&key); temp=searchnode(btree,key); if(temp==NULL) printf("\n SEARCH ELEMENT %d IS NOT FOUND",key); else printf("\n SEARCH ELEMENT %d IS FOUND ",temp->data); } break; case 5: if(btree!=NULL) { printf("\n BINARY SEARCH TREE IS\n"); view(btree,1); } else printf("\n BINARY TREE IS EMPTY\n"); break; default: printf("\n END OF THE RUN OF UR PROGRAM\n"); releasenode (btree); exit(0); } } } tree *getnode() { int size; tree *newnode; size=sizeof(tree); newnode=(tree *)malloc(size);

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

return(newnode); } void readnode(tree *newnode) { printf("\n ENTER THE DATA:"); scanf("%d",&newnode->data); newnode->left=NULL; newnode->right=NULL; } void releasenode(tree *head) { free(head); } tree *createbtree()

{ char ch; tree *btree=NULL,*temp; do { temp=getnode() readnode(temp); btree=insertnode(btree,temp); fflush(stdin); printf("\n DO U WISH TO ADD DATA IN THE TREE(Y/N)?"); scanf("%c",&ch); } while(ch=='y'); return btree; } tree *insertnode(tree *btree,tree *temp) { if(btree==NULL) return temp; else if(temp->data<btree->data) btree->left=insertnode(btree->left,temp); else if(temp->data>btree->data)

Page 23: Data Structures Lab manual

23

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

btree->right=insertnode(btree->right,temp); else if (temp->data==btree->data) { printf("\n DATA IS ALREADY EXISTING...."); return btree; } return btree; } tree *deletenode(int key,tree *btree) { tree *p,*fop,*suc,*fosuc; p=btree; fop=NULL; while(p!=NULL&&p->data!=key) { fop=p; p=(key<p->data)?p->left:p->right; } if(p==NULL) { printf("\n ELEMENT IS NOT FOUND .."); return btree; } if(p->left!=NULL&&p->right!=NULL) { for(fosuc=p,suc=p->right;suc->left!=NULL;suc=suc->left) fosuc=suc; if(suc->right==NULL) { if(fosuc->right==suc) fosuc->right=NULL; else if(fosuc->left==suc) fosuc->left=NULL; } else if(suc->right!=NULL) { if(fosuc->right==suc) fosuc->right=suc->right;

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

else if (fosuc->left==suc) fosuc->left=suc->right; } suc->left=p->left; suc->right=p->right; if(fop==NULL) return suc; if(fop->left==p) fop->left=suc; if(fop->left==p) fop->left=suc; else if(fop->right==p) fop->right=suc; return btree; } else if(p->left==NULL&&p->right==NULL) { if(fop==NULL) return NULL; else if(fop->right==p) fop->right=NULL; else if (fop->left==p) fop->left=NULL; } else if((p->left!=NULL&&p->right==NULL)||(p->left==NULL&&p->right!=NULL)) { if(fop==NULL) return((p->right!=NULL)?p->right:p->left); else if(fop->right==p) fop->right=((p->right!=NULL)?p->right:p->left); else if(fop->left==p) fop->left=((p->left!=NULL)?p->right:p->left); } releasenode(p); return btree; } tree *searchnode(tree *btree,int key)

Page 24: Data Structures Lab manual

24

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

{ if(btree==NULL) return NULL; else if (key<btree->data) return searchnode(btree->left,key); else if (key>btree->data) return searchnode(btree->right,key); else if(key==btree->data) return btree; } void view(tree *btree,int level) { int k; if(btree==NULL) return; view(btree->right,level+1); printf("\n"); for(k=0;k<level;k++) printf(" "); printf("%d",btree->data); view(btree->left,level+1); } void displaymenu() { printf("\n BASIC OPERATIONS IN A BINARY SEARCH TREE\n"); printf("\n 0.SHOW MENU"); printf("\n 1.CREATE BINARY TRE"); printf("\n 2.INSERT A NODE"); printf("\n 3.DELETE A NODE"); printf("\n 4.SEARCH A NODE"); printf("\n 5.VIEW THE BINARY TREE"); printf("\n 6.EXIT\n"); } OUT PUT BASIC OPERATIONS IN A BINARY SEARCH TREE 0.SHOW MENU

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

1.CREATE BINARY TRE 2.INSERT A NODE 3.DELETE A NODE 4.SEARCH A NODE 5.VIEW THE BINARY TREE 6.EXIT Enter ur choice:1 CREATE A NEW BINARY TREE ENTER THE DATA:10 DO U WISH TO ADD DATA IN THE TREE(Y/N)?y ENTER THE DATA:7 DO U WISH TO ADD DATA IN THE TREE(Y/N)?y ENTER THE DATA:9 DO U WISH TO ADD DATA IN THE TREE(Y/N)?y ENTER THE DATA:5 DO U WISH TO ADD DATA IN THE TREE(Y/N)?y ENTER THE DATA:2 DO U WISH TO ADD DATA IN THE TREE(Y/N)?n Enter ur choice:5 BINARY SEARCH TREE IS 10 9 7 5 2 Enter ur choice:2 INSERT THE NODE IN THE TREE ENTER THE DATA:6 Enter ur choice:5 BINARY SEARCH TREE IS 10 9 7 6

Page 25: Data Structures Lab manual

25

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

5 2 Enter ur choice:3 DELETE THE NODE FROM THE TREE ENTER THE NODE FOR DELETING :7 Enter ur choice:5 BINARY SEARCH TREE IS 10 9 6 5 2 Enter ur choice:4 SEARCH THE NODE IN THE TREE ENTER THE SEARCHING ELEMENT:7 SEARCH ELEMENT 7 IS NOT FOUND Enter ur choice:4 SEARCH THE NODE IN THE TREE ENTER THE SEARCHING ELEMENT:6 SEARCH ELEMENT 6 IS FOUND Enter ur choice:6 END OF THE RUN OF UR PROGRAM RESULT: The above program has been executed successfully and the out put has been verified Viva Questions

1. Define a queue. 2 . Define a Circular queue. 3. What are the applications of queue? 4. Define a Priority queue. 5. Define a deque. 6. What are the 2variatins of a deque? 7. What is meant by input restricted deque?

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

8. What is meant by output restricted deque? 9. Define a tree. 10. What is a root in a tree? 11. What is a node in a tree? 12. What is meant by level in a tree? 13. What is leaf in a tree?

EX: NO: 7 AVL TREE

AIM: To implement AVL Rotations

ALGORITHM

Step Get the element to be inserted Step Pass the element to be inserted to the add procedure which in turn invokes insert procedure and places the element in correct position by mataining the height factor Step Continue step-1 till the user request other wise exit from the process

PROGRAM

#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<alloc.h> typedef enum { FALSE ,TRUE } bool; struct node { int info; int balance; struct node *lchild; struct node *rchild; }; struct node *insert (int , struct node *, int *); struct node* search(struct node *,int);

Page 26: Data Structures Lab manual

26

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

inorder(struct node *); display(struct node *,int n); main() { int ht_inc; int info ; int choice; struct node *root = (struct node *)malloc(sizeof(struct node)); root = NULL; while(1) { printf("1.Insert\n"); printf("2.Display\n"); printf("3.Quit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("Enter the value to be inserted : "); scanf("%d", &info); if( search(root,info) == NULL ) root = insert(info, root, &ht_inc); else printf("Duplicate value ignored\n"); break; case 2: if(root==NULL) { printf("Tree is empty\n"); continue; } printf("Tree is :\n"); display(root, 1); printf("\n\n"); printf("Inorder Traversal is: "); inorder(root);

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

printf("\n"); break; case 3: exit(1); default: printf("Wrong choice\n"); } } } struct node* search(struct node *ptr,int info) { if(ptr!=NULL) if(info < ptr->info) ptr=search(ptr->lchild,info); else if( info > ptr->info) ptr=search(ptr->rchild,info); return(ptr); }/*End of search()*/ struct node *insert (int info, struct node *pptr, int *ht_inc) { struct node *aptr; struct node *bptr; if(pptr==NULL) { pptr = (struct node *) malloc(sizeof(struct node)); pptr->info = info; pptr->lchild = NULL; pptr->rchild = NULL; pptr->balance = 0; *ht_inc = TRUE; return (pptr); } if(info < pptr->info) { pptr->lchild = insert(info, pptr->lchild, ht_inc); if(*ht_inc==TRUE) { switch(pptr->balance)

Page 27: Data Structures Lab manual

27

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

{ case -1: /* Right heavy */ pptr->balance = 0; *ht_inc = FALSE; break; case 0: /* Balanced */ pptr->balance = 1; break; case 1: /* Left heavy */ aptr = pptr->lchild; if(aptr->balance == 1) { printf("Left to Left Rotation\n"); pptr->lchild= aptr->rchild; aptr->rchild = pptr; pptr->balance = 0; aptr->balance=0; pptr = aptr; } else { printf("Left to right rotation\n"); bptr = aptr->rchild; aptr->rchild = bptr->lchild; bptr->lchild = aptr; pptr->lchild = bptr->rchild; bptr->rchild = pptr; if(bptr->balance == 1 ) pptr->balance = -1; else pptr->balance = 0; if(bptr->balance == -1) aptr->balance = 1; else aptr->balance = 0; bptr->balance=0; pptr=bptr; }

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

*ht_inc = FALSE; }/*End of switch */ }/*End of if */ }/*End of if*/ if(info > pptr->info) { pptr->rchild = insert(info, pptr->rchild, ht_inc); if(*ht_inc==TRUE) { switch(pptr->balance) { case 1: /* Left heavy */ pptr->balance = 0; *ht_inc = FALSE; break; case 0: /* Balanced */ pptr->balance = -1; break; case -1: /* Right heavy */ aptr = pptr->rchild; if(aptr->balance == -1) { printf("Right to Right Rotation\n"); pptr->rchild= aptr->lchild; aptr->lchild = pptr; pptr->balance = 0; aptr->balance=0; pptr = aptr; } else { printf("Right to Left Rotation\n"); bptr = aptr->lchild; aptr->lchild = bptr->rchild; bptr->rchild = aptr; pptr->rchild = bptr->lchild; bptr->lchild = pptr;

Page 28: Data Structures Lab manual

28

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

if(bptr->balance == -1) pptr->balance = 1; else pptr->balance = 0; if(bptr->balance == 1) aptr->balance = -1; else aptr->balance = 0; bptr->balance=0; pptr = bptr; }/*End of else*/ *ht_inc = FALSE; }/*End of switch */ }/*End of if*/ }/*End of if*/ return(pptr); }/*End of insert()*/ display(struct node *ptr,int level) { int i; if ( ptr!=NULL ) { display(ptr->rchild, level+1); printf("\n"); for (i = 0; i < level; i++) printf(" "); printf("%d", ptr->info); display(ptr->lchild, level+1); } return(0); } inorder(struct node *ptr) { if(ptr!=NULL) { inorder(ptr->lchild); printf("%d ",ptr->info);

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

inorder(ptr->rchild); } return(0); } OUT PUT

1.Insert 2.Display 3.Quit Enter your choice : 1 Enter the value to be inserted : 10 Right to Right Rotation 1.Insert 2.Display 3.Quit Enter your choice : 1 Enter the value to be inserted : 12 Right to Right Rotation 1.Insert 2.Display 3.Quit Enter your choice : 1 Enter the value to be inserted : 14 Right to Right Rotation 1.Insert 2.Display 3.Quit Enter your choice : 1 Enter the value to be inserted : 16 1.Insert 2.Display 3.Quit Enter your choice : 1 Enter the value to be inserted : 18 Right to Right Rotation

Page 29: Data Structures Lab manual

29

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

1.Insert 2.Display 3.Quit Enter your choice : 2 Tree is :

18 16 14 12 10 8 6 4 2 Inorder Traversal is: 2 4 6 8 10 12 14 16 18 1.Insert 2.Display 3.Quit Enter your choice : RESULT: Thus the program for implementing AVL rotation’s is executed. Viva Questions

1. What is meant by interior node? 2. What is meant by sibling? 3. What is meant by depth of a tree? 4. Define a Binary tree. 5. Define a Full Binary tree. 6. Define a complete Binary tree 7. Define a Binary tree search. 8. What are the types of tree traversal? 9. What is inorder traversal?

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

10. What is postorder traversal? 11. Define AVL tree.

EX: NO: 8 QUEUE USING BINARY HEAPING

AIM: To implement heap

ALGORITHM

Step Get the choice which ADT to perform i) If insert get the element to be inserted and pass it to insert function ii) If delete call delete function iii) If search get the element to be searched and pass it so search function Iv) if display call the display function Step Continue step-1 till the user request Step Exit from the process if the user don’t want to continue step-3

PRIORITY QUEUE

#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<alloc.h> insert(); del(); display(); struct node { int priority; int info;

Page 30: Data Structures Lab manual

30

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

struct node *link; }*front = NULL; 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; default : printf("Wrong choice\n"); }/*End of switch*/ }/*End of while*/ }/*End of main()*/ insert() { struct node *tmp,*q; int added_item,item_priority; tmp = (struct node *)malloc(sizeof(struct node)); printf("Input the item value to be added in the queue : "); scanf("%d",&added_item); printf("Enter its priority : ");

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

scanf("%d",&item_priority); tmp->info = added_item; tmp->priority = item_priority; /*Queue is empty or item to be added has priority more than first item*/ if( front == NULL || item_priority < front->priority ) { tmp->link = front; front = tmp; } else { q = front; while( q->link != NULL && q->link->priority <= item_priority ) q=q->link; tmp->link = q->link; q->link = tmp; } return(0); } del() { struct node *tmp; if(front == NULL) printf("Queue Underflow\n"); else { tmp = front; printf("Deleted item is %d\n",tmp->info); front = front->link; free(tmp); } return(0); } display() { struct node *ptr; ptr = front;

Page 31: Data Structures Lab manual

31

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

if(front == NULL) printf("Queue is empty\n"); else { printf("Queue is :\n"); printf("Priority Item\n"); while(ptr != NULL) { printf("%5d %5d\n",ptr->priority,ptr->info); ptr = ptr->link; } } return(0); } OUT PUT

4.Quit Enter your choice : 1 Input the item value to be added in the queue : 4 Enter its priority : 3 1.Insert 2.Delete 3.Display 4.Quit Enter your choice : 1 Input the item value to be added in the queue : 5 Enter its priority : 2 1.Insert 2.Delete 3.Display 4.Quit Enter your choice : 1 Input the item value to be added in the queue : 6 Enter its priority : 4 1.Insert 2.Delete 3.Display

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

4.Quit Enter your choice : 3 Queue is : Priority Item 1 2 2 5 3 4 4 6 1.Insert 2.Delete 3.Display 4.Quit Enter your choice : 2 Deleted item is 2 1.Insert 2.Delete 3.Display 4.Quit Enter your choice : 3 Queue is : Priority Item 2 5 3 4 4 6 1.Insert 2.Delete 3.Display 4.Quit Enter your choice : RESULT: Thus the program for implementing heap (priority queue) ADT is executed successfully Viva Questions

1. Define heap. 2. What is meant by an expression tree? 3. What do you mean by general tree?

Page 32: Data Structures Lab manual

32

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

4. How will you convert general tree into a binary

tree? 5. Define a graph. 6. What is meant by a directed graph? 7. What is meant by an undirected graph? 8. What is meant by a mixed graph? 9. What is meant by a weighted graph?

EX: NO: 9 HASHING

AIM: To implement hashing using Open Addressing

ALGORITHM:

Step 1:Get the Hash Size Step 2:Get the element to placed inside the Hash Table perform open

hashing place the element in the particular position chain Strep 3: If the element is already in particular Index ,find next Empty Space Step 4: If index is HashSize-1 then Index becomes 0. Step 5:Continue step-2 till the user request other wise exit from the process PROGRAM:

#include <stdio.h> #include <conio.h> #define HSIZE 7 void main() { int key[10],H[HSIZE]={0,0,0,0,0,0,0},hash[10],hash1,i,j,n; clrscr(); printf("Enter no of elements:\n"); scanf("%d",&n); printf("Enter Key values:\n");

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

for(j=0;j<n;j++) { scanf("%d",&key[j]); } printf("output:\n"); printf("Index\t\tHashtablevalue:\n"); printf("------------------------:\n"); for(j=0;j<n;j++) { hash[j]=key[j]%HSIZE; hash1=hash[j]; printf("Index=%d\n",hash1); if(H[hash1]==0) { H[hash1]=key[j]; } else { if(hash1==HSIZE-1) hash1=0; for(i=hash1;i<HSIZE;i++) { if(H[i]==0) { H[i]=key[j]; break; } } } } for(i=0;i<HSIZE;i++) { printf("%d\t\t%d\n",i,H[i]); } getch();

Page 33: Data Structures Lab manual

33

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

} OUTPUT:

Enter no of elements: 5 Enter Key values: 18 72 65 34 13 output: Index Hashtablevalue: ------------------------: Index=4 Index=2 Index=2 Index=6 Index=6 0 13 1 0 2 72 3 65 4 18 5 0 6 34 RESULT: Thus the program for implementing hashing is executed successfully. Viva Questions

1. Define a path.

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

2. Define a cycle. 3. What is meant by a acyclic graph? 4. What is meant by a connected graph? 5. What is meant by a strong connected graph? 6. What is meant by a weakly connected graph? 7. Define adjacency – list. 8. Define a spanning tree. 9. What are the applications of graphs? 10. What is meant by searching? 11. What is meant by linear searching? 12. Types of Searching. 13. What is meant by binary search? Examples. 14. What are the Hash Functions?

EX . NO: 10 PRIMS ALGORITHM

AIM: To implement prim’s algorithm

ALGORITHM:

Step 1) Get the no of vertex in the graph Step 2)Get the edge details from the user i.e from which source to which destination edge is present Step 3)Get which algorithm to perform i)if prims call prims algorithm display the result exit from the process Step 4:continue to step-1 till the user request Step 5:Exit from the process PROGRAM

#include<conio.h> #include<stdio.h> #define MAX 10 #define TEMP 0

Page 34: Data Structures Lab manual

34

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

#define PERM 1 #define FALSE 0 #define TRUE 1 #define infinity 9999 struct node { int predecessor; int dist; /*Distance from predecessor */ int status; }; struct edge { int u; int v; }; int adj[MAX][MAX]; int n; create_graph(); display(); maketree(struct edge tree[MAX] , int *); all_perm(struct node state[MAX]); main() { int i; //int path[MAX]; int wt_tree,count; struct edge tree[MAX]; create_graph(); printf("Adjacency matrix is :\n"); display(); count = maketree(tree,&wt_tree); printf("Weight of spanning tree is : %d\n", wt_tree); printf("Edges to be included in spanning tree are : \n"); for(i=1;i<=count;i++) { printf("%d->",tree[i].u); printf("%d\n",tree[i].v);

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

} return(0); } create_graph() { int i,max_edges,origin,destin,wt; printf("Enter number of vertices : "); scanf("%d",&n); max_edges=n*(n-1)/2; for(i=1;i<=max_edges;i++) { printf("Enter edge %d(0 0 to quit) : ",i); scanf("%d %d",&origin,&destin); if((origin==0) && (destin==0)) break; printf("Enter weight for this edge : "); scanf("%d",&wt); if( origin > n || destin > n || origin<=0 || destin<=0) { printf("Invalid edge!\n"); i--; } else { adj[origin][destin]=wt; adj[destin][origin]=wt; } }/*End of for*/ if(i<n-1) { printf("Spanning tree is not possible\n"); } return(0); } display() { int i,j; for(i=1;i<=n;i++)

Page 35: Data Structures Lab manual

35

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

{ for(j=1;j<=n;j++) printf("%3d",adj[i][j]); printf("\n"); } return(0); } int maketree(struct edge tree[MAX],int *weight) { struct node state[MAX]; int i,min,count,current; //int m; int u1,v1; *weight=0; /*Make all nodes temporary*/ for(i=1;i<=n;i++) { state[i].predecessor=0; state[i].dist = infinity; state[i].status = TEMP; } /*Make first node permanent*/ state[1].predecessor=0; state[1].dist = 0; state[1].status = PERM; /*Start from first node*/ current=1; count=0; /*count represents number of nodes in tree */ while( all_perm(state) != TRUE ) /*Loop till all the nodes become PERM*/ { for(i=1;i<=n;i++) { if ( adj[current][i] > 0 && state[i].status == TEMP ) { if( adj[current][i] < state[i].dist ) { state[i].predecessor = current;

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

state[i].dist = adj[current][i]; } } }/*End of for*/ /*Search for temporary node with minimum distance and make it current node*/ min=infinity; for(i=1;i<=n;i++) { if(state[i].status == TEMP && state[i].dist < min) { min = state[i].dist; current=i; } }/*End of for*/ state[current].status=PERM; /*Insert this edge(u1,v1) into the tree */ u1=state[current].predecessor; v1=current; count++; tree[count].u=u1; tree[count].v=v1; /*Add wt on this edge to weight of tree */ *weight=*weight+adj[u1][v1]; }/*End of while*/ return (count); }/*End of maketree()*/ /*This function returns TRUE if all nodes are permanent*/ int all_perm(struct node state[MAX] ) { int i; for(i=1;i<=n;i++) if( state[i].status == TEMP ) return FALSE; return TRUE; }/*End of all_perm()*/

Page 36: Data Structures Lab manual

36

M.A.M. SCHOOL OF ENGINEERING, SIRUGANUR, TIRUCHIRAPPALLI – 621 105.

Department of Computer Science and Engineering

LABORATORY MANUAL – CS 2208 – DATA STRUCTURES LABORATORY

OUT PUT Enter number of vertices: 3 Enter edge 1(0,0 to quit): 1 2 Enter weight for this edge:1 Enter edge 2(0,0 to quit): 2 3 Enter weight for this edge: 4 Enter edge 3(0,0 to quit): 3 1 Enter weight for this edge: 2 Adjacency matrix:

0 1 2 1 0 4 2 4 0

Weight of the spanning tree:4 Edge to be included in spanning tree are:

RESULT: Thus the program for implementing prim’s algorithm is executed successfully