Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

VISVESVARAYA TECHNOLOGICAL UNIVERSITY,

BELGAUM

Data Structures Laboratory


(17CSL38)

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING,

ACHARYA INSTITUTE OF TECHNOLOGY,


BANGALORE
2018-2019
Data Structures Laboratory - 17CSL38
No of Lecture hrs/week - 01I+02
Exam Marks - 60
IA Marks - 40 P Total No of Lecture Hrs - 40
Exam Hours - 03
Credits - 2

Course Objective -This laboratory course enable students to get practical experience in design, develop, implement
and analysis of
1. Linear data structures and their applications such as Arrays, Stacks, Queues and Lists.
2. Non-Linear Data Structures and their Applications such as Trees and Graphs.

1. Design, Develop and Implement a menu driven Program in C for the following Array operations
a) Creating an Array of N Integer Elements
b) Display of Array Elements with Suitable Headings
c) Inserting an Element (ELEM) at a given valid Position (POS)
d) Deleting an Element at a given valid Position(POS)
e) Exit.
Support the program with functions for each of the above operations.

2. Design, Develop and Implement a Program in C for the following operationson Strings
a) Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b) Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in 

STR with REP if PAT exists in STR. Report suitable messages in case PAT does 

not exist in STR
Support the program with functions for each of the above operations. Don't use Built-in functions.

3. Design, Develop and Implement a menu driven Program in C for the following operations on STACK of Integers
(Array Implementation of Stack with maximum size MAX)
a) Push an Element on to Stack
b) Pop an Element from Stack
c) Demonstrate how Stack can be used to check Palindrome
d) Demonstrate Overflow and Underflow situations on Stack
e) Display the status of Stack
f) Exit
Support the program with appropriate functions for each of the above operations

4. Design, Develop and Implement a Program in C for converting an Infix Expression to Postfix Expression.
Program should support for both parenthesized and free parenthesized expressions with the operators: +, -, *, /,
%(Remainder), ^(Power) and alphanumeric operands. 


5. Design, Develop and Implement a Program in C for the following Stack Applications
a) Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %, ^
b) Solving Tower of Hanoi problem with n disks 


6. Design, Develop and Implement a menu driven Program in C for the following operations on Circular QUEUE
of Characters (Array Implementation of Queue with maximum size MAX)
a) Insert an Element on to Circular QUEUE
b) Delete an Element from Circular QUEUE
c) Demonstrate Overflow and Underflow situations on Circular QUEUE
d) Display the status of Circular QUEUE
e) Exit
Support the program with appropriate functions for each of the above operations

7. Design, Develop and Implement a menu driven Program in C for the following operations on Singly Linked List
(SLL) of Student Data with the fields: USN, Name, Branch, Sem, PhNo
f) Create a SLL of N Students Data by using front insertion.
g) Display the status of SLL and count the number of nodes in it
h) Perform Insertion / Deletion at End of SLL
i) Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
j) Exit
8. Design, Develop and Implement a menu driven Program in C for the following operations on Doubly Linked
List (DLL) of Employee Data with the fields: SSN, Name, Dept, Designation, Sal, PhNo
a) Create a DLL of N Employees Data by using end insertion.
b) Display the status of DLL and count the number of nodes in it
c) Perform Insertion and Deletion at End of DLL
d) Perform Insertion and Deletion at Front of DLL Demonstrate how this DLL can be used as Double
Ended Queue
e) Exit

9. Design, Develop and Implement a Program in C for the following operations on Singly Circular Linked List
(SCLL) with header nodes
f) Represent and Evaluate a Polynomial P(x,y,z) = 6x2 y2 z3-4yz +3x yz+2xy z-2xyz
g) Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the result in
POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations


10. Design, Develop and Implement a menu driven Program in C for the following operations on Binary Search Tree
(BST) of Integers
h) Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
i) Traverse the BST in Inorder, Preorder and Post Order
j) Search the BST for a given element (KEY) and report the appropriate message
k) Exit

11. Design, Develop and Implement a Program in C for the following operations on Graph(G) of Cities
l) Create a Graph of N cities using Adjacency Matrix.
m) Print all the nodes reachable from a given starting node in a digraph using DFS/BFS method

12. Given a File of N employee records with a set K of Keys(4-digit) which uniquely determine the records in file F.
Assume that file F is maintained in memory by a Hash Table(HT) of m memory locations with L as the set of
memory addresses (2-digit) of locations in HT. Let the keys in K and addresses in L are Integers. Design and
develop a Program in C that uses Hash function H: K →L as H(K)=K mod m (remainder method), and
implement hashing technique to map a given key K to the address space L. Resolve the collision (if any) using
linear probing.


Course Outcomes: After studying this course, students will be able to:
1. Describe various linear and non-linear data structures.
2. Choose the appropriate data structure for solving real world problems.
3. Develop and analyze various types of data structures and their applications.
Data Structures Laboratory (17CSL38) 2018-19

1. Design, Develop and Implement a menu driven Program in C for the following Array
operations
a. Creating an Array of N Integer Elements
b. Display of Array Elements with Suitable Headings
c. Inserting an Element (ELEM) at a given valid Position (POS)
d. Deleting an Element at a given valid Position(POS)
e. Exit.
Support the program with functions for each of the above operations.
#include<stdio.h>
#include<stdlib.h>
int a[20], N, ELEM, POS, i;
void create( );
void display( );
void insert( );
void delete_ele( );

