LORDS INSTITUTE OF ENGINEERING & TECHNOLOGY (Autonomous) Department of CSE/IT/CSM/CSD/AIML Lab Manual Course Name : DATA STRUCTURES Course Code : U21CS302 Class : BE - II SEMESTER - III Branch : Department of CSE/IT/CSM/CSD/AIML Year : 2022 - 23 Course Coordinator : Mr. Shaik Imam, Associate Professor, Department of CSE Course Faculty : Mrs.B Nagalakshmi, Assistant Professor, Department of IT OBJECTIVES To meet the challenge of ensuring excellence in engineering education, the issue of quality needs to be addressed, debated and taken forward in a systematic manner. Accreditation is the principal means of quality assurance in higher education. The major emphasis of accreditation process is to measure the outcomes of the program that is being accredited. In line with this, Faculty of Institute of Aeronautical Engineering, Hyderabad has taken a lead in incorporating philosophy of outcome-based education in the process of problem solving and career development. So, all students of the institute should understand the depth and approach of course to be taught through this question bank, which will enhance learner’s learning process. Course Code Course Title Core U21CS3L1 DATA STRUCTURES LAB Core Prerequisite Hours Per Week CIE SEE Credits L T D P PPS Lab - - - 3 25 50 1.5 Course Objectives: Develop ability to 1. Understand essential concepts of simple linear and nonlinear data structures. 2. Analyze and implement programming skills to implement sorting and searching algorithms 3. Apply the suitable data structures for the given real world problems. 4. Acquire knowledge in practical applications of data structures. 5. Provide solutions for various graphical concepts. Course Outcomes: At the end of the course, student would be able to 1. Write programs in various data structures using arrays and linked lists. 2. Develop ADT necessary for solving problems based on Stacks and Queues 3. Evaluate binary trees, general tree structures, advanced search trees, heaps, graphs. 4. Apply hash functions and handle collisions 5. Implement various kinds of sorting techniques and apply appropriate techniques forsolving a given problem List of Experiments: 1. Implementation of Stacks and Queues using Arrays. 2. Solving Towers of Hanoi problem 3. Implementation of Circular Queue. 4. Solving tic-tac -toe problem 5. Implementation of Infix to Postfix Conversion, Postfix ExpressionEvaluation. 6. Implementation of Singly Linked List 7. Implementation of Doubly Linked List. 8. Implementation of Circular Linked List. 9. Implementation of Stacks, Queues using Linked Lists. 10. Implementation of Binary Search and Hashing 11. Implementation of Operations on Binary Tree (Delete Entire Tree, Copy Entire Tree, Mirror Image, Level Order, Search for a Node etc.) 12. Implementation of Tree Traversals on Binary Trees. 13. Implementation of Binary Search Tree. (Insertion, Deletion and Search operations) 14. Implementation of operations on AVL Trees. 15. Implementation of Traversal on Graphs. 16. Implementation of Selection, Merge, Quick and InsertionSort. 17. Implementation of Prims and Kruskals Algorithm. Suggested Readings: 1.S. Lipschutz, “Data Structures”, Tata McGraw Hill Education, 1st Edition, 2008. 2.D. Samanta, “Classic Data Structures”, PHI Learning, 2nd Edition, 2004. 3. Mark A Weiss, Data Structures and Algorithm Analysis In C, Second Edition (2002), Pearson. 1) a ) Implementation of Stack Using Array #include <stdio.h> int stack[100],i,j,choice=0,n,top=-1; void push(); void pop(); void show(); void main () { printf("Enter the number of elements in the stack "); scanf("%d",&n); printf("*********Stack operations using array*********"); printf("\n \n"); while(choice != 4) { printf("Chose one from the below options...\n"); printf("\n1.Push\n2.Pop\n3.Show\n4.Exit"); printf("\n Enter your choice \n"); scanf("%d",&choice); switch(choice) { case 1: { push(); break; } case 2: { pop(); break; } case 3: { show(); break; } case 4: { printf("Exiting ... "); break; } default: { printf("Please Enter valid choice "); } }; } } void push () { int val; if (top == n ) printf("\n Overflow"); else { printf("Enter the value?"); scanf("%d",&val); top = top +1; stack[top] = val; } } void pop () { if(top == -1) printf("Underflow"); else top = top -1; } void show() { for (i=top;i>=0;i--) { printf("%d\n",stack[i]); } if(top == -1) { printf("Stack is empty"); } } b) Implementation of Queue Using Array: #include<stdio.h> #include<stdlib.h> #define maxsize 5 void insert(); void delete(); void display(); int front = -1, rear = -1; int queue[maxsize]; void main () { int choice; while(choice != 4) { printf("\n*************************Main Menu*****************************\n"); printf("\n=================================================================\n"); printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n"); printf("\nEnter your choice ?"); scanf("%d",&choice); switch(choice) { case 1: insert(); break; case 2: delete(); break; case 3: display(); break; case 4: exit(0); break; default: printf("\nEnter valid choice??\n"); } } } void insert() { int item; printf("\nEnter the element\n"); scanf("\n%d",&item); if(rear == maxsize-1) { printf("\nOVERFLOW\n"); return; } if(front == -1 && rear == -1) { front = 0; rear = 0; } else { rear = rear+1; } queue[rear] = item; printf("\nValue inserted "); } void delete() { int item; if (front == -1 || front > rear) { printf("\nUNDERFLOW\n"); return; } else { item = queue[front]; if(front == rear) { front = -1; rear = -1 ; } else { front = front + 1; } printf("\nvalue deleted "); } } void display() { int i; if(rear == -1) { printf("\nEmpty queue\n"); } else { printf("\nprinting values ..... \n"); for(i=front;i<=rear;i++) { printf("\n%d\n",queue[i]); } } } OutPut: [ nagalakshmi@localhost DSA]$ gedit Q_With_Arry.c [nagalakshmi@localhost DSA]$ cc Q_With_Arry.c [nagalakshmi@localhost DSA]$ ./a.out *************************Main Menu***************************** ================================================================= 1.insert an element 2.Delete an element 3.Display the queue 4.Exit Enter your choice ?1 Enter the element 10 Value inserted *************************Main Menu***************************** ================================================================= 1.insert an element 2.Delete an element 3.Display the queue 4.Exit Enter your choice ?1 2) Solving Towers of Hanoi Problem using C #include <stdio.h> void towers(int, char, char, char); int main() { int num; printf("Enter the number of disks : "); scanf("%d", &num); printf("The sequence of moves involved in the Tower of Hanoi are :\n"); towers(num, 'A', 'C', 'B'); return 0; } void towers(int num, char frompeg, char topeg, char auxpeg) { // Base Condition if no of disks are if (num == 1) { printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg); return; } // Recursively calling function twice towers(num - 1, frompeg, auxpeg, topeg); printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg); towers(num - 1, auxpeg, topeg, frompeg); } OutPut: [nagalakshmi@localhost DSA]$ gedit Towewr_Hanoi.c [nagalakshmi@localhost DSA]$ cc Towewr_Hanoi.c [nagalakshmi@localhost DSA]$ ./a.out Enter the number of disks : 3 The sequence of moves involved in the Tower of Hanoi are : Move disk 1 from peg A to peg C Move disk 2 from peg A to peg B Move disk 1 from peg C to peg B Move disk 3 from peg A to peg C Move disk 1 from peg B to peg A Move disk 2 from peg B to peg C Move disk 1 from peg A to peg C 3. Implementation of Circular Queue Using C #include <stdio.h> # define max 6 int queue[max]; // array declaration int front=-1; int rear=-1; // function to insert an element in a circular queue void enqueue(int element) { if(front==-1 && rear==-1) // condition to check queue is empty { front=0; rear=0; queue[rear]=element; } else if((rear+1)%max==front) // condition to check queue is full { printf("Queue is overflow.."); } else { rear=(rear+1)%max; // rear is incremented queue[rear]=element; // assigning a value to the queue at the rear position. } } // function to delete the element from the queue int dequeue() { if((front==-1) && (rear==-1)) // condition to check queue is empty { printf("\nQueue is underflow.."); } else if(front==rear) { printf("\nThe dequeued element is %d", queue[front]); front=-1; rear=-1; } else { printf("\nThe dequeued element is %d", queue[front]); front=(front+1)%max; } } // function to display the elements of a queue void display() { int i=front; if(front==-1 && rear==-1) { printf("\n Queue is empty.."); } else { printf("\nElements in a Queue are :"); while(i<=rear) { printf("%d,", queue[i]); i=(i+1)%max; } } } int main() { int choice=1,x; // variables declaration while(choice<4 && choice!=0) // while loop { printf("\n Press 1: Insert an element"); printf("\nPress 2: Delete an element"); printf("\nPress 3: Display the element"); printf("\nEnter your choice"); scanf("%d", &choice); switch(choice) { case 1: printf("Enter the element which is to be inserted"); scanf("%d", &x); enqueue(x); break; case 2: dequeue(); break; case 3: display(); }} return 0; } Output: [nagalakshmi@localhost DSA]$ gedit Circular_queue_Arry.c [nagalakshmi@localhost DSA]$ cc Circular_queue_Arry.c [nagalakshmi@localhost DSA]$ ./a.out Press 1: Insert an element Press 2: Delete an element Press 3: Display the element Enter your choice1 Enter the element which is to be inserted100 Press 1: Insert an element Press 2: Delete an element Press 3: Display the element Enter your choice1 Enter the element which is to be inserted200 Press 1: Insert an element Press 2: Delete an element Press 3: Display the element Enter your choice3 Elements in a Queue are :100,200, Press 1: Insert an element Press 2: Delete an element Press 3: Display the element Enter your choice2 The dequeued element is 100 Press 1: Insert an element Press 2: Delete an element Press 3: Display the element Enter your choice3 Elements in a Queue are :200, Press 1: Insert an element Press 2: Delete an element Press 3: Display the element Enter your choice 4. Implementation of tic-tac-toe Problem Using C #include <stdio.h> char square[10] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; int checkwin(); void board(); int main() { int player = 1, i, choice; char mark; do { board(); player = (player % 2) ? 1 : 2; printf("Player %d, enter a number: ", player); scanf("%d", &choice); mark = (player == 1) ? 'X' : 'O'; if (choice == 1 && square[1] == '1') square[1] = mark; square[2] = mark; square[3] = mark; square[4] = mark; square[5] = mark; square[6] = mark; square[7] = mark; square[8] = mark; square[9] = mark; else { printf("Invalid move "); player--; //getch(); } i = checkwin(); player++; }while (i == - 1); board(); if (i == 1) printf("==>\aPlayer %d win ", --player); else printf("==>\aGame draw"); /*getch();*/ return 0; } /********************************************* FUNCTION TO RETURN GAME STATUS 1 FOR GAME IS OVER WITH RESULT -1 FOR GAME IS IN PROGRESS O GAME IS OVER AND NO RESULT **********************************************/ int checkwin() { if (square[1] == square[2] && square[2] == square[3]) return 1; else if (square[4] == square[5] && square[5] == square[6]) return 1; else if (square[7] == square[8] && square[8] == square[9]) return 1; else if (square[1] == square[4] && square[4] == square[7]) return 1; else if (square[2] == square[5] && square[5] == square[8])return 1; else if (square[3] == square[6] && square[6] == square[9]) return 1; else if (square[1] == square[5] && square[5] == square[9]) return 1; else if (square[3] == square[5] && square[5] == square[7]) return 1; else if (square[1] != '1'&& square[2] != '2'&& square[3] != '3'&& square[4] != '4'&& square[5] != '5'&& square[6] != '6'&& square[7] != '7'&& square[8] != '8'&& square[9] != '9') return 0; else return - 1; } /******************************************************************* FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK ********************************************************************/ void board() { //system("cls"); printf("\n\n\tTic Tac Toe\n\n"); printf("Player 1 (X) - Player 2 (O)\n\n\n"); printf(" | | \n"); printf(" %c | %c | %c \n", square[1], square[2], square[3]); printf(" | | \n"); printf(" | | \n"); printf(" %c | %c | %c \n", square[4], square[5], square[6]); printf(" | | \n"); printf(" | | \n"); printf(" %c | %c | %c \n", square[7], square[8], square[9]); printf(" | | \n\n"); } OutPut : [nagalakshmi@localhost DSA]$ cc TicTacToe.c [nagalakshmi@localhost DSA]$ ./a.out 5. Implementation of Infix to Postfix Conversion. Postfix Expression Evaluation. #define SIZE 50 /* Size of Stack */ #include <ctype.h> #include <stdio.h> char s[SIZE]; int top = -1; /* Global declarations */ /* Function to remove spaces from given string */ void RemoveSpaces(char* source) { char* i = source; char* j = source; while(*j != 0) { *i = *j++; if(*i != '') i++; } *i = 0; } /* Function for PUSH operation */ void push(char elem) { s[++top] = elem; } /* Function for POP operation */ char pop() { return (s[top--]); } /* Function for precedence */ int pr(char elem) { switch (elem) { case '#': return 0; case '(': return 1; case '+': case '-': return 2; case '*': case '/': return 3; } } /* * Function to convert from infix to postfix expression */ void infix_to_postfix(char *infix, char *postfix) { char ch, elem; int i = 0, k = 0; RemoveSpaces(infix); push('#'); while ((ch = infix[i++]) != '\n') { if (ch == '(') push(ch); else if (isalnum(ch)) postfix[k++] = ch; else if (ch == ')') { while (s[top] != '(') postfix[k++] = pop(); elem = pop(); /* Remove ( */ } else { /* Operator */ while (pr(s[top]) >= pr(ch)) postfix[k++] = pop(); push(ch); } } while (s[top] != '#') /* Pop from stack till empty */ postfix[k++] = pop(); postfix[k] = 0; /* Make postfix as valid string */ } /* * Function to evaluate a postfix expression*/ int eval_postfix(char *postfix) { char ch; int i = 0, op1, op2; while((ch = postfix[i++]) != 0) { if(isdigit(ch)) push(ch-'0'); /* Push the operand */ else { /* Operator,pop two operands */ op2 = pop(); op1 = pop(); switch(ch) { case '+' : push(op1+op2); break; case '-' : push(op1-op2); break; case '*' : push(op1*op2); break; case '/' : push(op1/op2); break; } } } return s[top]; } void main() { /* Main Program */char infx[50], pofx[50]; printf("\nInput the infix expression: "); fgets(infx, 50, stdin); infix_to_postfix(infx, pofx); printf("\nGiven Infix Expression: %sPostfix Expression: %s", infx, pofx); top = -1; printf("\nResult of evaluation of postfix expression : %d", eval_postfix(pofx)); } Output: [nagalakshmi@localhost DSA]$ cc Expresion_Evaluation.c [nagalakshmi@localhost DSA]$ ./a.out Input the infix expression: 2*3+4 Given Infix Expression: 2*3+4 Postfix Expression: 23*4+ Result of evaluation of postfix expression : 10 6. Implementation of Singly Linked List Using C. #include<stdio.h>