IT
int main( )
{

a
int choice;
while(1)
{
ry
ha
printf("\n\n----A PROGRAM TO PERFORM ARRAY OPERATIONS----");
printf("\n1.CREATE\n");
Ac

printf("2.DISPLAY\n");
printf("3.INSERT\n");
printf("4.DELETE\n");
E,

printf("5.EXIT\n");
S&

printf("-----------------------");
printf("\nENTER YOUR CHOICE:\t");
scanf("%d", &choice);
fC

switch(choice)
{
.o

case 1: create( ); break;


case 2: display( ); break;
pt

case 3: insert( ); break;


case 4: delete_ele( ); break;
De

case 5: exit(0);
default: printf("\n Invalid choice:\n");
break;
}
}
return 0;
}
void create( )
{
printf("\nEnter the size of the array elements:\t");
scanf("%d", &N);
printf("\nEnter the elements for the array:\n");

Dept. of Computer Science & Engg, AcIT "1


Data Structures Laboratory (17CSL38) 2018-19
for(i=0;i<N;i++)
scanf("%d", &a[i]);
}
void display( )
{
if(N==0)
{
printf("\nArray is Empty-Nothing to Display\n");
return;
}
printf("\n The array elements are:\n");
for(i=0; i<N; i++)
printf("%d\t", a[i]);
}
void insert( )
{

IT
printf("\nEnter the position(between 1 and %d) to insert a new element:\t",N);
scanf("%d", &POS);

a
POS = POS-1;
if (POS>=0 && POS<=N)
{
ry
ha
printf("\nEnter the element to be inserted :\t");
scanf("%d", &ELEM);
Ac

for(i=N-1;i>=POS; i--)
a[i+1]=a[i];
E,

a[POS]=ELEM;
N=N+1;
S&

}
}
void delete_ele( )
fC

{
if(N==0)
.o

{
printf("\nArray Empty - Deletion Not Possible\n");
pt

return;
}
De

printf("\nEnter the position (between 1 and %d) of the element to be deleted:\t",N);


scanf("%d", &POS);
POS = POS-1;
if(POS>=0 && POS<N)
{
ELEM=a[POS];
for(i=POS; i<N-1;i++)
a[i]=a[i+1];
printf("\nThe deleted element is =%d\n", ELEM);
N = N-1;
}
}
Dept. of Computer Science & Engg, AcIT "2
Data Structures Laboratory (17CSL38) 2018-19

Output:
Run 1:

aIT
ry
ha
Ac
E,

Run 2:
S&
fC
.o
pt
De

Dept. of Computer Science & Engg, AcIT "3


Data Structures Laboratory (17CSL38) 2018-19

2. Design, Develop and Implement a Program in C for the following operations on Strings
a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT
in STR with REP if PAT exists in STR.
c. Report suitable messages in case PAT does not exist in STR.
Support the program with functions for each of the above operations. Don't use Built-
in functions.

#include<stdio.h>
char STR[100],PAT[100],REP[100],ANS[100];
int temp, s, p, r, a, flag = 0;
void read( );
void replace( );
void print( );
int main( )

IT
{
read( );

a
replace( );
print( );
return 0;
ry
ha
}
void read( )
Ac

{
printf("Enter the MAIN string: \n");
E,

gets(STR);
printf("Enter a PATTERN string: \n");
S&

gets(PAT);
printf("Enter a REPLACE string: \n");
gets(REP);
fC

}
void replace ( )
.o

{
temp = s = p = a = 0;
pt

while ( STR[temp] != '\0')


{
De

// Checking for Match


if ( STR[s] == PAT[p] )
{
p++;
s++;
if ( PAT[p] == '\0')
{
//copy replace string in ans string
for(r = 0; REP[r] != '\0';r++, a++)
ANS[a] = REP[r];
flag=1;
p = 0;
Dept. of Computer Science & Engg, AcIT "4
Data Structures Laboratory (17CSL38) 2018-19
temp = s;
}
}
else //Mismatch
{
ANS[a] = STR[temp];
temp++;
a++;
s = temp;
p = 0;
}
}
}
void print( )
{
if(flag==0)

IT
printf("Pattern not found!!!\n”);
else

a
{
ANS[a] = '\0';
ry
printf("\nThe RESULTANT string is:\n%s\n" ,ANS);
ha
}
}
Ac

Output:
E,

Run 1:
S&
fC
.o

Run 2:
pt
De

Dept. of Computer Science & Engg, AcIT "5


Data Structures Laboratory (17CSL38) 2018-19

3. Design, Develop and Implement a menu driven Program in C for the following operations
on STACK of Integers (Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Support the program with appropriate functions for each of the above operation.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAX 5
int s[MAX],top=-1,item;

IT
int IsFull( )

a
{
if(top>=MAX-1)
return 1;
ry
ha
return 0;
}
Ac

int IsEmpty( )
{
E,

if(top==-1)
return 1;
S&

return 0;
}
void push(int item)
fC

{
top++;
.o

s[top]=item;
}
pt

void pop( )
De

{
item=s[top];
top--;
}

void display( )
{
int i;
printf("\n the elements of the stack are");
for(i=top;i>=0;i--)
printf("\n %d",s[i]);
}
Dept. of Computer Science & Engg, AcIT "6
Data Structures Laboratory (17CSL38) 2018-19

void check_pal( )
{
int num, temp, digit, revnum=0,k=0;
while(top!=-1)
pop( );
printf("\nenter the number\n");
scanf("%d",&num);
temp=num;
top=-1;
while(temp!=0)
{
digit=temp%10;
push(digit);
temp=temp/10;
}

IT
while(top!=-1)
{

a
pop( );
revnum=item*pow(10,k)+revnum;
k=k+1;
ry
ha
}
Ac

printf("\nReverse of %d is %d\n",num,revnum);
if(num == revnum)
E,

printf("The %d is Palindrome\n", num);


else
S&

printf("The %d is not a Palindrome\n", num);


}
fC

int main( )
{
.o

int ch;
do
pt

{
printf("\n1. Push \n2. Pop \n3. Display\n4. Check Palindrome\n5. Exit\n");
De

printf("\n Enter the choice: ");


scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter the element to be inserted:”);
scanf("%d",&item);
if(IsFull( ))
printf("Stack Overflow\n");
else
push(item);
break;
case 2: if(IsEmpty( ))
Dept. of Computer Science & Engg, AcIT "7
Data Structures Laboratory (17CSL38) 2018-19
printf("Stack Underflow\n");
else
{
pop( );
printf("The item deleted is %d\n",item);
}
break;
case 3: if(IsEmpty( ))
printf("Stack is Empty\n");
else
display( );
break;
case 4: check_pal( );
break;
case 5: exit(0);
default:printf("Invalid choice: \n");

IT
}
}while(ch!=5);

a
return 0;
}
ry
ha
Ac
E,
S&
fC
.o
pt
De

Dept. of Computer Science & Engg, AcIT "8


Data Structures Laboratory (17CSL38) 2018-19

Output:

aIT
ry
ha
Ac
E,
S&
fC
.o
pt
De

Dept. of Computer Science & Engg, AcIT "9


Data Structures Laboratory (17CSL38) 2018-19

4. Design, Develop and Implement a program in C for converting an Infix Expression to


Postfix Expression. The below program support for both parenthesized and free
parenthesized expressions with the operators: +, -, *, /, %(Remainder), ^(Power) and
alphanumeric operands.

#include <stdio.h>
#include<ctype.h>
#define SIZE 20
char s[SIZE];
int top = -1;
void push(char elem)
{
s[++top] = elem;
}
char pop( )

IT
{
return (s[top--]);

a
}
int precedence(char elem) /* Function for precedence */
{
ry
ha
switch (elem)
{
Ac

case '#': return 0;


case '(': return 1;
E,

case '+':
case '-': return 2;
S&

case '*':
case '/':
case '%': return 3;
fC

case '$':
case '^': return 4;
.o

}
return elem;
pt

}
De

int main( )
{
char infix[50], postfix[50], ch, elem;
int i = 0, k = 0;
printf("\n\nEnter the Infix Expression: ");
scanf("%s", infix);
push('#');
while ((ch = infix[i++]) != '\0')
{
if (ch == '(')
push(ch);
else if (isalnum(ch))
Dept. of Computer Science & Engg, AcIT "10
Data Structures Laboratory (17CSL38) 2018-19
postfix[k++] = ch;
else if (ch == ')')
{
while (s[top] != '(')
postfix[k++] = pop();
elem = pop(); /* Remove ( */
}
else /* Operator */
{
while (precedence(s[top]) >= precedence(ch))
postfix[k++] = pop();
push(ch);
}
}
while (s[top] != '#') /* Pop from stack till empty */
postfix[k++] = pop();

IT
postfix[k] = '\0'; /* Make pofx as valid string */
printf("\nGiven Infix Expression is: %s\n",infix);

a
printf("\nPostfix Expression is: %s\n", postfix);
}
ry
ha
Output:
Ac
E,
S&
fC
.o
pt
De

Dept. of Computer Science & Engg, AcIT "11


Data Structures Laboratory (17CSL38) 2018-19

5. Design, Develop and Implement a Program in C for the following Stack Applications
a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /,
%, ^
b. Solving Tower of Hanoi problem with n disks.

a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %, ^


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<process.h>
double compute(char symbol, double op1, double op2)
{
switch(symbol)
{

IT
case '+': return op1+op2; /* Perform addition */
case '-': return op1-op2; /* Perform subtraction */

a
case '*': return op1*op2; /* Perform multiplaction */
ry
case '/': return op1/op2; /* Perform division */
case '%': return op1%op2; /* Perform division and gives reminder */
ha
case '$':
case '^': return pow(op1,op2); /* Compute power */
Ac

}
}
int main( )
E,

{
S&

double s[20]; /* Place for stack elements */


double res; /* Holds partial or final result */
double op1; /* First operand */
fC

double op2; /* Second operand */


int top; /* Points to the topmost element */
.o

int i; /* Index value */


char postfix[20]; /* Input expression */
pt

char symbol; /* Scanned postfix symbol */


printf("Enter the postfix expression\n");
De

scanf("%s",postfix);
top=-1;
for(i=0;i<strlen(postfix);i++)
{
symbol=postfix[i]; /* Obtains the next character */
if(isdigit(symbol)) /* If character is a digit or not */
s[++top]=symbol-'0';
else
{
op2=s[top--]; /* Obtain second operand from stack */
op1=s[top--]; /* Obtain first operand from stack */
/* Perform specified operation */

Dept. of Computer Science & Engg, AcIT "12


Data Structures Laboratory (17CSL38) 2018-19
res=compute(symbol,op1,op2);
/* Push partial results on the stack */
s[++top]=res;
}
}
res=s[top--];
printf("the result is %f\n”,res);
return 0;
}
Output:

a IT
ry
ha
Ac
E,
S&
fC
.o
pt
De

Dept. of Computer Science & Engg, AcIT "13


Data Structures Laboratory (17CSL38) 2018-19

b) Solving Tower of Hanoi Problem with n disks


#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', 'B', ‘C');
return 0;
}
void towers(int n, char source, char temp, char dest)
{
if (n == 1)

IT
{
printf(" Move disk 1 from peg %c to peg %c\n”, source, dest);

a
return;
}
towers(n - 1, source, dest, temp);
ry
ha
printf(" Move disk %d from peg %c to peg %c\n”, n, source, dest);
towers(n - 1, temp, source, dest);
Ac

}
E,

Output:
S&
fC
.o
pt
De

Dept. of Computer Science & Engg, AcIT "14


Data Structures Laboratory (17CSL38) 2018-19

6. Design, Develop and Implement a menu driven Program in C for the following operations
on Circular QUEUE of Characters (Array Implementation of Queue with maximum size
MAX)
a) Insert an Element on to Circular QUEUE
b) Delete an Element from Circular QUEUE
c) Demonstrate Overflow and Underflow situations on Circular QUEUE
d) Display the status of Circular QUEUE
e) Exit
Support the program with appropriate functions for each of the above operations.

#include <stdio.h>
#include<stdlib.h>
#define QSIZE 4
int q[5], r=-1, f=0, count=0, item;
/* Insert Operation */

IT
void insert( )
{

a
/* Check for queue overflow */
if(count == QSIZE)
{
ry
ha
printf("Queue Overflow\n");
return;
Ac

}
r = (r+1) % QSIZE; /* Increment rear by 1 */
E,

q[r] = item; /* Insert into queue */


count++;
S&

/* Delete Operation */
fC

void del( )
{
.o

/* Check for Queue Underflow */


if(count == 0)
pt

{
printf("Queue Underflow\n");
De

return;
}
printf("The item deleted is: %d\n", q[f]);
f = (f+1) % QSIZE;
count--;
}

/* Display Operation */
void display(int front)
{
int i;
/* Check for Empty Queue */
Dept. of Computer Science & Engg, AcIT "15
Data Structures Laboratory (17CSL38) 2018-19
if(count == 0)
{
printf("Queue is Empty\n");
return;
}
/* Display the contents of the queue */
printf("Contents of the queue\n");
for(i=1; i<=count; i++)
{
printf("%d\n",q[front]);
front = (front+1) % QSIZE;
}
}

int main( )
{

IT
int choice;
do

a
{
printf("***********************\n");
printf("Circular Queue Operations\n");
ry
ha
printf("1. Insert\n");
printf("2. Delete\n");
Ac

printf("3. Display\n");
printf("4. Quit\n");
E,

printf("Enter your choice:\n");


scanf("%d",&choice);
S&

switch(choice)
{
fC

case 1: printf("Enter the item to be inserted\n");


scanf("%d",&item);
.o

insert( );
break;
pt

case 2: del( );
break;
De

case 3: display(f);
break;
case 4: exit(0);
default:printf("Invalid choice\n");
}
}while(choice!=4);
return 0;
}

Dept. of Computer Science & Engg, AcIT "16


Data Structures Laboratory (17CSL38) 2018-19
Output:

aIT
ry
ha
Ac
E,
S&
fC
.o
pt
De

Dept. of Computer Science & Engg, AcIT "17


Data Structures Laboratory (17CSL38) 2018-19

7. Design, Develop and Implement a menu driven Program in C for the following operations
on Singly Linked List (SLL) of Student Data with the fields: USN, Name, Branch, Sem, PhNo
a) Create a SLL of N Students Data by using front insertion.
b) Display the status of SLL and count the number of nodes in it
c) Perform Insertion and Deletion at End of SLL
d) Perform Insertion and Deletion at Front of SLL
e) Demonstrate how this SLL can be used as STACK and QUEUE
f) Exit

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int count=0;
struct node
{

IT
int sem, phno;
char name[20], branch[10], usn[20];

a
struct node *next;
}*first=NULL,*last=NULL,*temp=NULL, *temp1;
ry
ha
void create( )
{
Ac

int sem, phno;


char name[20],branch[10],usn[20];
E,

temp=(struct node*)malloc(sizeof(struct node));


printf("\n Enter USN, NAME, BRANCH, SEMESTER, PHNUM of student : ");
S&

scanf("%s %s %s %d %d", usn, name,branch, &sem,&phno);


strcpy(temp->usn,usn);
strcpy(temp->name,name);
fC

strcpy(temp->branch,branch);
temp->sem = sem;
.o

temp->phno = phno;
temp->next=NULL;
pt

count++;
}
De

void insert_atfirst( )
{
if (first == NULL)
{
create( );
first = temp;
last = first;
}
else
{
create( );
Dept. of Computer Science & Engg, AcIT "18
Data Structures Laboratory (17CSL38) 2018-19
temp->next = first;
first = temp;
}
}

void insert_atlast( )
{
if(first==NULL)
{
create( );
first = temp;
last = first;
}
else
{
create( );

IT
last->next = temp;
last = temp;

a
}
}
ry
ha
void display( )
{
Ac

temp1=first;
if(temp1 == NULL)
E,

{
printf("List empty to display \n");
S&

return;
}
printf("\n Linked list elements from begining : \n");
fC

printf("USN\t NAME\t BRANCH\t SEMESTER\t PH.NUM\n");


while (temp1!= NULL)
.o

{
printf("%s\t %s\t %s\t %d\t\t %d\n", temp1->usn, temp1->name,temp1-
pt

>branch,temp1->sem,temp1->phno);
temp1 = temp1->next;
De

}
printf(" No of students = %d ", count);
}

void delete_end( )
{
struct node *temp;
temp=first;
if(first==NULL) /* List is Empty */
{
printf("List is Empty\n");
return;
Dept. of Computer Science & Engg, AcIT "19
Data Structures Laboratory (17CSL38) 2018-19
}
if(temp->next==NULL) /* If only there is one node in the List */
{
printf("%s %s %s %d %d\n", temp->usn, temp->name,temp->branch, temp->sem,
temp->phno );
free(temp);
first=NULL;
}
else /* If more than one node in the List */
{
while(temp->next!=last)
temp=temp->next;
printf("%s %s %s %d %d\n", last->usn, last->name,last->branch, last->sem, last-
>phno );
free(last);
temp->next=NULL;

IT
last=temp;
}

a
count--;
}
ry
ha
void delete_front( )
{
Ac

struct node *temp;


temp=first;
E,

if(first==NULL) /* List is Empty */


{
S&

printf("List is Empty\n");
return;
}
fC

if(temp->next==NULL) /* If only there is one node in the List */


{
.o

printf("%s %s %s %d %d\n", temp->usn, temp->name,temp->branch, temp->sem,


temp->phno );
pt

free(temp);
first=NULL;
De

}
else /* If more than one node in the List */
{
first=temp->next;
printf("%s %s %s %d %d", temp->usn, temp->name,temp->branch,temp->sem,
temp->phno );
free(temp);
}
count--;
}

int main( )
Dept. of Computer Science & Engg, AcIT "20
Data Structures Laboratory (17CSL38) 2018-19
{
int ch, n, i;
first=NULL;
temp = temp1 = NULL;
printf("-----------------MENU----------------------\n");
printf("\n 1 Create a SLL of n Employees");
printf("\n 2 - Display from beginning");
printf("\n 3 - Insert at end");
printf("\n 4 - delete at end");
printf("\n 5 - Insert at beg");
printf("\n 6 - delete at beg");
printf("\n 7 - exit\n");
printf("-------------------------------------------\n");
while (1)
{
printf("\n Enter choice : ");

IT
scanf("%d", &ch);
switch (ch)

a
{
ry
case 1: printf("\n Enter no of students : ");
scanf("%d", &n);
ha
for(i=0;i<n;i++)
insert_atfirst();
Ac

break;
case 2: display( );
E,

break;
case 3: insert_atlast( );
S&

break;
case 4: delete_end( );
break;
fC

case 5: insert_atfirst( );
break;
.o

case 6: delete_front( );
break;
pt

case 7: exit(0);
default:printf("Wrong Choice\n");
De

}
}
return 0;
}

Dept. of Computer Science & Engg, AcIT "21


Data Structures Laboratory (17CSL38) 2018-19
Output:

aIT
ry
ha
Ac
E,
S&
fC
.o
pt
De

Dept. of Computer Science & Engg, AcIT "22


Data Structures Laboratory (17CSL38) 2018-19

8. Design, Develop and Implement a menu driven Program in C for the following operations
on Doubly Linked List (DLL) of Employee Data with the fields: SSN, Name, Dept,
Designation, Sal, PhNo
a) Create a DLL of N Employees Data by using end insertion.
b) Display the status of DLL and count the number of nodes in it
c) Perform Insertion and Deletion at End of DLL
d) Perform Insertion and Deletion at Front of DLL
e) Demonstrate how this DLL can be used as Double Ended Queue
f) Exit

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int count=0;
struct node

IT
{
struct node *prev;

a
int ssn, phno;
float sal;
char name[20],dept[10],desg[20];
ry
ha
struct node *next;
}*first,*temp,*last;
Ac

void create( )
E,

{
int ssn,phno;
S&

float sal;
char name[20],dept[10],desg[20];
temp =(struct node *)malloc(sizeof(struct node));
fC

temp->prev = NULL;
temp->next = NULL;
.o

printf("\n Enter ssn,name,department, designation, salary and phno of employee : ");


scanf("%d %s %s %s %f %d", &ssn, name, dept, desg, &sal, &phno);
pt

temp->ssn = ssn;
strcpy(temp->name,name);
De

strcpy(temp->dept,dept);
strcpy(temp->desg,desg);
temp->sal = sal;
temp->phno = phno;
count++;
}

void display( )
{
temp = first;
if(temp == NULL)
{
Dept. of Computer Science & Engg, AcIT "23
Data Structures Laboratory (17CSL38) 2018-19
printf("List is Empty\n");
return;
}
printf("\n Linked list elements from begining : \n");
while (temp != NULL)
{
printf("%d %s %s %s %f %d\n", temp->ssn, temp->name,temp->dept,temp-
>desg,temp->sal, temp->phno );
temp = temp->next;
}
printf(" No of employees = %d", count);
}

void insert_front( )
{
if (first == NULL)

IT
{
create( );

a
first = temp;

}
last = first;
ry
ha
else
{
Ac

create( );
temp->next = first;
E,

first->prev = temp;
first = temp;
S&

}
}
fC

void delete_front( )
{
.o

struct node *cur=first;


if(first == NULL) /* If the List is Empty */
pt

{
printf("List is Empty\n");
De

return;
}
if(first->next == NULL) /*If there is only one node in the List */
{
printf("%d %s %s %s %f %d\n", first->ssn, first->name,first->dept, first->desg,first-
>sal, first->phno );
free(first);
first = NULL;
}
else
{
first = first->next;
Dept. of Computer Science & Engg, AcIT "24
Data Structures Laboratory (17CSL38) 2018-19
printf("%d %s %s %s %f %d", cur->ssn, cur->name,cur->dept, cur->desg,cur->sal,
cur->phno );
free(cur);
}
count--;
}

void insert_rear( )
{
if(first == NULL)
{
create( );
first = temp;
last = first;
}
else

IT
{
create( );

a
last->next = temp;
temp->prev = last;
last = temp;
ry
ha
}
}
Ac

void delete_rear( )
E,

{
if(first == NULL) /*If the list is Empty */
S&

{
printf("List is Empty\n");
return;
fC

}
if(first->next == NULL) /*If there is only one node in the List */
.o

{
printf("%d %s %s %s %f %d\n", first->ssn, first->name,first->dept, first->desg,first-
pt

>sal, first->phno );
free(first);
De

first = NULL;
}
else
{
temp = last->prev;
temp->next = NULL;
printf("%d %s %s %s %f %d\n", last->ssn, last->name,last->dept, last->desg,last-
>sal, last->phno );
free(last);
last=temp;
}
count--;
Dept. of Computer Science & Engg, AcIT "25
Data Structures Laboratory (17CSL38) 2018-19
}
int main( )
{
int ch,n,i;
first = NULL;
temp = last = NULL;
printf("-----------------MENU--------------------\n");
printf("\n 1 - Create a DLL of n emp");
printf("\n 2 - Display from beginning");
printf("\n 3 - Insert at front end");
printf("\n 4 - Delete at front end");
printf("\n 5 - Insert at rear end");
printf("\n 6 - Delete at rear end");
printf("\n 7 - Exit\n");
printf("------------------------------------------\n");
while (1)

IT
{
printf("\n Enter Choice : ");

a
scanf("%d", &ch);
switch (ch)
{
ry
ha
case 1: printf("\n Enter no of employees : ");
scanf("%d", &n);
Ac

for(i=0;i<n;i++)
insert_rear( );
E,

break;
case 2: display( );
S&

break;
case 3: insert_front( );
break;
fC

case 4: delete_front( );
break;
.o

case 5: insert_rear( );
break;
pt

case 6: delete_rear( );
break;
De

case 7: exit(0);
default:printf("Wrong Choice\n");
}
}
return 0;
}

Dept. of Computer Science & Engg, AcIT "26


Data Structures Laboratory (17CSL38) 2018-19
Output:

aIT
ry
ha
Ac
E,
S&
fC
.o
pt
De

Dept. of Computer Science & Engg, AcIT "27


Data Structures Laboratory (17CSL38) 2018-19

9. Design, Develop and Implement a Program in C for the following operations on Singly
Circular Linked List (SCLL) with header nodes
a) Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
b) Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the
result in POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations.

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
struct node
{
int cf, px, py, pz;
int flag;
struct node *link;

IT
};
typedef struct node NODE;

a
NODE* getnode( )
{
NODE *x;
ry
ha
x = (NODE*)malloc(sizeof(NODE));
if(x == NULL)
Ac

{
printf("Insufficient memory\n");
E,

exit(0);
}
S&

return x;
}
void display(NODE *head)
fC

{
NODE *temp;
.o

if(head->link == head)
{
pt

printf("Polynomial does not exist\n");


return;
De

}
temp = head->link;
printf("\n");
while(temp != head)
{
printf("%d x^%d y^%d z^%d",temp->cf,temp->px,temp->py,temp->pz);
if(temp->link != head)
printf(" + ");
temp = temp->link;
}
printf("\n");
}
Dept. of Computer Science & Engg, AcIT "28
Data Structures Laboratory (17CSL38) 2018-19

NODE* insert_rear(int cf,int x,int y,int z,NODE *head)


{
NODE *temp, *cur;
temp = getnode( );
temp->cf = cf;
temp->px = x;
temp->py = y;
temp->pz = z;
cur = head->link;
while(cur->link != head)
cur = cur->link;
cur->link = temp;
temp->link = head;
return head;
}

IT
NODE* read_poly(NODE *head)
{

a
int px, py, pz, cf, ch;
printf("\nEnter coeff: ");
scanf("%d",&cf);
ry
ha
printf("\nEnter x, y, z powers(0-indiacate NO term): ");
scanf("%d%d%d", &px, &py, &pz);
Ac

head = insert_rear(cf,px,py,pz,head);
printf("\nIf you wish to continue press 1 otherwise 0: ");
E,

scanf("%d", &ch);
while(ch != 0)
S&

{
printf("\nEnter coeff: ");
scanf("%d",&cf);
fC

printf("\nEnter x, y, z powers(0-indiacate NO term): ");


scanf("%d%d%d",&px, &py, &pz);
.o

head = insert_rear(cf,px,py,pz,head);
printf("\nIf you wish to continue press 1 otherwise 0: ");
pt

scanf("%d", &ch);
}
De

return head;
}
NODE* add_poly(NODE *h1,NODE *h2,NODE *h3)
{
NODE *p1, *p2;
int cf;
p1 = h1->link;
while(p1 != h1)
{
p2=h2->link;
while(p2 != h2)
{
Dept. of Computer Science & Engg, AcIT "29
Data Structures Laboratory (17CSL38) 2018-19
if(p1->px == p2->px && p1->py == p2->py && p1->pz == p2->pz)
break;
p2 = p2->link;
}
if(p2 != h2)
{
cf = p1->cf + p2->cf;
p2->flag = 1;
if(cf != 0)
h3 = insert_rear(cf,p1->px,p1->py,p1->pz,h3);
}
else
h3 = insert_rear(p1->cf,p1->px,p1->py,p1->pz,h3);
p1 = p1->link;
}
p2 = h2->link;

IT
while(p2 != h2)
{

a
if(p2->flag == 0)
ry
h3 = insert_rear(p2->cf,p2->px,p2->py,p2->pz,h3);
p2 = p2->link;
ha
}
return h3;
Ac

}
void evaluate(NODE *head)
E,

{
NODE *h1=head->link;
S&

int x, y, z;
float result = 0.0;
printf("\nEnter x, y, z, terms to evaluate:\n");
fC

scanf("%d%d%d", &x, &y, &z);


while(h1 != head)
.o

{
result = result + (h1->cf * pow(x,h1->px) * pow(y,h1->py) * pow(z,h1->pz));
pt

h1 = h1->link;
}
De

printf("\nPolynomial result is: %f", result);


}

int main( )
{
NODE *h1, *h2, *h3, *eval;
int ch;
while(1)
{
eval = getnode( );
h1 = getnode( );
h2 = getnode( );
Dept. of Computer Science & Engg, AcIT "30
Data Structures Laboratory (17CSL38) 2018-19
h3 = getnode( );
eval->link = eval;
h1->link = h1;
h2->link = h2;
h3->link = h3;
printf("\n\n1.Evaluate polynomial\n2.Add two polynomials\n3.Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nEnter polynomial to evaluate:\n");
eval = read_poly(eval);
display(eval);
evaluate(eval);
free(eval);
break;

IT
case 2: printf("\nEnter the first polynomial: ");
h1 = read_poly(h1);

a
printf("Flag = %d\n",h1->flag);
ry
printf("\nEnter the second polynomial: ");
h2 = read_poly(h2);
ha
h3 = add_poly(h1,h2,h3);
printf("\nFirst polynomial is: ");
Ac

display(h1);
printf("\nSecond polynomial is: ");
E,

display(h2);
printf("\nThe sum of 2 polynomials is: ");
S&

display(h3);
free(h1); free(h2); free(h3);
break;
fC

case 3: exit(0);
break;
.o

default:printf("\nInvalid entry");
}
pt

}
return 0;
De

Dept. of Computer Science & Engg, AcIT "31


Data Structures Laboratory (17CSL38) 2018-19
Output:

aIT
ry
ha
Ac
E,
S&
fC
.o
pt
De

Dept. of Computer Science & Engg, AcIT "32


Data Structures Laboratory (17CSL38) 2018-19

aIT
ry
ha
Ac
E,
S&
fC
.o
pt
De

Dept. of Computer Science & Engg, AcIT "33


Data Structures Laboratory (17CSL38) 2018-19
10. Design, Develop and Implement a menu driven Program in C for the following operations
on Binary Search Tree (BST) of Integers
a) Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
b) Traverse the BST in Inorder, Preorder and Post Order
c) Search the BST for a given element (KEY) and report the appropriate message
d) Exit

#include <stdio.h>
#include <stdlib.h>
struct BST
{
int data;
struct BST *left;
struct BST *right;
};
typedef struct BST NODE;

IT
NODE* createtree(NODE *root, int data)

a
{
if (root == NULL)
{
ry
ha
NODE *temp;
temp = (NODE*) malloc (sizeof(NODE));
Ac

temp->data = data;
temp->left = temp->right = NULL;
E,

return temp;
}
S&

if (data < (root->data))


root->left = createtree(root->left, data);
else if (data > root->data)
fC

root -> right = createtree(root->right, data);


return root;
.o

}
void inorder(NODE *root)
pt

{
if(root != NULL)
De

{
inorder(root->left);
printf("%d\t", root->data);
inorder(root->right);
}
}
void preorder(NODE *root)
{
if(root != NULL)
{
printf("%d\t", root->data);
preorder(root->left);
Dept. of Computer Science & Engg, AcIT "34
Data Structures Laboratory (17CSL38) 2018-19
preorder(root->right);
}
}
void postorder(NODE *root)
{
if(root != NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d\t", root->data);
}
}
NODE *search(NODE *root, int data)
{
if(root == NULL)
printf("\nElement not found");

IT
else if(data < root->data)
root->left = search(root->left, data);

a
else if(data > root->data)

else
root->right = search(root->right, data);
ry
ha
printf("\nElement found is: %d", root->data);
return root;
Ac

}
E,

int main( )
{
S&

int data, ch, i, n;


NODE *root = NULL;
while (1)
fC

{
printf("\n1.Creation of Binary Search Tree");
.o

printf("\n2.Inorder\n3.Preorder\n4.Postorder\n5.Search\n6.Exit");
printf("\nEnter your choice: ");
pt

scanf("%d", &ch);
switch (ch)
De

{
case 1: printf("\nEnter N value: " );
scanf("%d", &n);
printf("\nEnter the values to create BST
like(6,9,5,2,8,15,24,14,7,8,5,2)\n");
for(i=0; i<n; i++)
{
scanf("%d", &data);
root = createtree(root, data);
}
break;
case 2: printf("\nInorder Traversal: \n");
Dept. of Computer Science & Engg, AcIT "35
Data Structures Laboratory (17CSL38) 2018-19
inorder(root);
break;
case 3: printf("\nPreorder Traversal: \n");
preorder(root);
break;
case 4: printf("\nPostorder Traversal: \n");
postorder(root);
break;
case 5: printf("\nEnter the element to Search: ");
scanf("%d", &data);
root=search(root, data);
break;
case 6: exit(0);
default:printf("\nWrong Option");
break;
}

IT
}
return 0;

a
}
ry
ha
Ac
E,
S&
fC
.o
pt
De

Dept. of Computer Science & Engg, AcIT "36


Data Structures Laboratory (17CSL38) 2018-19
Output:

aIT
ry
ha
Ac
E,
S&
fC
.o
pt
De

Dept. of Computer Science & Engg, AcIT "37


Data Structures Laboratory (17CSL38) 2018-19

11. Design, Develop and Implement a Program in C for the following operations on Graph(G)
of Cities
a) Create a Graph of N cities using Adjacency Matrix.
b) Print all the nodes reachable from a given starting node in a digraph using DFS /
BFS method.

#include<stdio.h>
#include<stdlib.h>
int n, a[20][20], visited1[20], visited2[20], source;
void read_data( )
{
int i,j;
printf("enter the number of nodes\n");
scanf("%d",&n);
printf("enter the adjacency matrix\n");

IT
for(i=0;i<n;i++)
for(j=0;j<n;j++)

a
scanf("%d",&a[i][j]);
}
void print_data(int visited[])
ry
ha
{
int i;
Ac

for(i=0;i<n;i++)
{
E,

if(visited[i]==0)
printf("\nvertex %d is not reachable\n",i);
S&

else
printf("\nvertex %d is reachable\n",i);
}
fC

}
void BFS( )
.o

{
int f = 0, r = 0, q[20], i, j;
pt

q[r]=source;
visited1[source]=1;
De

while(f<=r)
{
i=q[f++];
printf("--%d--",i);
for(j=0;j<n;j++)
{
if(a[i][j]==1 && visited1[j]==0)
{
visited1[j]=1;
q[++r]=j;
}
}
Dept. of Computer Science & Engg, AcIT "38
Data Structures Laboratory (17CSL38) 2018-19
}
}
void DFS(int src, int *cnt)
{
int i, j;
printf("--%d--",src);
visited2[src]=1;
for(j=0;j<n;j++)
{
if(a[src][j]==1 && visited2[j]==0)
{
(*cnt)++;
DFS(j,cnt);
}
}
}

IT
int main( )
{

a
int i, choice, a,*count=&a;
read_data( );
ry
p r i n t f ( " \ t \ t * * A D J A C E N C Y M AT R I X F O R C I T I E S H A S C R E AT E D
ha
SUCCESSFULLY**\n");
while(1)
Ac

{
printf("\n1.BFS\n2.DFS\n3.Exit");
E,

printf("\nEnter Your Choice: ");


scanf("%d",&choice);
S&

switch(choice)
{
case 1: for(i=0;i<n;i++)
fC

visited1[i]=0;
printf("Enter the source vertex between 0 to %d\n",n-1);
.o

scanf("%d",&source);
BFS();
pt

print_data(visited1);
break;
De

case 2: for(i=0;i<n;i++)
visited2[i]=0;
printf("Enter the source vertex between 0 to %d\n",n-1);
scanf("%d",&source);
a=0;
DFS(source,count);
print_data(visited2);
if(*count==n-1)
printf("graph is connected\n");
else
printf("graph is not connected\n");
break;
Dept. of Computer Science & Engg, AcIT "39
Data Structures Laboratory (17CSL38) 2018-19
case 3: exit(0);
default:printf("\nEnter proper Choice");
}
}
return 0;
}

Output:

a IT
ry
ha
Ac
E,
S&
fC
.o
pt
De

Dept. of Computer Science & Engg, AcIT "40


Data Structures Laboratory (17CSL38) 2018-19

aIT
ry
ha
Ac
E,
S&
fC
.o
pt
De

Dept. of Computer Science & Engg, AcIT "41


Data Structures Laboratory (17CSL38) 2018-19
12. Given a File of N employee records with a set K of Keys(4-digit) which uniquely determine
the records in file F. Assume that file F is maintained in memory by a Hash Table(HT) of m
memory locations with L as the set of memory addresses (2-digit) of locations in HT. Let the
keys in K and addresses in L are Integers. Design and develop a Program in C that uses Hash
function H: K ®L as H (K)=K mod m (remainder method), and implement hashing technique
to map a given key K to the address space L. Resolve the collision (if any) using linear
probing.

#include<stdio.h>
#include<string.h>
#define MAX_ADDR 100
struct emmployee
{
int empid;
int age;
char name[20];

IT
}ht[MAX_ADDR], temp;

a
int hash(int key) //Hash function to convert 4-digit-key to 2-digit index (Hash Address)
{
return key % MAX_ADDR;
ry
ha
}
Ac

int main( )
{
E,

int n, i, index, count=0;


FILE *fp;
S&

printf("Enter the number of Employees\n");


scanf("%d", &n);
fp = fopen("source.txt","w");
fC

for(i=0; i<n; i++)


{
.o

printf("Enter the 4-digit key, age and name of an employee %d:\n", i+1);
scanf("%d", &temp.empid);
pt

scanf("%d", &temp.age);
fflush(stdin);
De

gets(temp.name);
fprintf(fp, "%d\t%d\t%s\n", temp.empid, temp.age, temp.name);
}
fclose(fp);
//Initialize all the employee id in hash table ht to -1
for(i=0; i<MAX_ADDR; i++)
ht[i].empid = -1;
// To store the records from file to hash table using remainder method of hashing
fp = fopen("source.txt", "r");
while(fscanf(fp, "%d%d%s", &temp.empid, &temp.age, temp.name)!=EOF)
{
if(count == MAX_ADDR)
Dept. of Computer Science & Engg, AcIT "42
Data Structures Laboratory (17CSL38) 2018-19
{
printf("No space is available\n");
break;
}
index = hash(temp.empid);
if(ht[index].empid == -1) //Found free location (No Collision)
{
ht[index].empid = temp.empid;
ht[index].age = temp.age;
strcpy(ht[index].name, temp.name);
count++;
}
else //Collision Resolution (Linear Probing)
{
printf("Collision found for employee id %d\n", temp.empid);
for(i=1; i<MAX_ADDR; i++) //start from 1

IT
{
index = (hash(temp.empid) + i) % MAX_ADDR;

a
if(ht[index].empid == -1) //Found free location
{
ry
ht[index].empid = temp.empid;
ha
ht[index].age = temp.age;
strcpy(ht[index].name, temp.name);
Ac

count++;
printf("Collision Resolved for empid %d\n", temp.empid);
E,

break;
}
S&

}
}
}
fC

fclose(fp);
printf("Hash Address \t Empid \t Age \t Name\n");
.o

for(i=0; i<MAX_ADDR;i++)
{
pt

if(ht[i].empid != -1)
printf("%d\t%d\t%d\t%s\n", i, ht[i].empid, ht[i].age, ht[i].name);
De

}
return 0;
}

Dept. of Computer Science & Engg, AcIT "43


Data Structures Laboratory (17CSL38) 2018-19

Output:
Run1 :

IT
Run 2:

a
ry
ha
Ac
E,
S&
fC
.o

"
pt
De

Dept. of Computer Science & Engg, AcIT "44

You might also like