0% found this document useful (0 votes)
28 views59 pages

C Lab Manual: Data Structures Guide

Data structure whehhewj hwhwjwjw wjwjwjjw

Uploaded by

uds2694
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views59 pages

C Lab Manual: Data Structures Guide

Data structure whehhewj hwhwjwjw wjwjwjjw

Uploaded by

uds2694
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Data structures using C BECL456D

CAMBRIDGE INSTITUTE OF TECHNOLOGY


(Approved by AICTE, Recognized by Government of Karnataka &
Affiliated to Visvesvaraya Technological University, Belgaum, Karnataka)
NBA & NAAC Accredited, an ISO 9001: 2015 Certified Institute
[Link], Bangalore-560 036 Phone: 080-25618798/99, Fax:080-25618789,

Department of Electronics & Communication


Engineering

IV Semester
Data Structure using C Lab Manual
Subject code: BECL456D

Lab coordinator: Prof. SHYAM SUNDAR V

Department of ECE, CITECH Bangalore Page 1


Data structures using C BECL456D

LIST of Experiments
[Link] Experiment name
Write a C Program to create a student record structure to store, N records, each record
having the structure shown below: USN, Student Name and Semester. Write necessary
functions a. To display all the records in the file. b. To search for a specific record based
1 on the USN. In case the record is not found, suitable message should be displayed. Both
the options in this case must be demonstrated. (Use pointer to structure for dynamic
memory allocation)
Write a C Program to construct a stack of integers and to perform the following
2 operations on it: a. Push b. Pop c. Display The program should print appropriate
messages for stack overflow, stack underflow, and stack empty.
Write a C Program to convert and print a given valid parenthesized infix arithmetic
expression to postfix expression. The expression consists of single character operands
3 and the binary operators + (plus), - (minus), * (multiply) and / (divide).

Write a C Program to simulate the working of a queue of integers using an array.


4 Provide the following operations: a. Insert b. Delete c. Display
Write a C Program using dynamic variables and pointers to construct a stack of integers
using singly linked list and to perform the following operations: a. Push b. Pop c.
5 Display The program should print appropriate messages for stack overflow and stack
empty
Write a C Program to support the following operations on a doubly linked list where
each node consists of integers: a. Create a doubly linked list by adding each node at the
front. b. Insert a new node to the left of the node whose key value is read as an input c.
6 Delete the node of a given data, if it is found, otherwise display appropriate message. d.
Display the contents of the list. (Note: Only either (a,b and d) or (a, c and d) may be
asked in the examination)
Write a C Program a. To construct a binary search tree of integers. b. To traverse the tree
7 using all the methods i.e., inorder, preorder and postorder. c. To display the elements in
the tree.
Write recursive C Programs for a. Searching an element on a given list of integers using
8
the Binary Search method. b. Solving the Towers of Hanoi problem.
Write a program to traverse a graph using BFS method. Write a program to check
9
whether given graph is connected or not using DFS method.
Design and develop a program in C that uses Hash Function H:K->L as H(K)=K mod
10 m(reminder method) and implement hashing technique to map a given key K to the
address space L. Resolve the collision (if any) using linear probing

Department of ECE, CITECH Bangalore Page 2


Data structures using C BECL456D

GENERAL INSTRUCTIONS

1. Students are instructed to come to Data Structures through C++ laboratory on time.
Late comers are not entertained in the lab.

2. Students should be punctual to the lab. If not, the conducted experiments will not be
repeated.

3. Students are expected to come prepared at home with the experiments which are going to be
performed.

4. Students are instructed to display their identity cards before entering into the lab.

5. Students are instructed not to bring mobile phones to the lab.

6. Any damage/loss of system parts like keyboard, mouse during the lab
session, it is student’s responsibility and penalty or fine will be collected from
the student.

7. Students should update the records and lab observation books session wise. Before
leaving the lab the student should get his lab observation book signed by the faculty.

8. Students should submit the lab records by the next lab to the concerned faculty members
in the staffroom for their correction and return.

9. Students should not move around the lab during the lab session.

10. If any emergency arises, the student should take the permission from faculty
member concerned in written format.

11. The faculty members may suspend any student from the lab session on disciplinary grounds.

12. Never copy the output from other students. Write down your own outputs.

Department of ECE, CITECH Bangalore Page 3


Data structures using C BECL456D

Experiment-1:

Write a C Program to create a student record structure to store, N records, each record
having the structure shown below: USN, Student Name and Semester. Write necessary
functions a. To display all the records in the file. b. To search for a specific record based on
the USN. In case the record is not found, suitable message should be displayed. Both the
options in this case must be demonstrated. (Use pointer to structure for dynamic memory
allocation)

#include <stdio.h>

#include <stdlib.h>

struct student {

char firstName[50];

int USN;

float marks;

};

void display(struct student *s,int l)

// displaying information

printf("\nRoll number= %d\n", (s+l)->USN);

printf("Name = %s\n",(s+l)->firstName);

printf("Marks= %f\n", (s+l)->marks);

void main() {

int N=3;

struct student *s1 = (struct student *) malloc(N * sizeof(struct student));

Department of ECE, CITECH Bangalore Page 4


Data structures using C BECL456D

int i,usn,ch=1;

printf("Enter information of students:\n");

// storing information

for (i = 0; i < N; i++) {

printf("Enter students database S[%d]=\n",i);

printf("\nFor roll number %d",i);

scanf("%d",&(s1+i)->USN);

printf("Enter first name: ");

scanf("%s", &(s1+i)->firstName);

printf("Enter marks: ");

scanf("%f", &(s1+i)->marks);

printf("Displaying Information:\n\n");

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

printf("Database of student S[%d]= \n",i);

display(s1,i);

while(ch)

printf("enter 1 to continue 0 to exit");

scanf("%d",&ch);

Department of ECE, CITECH Bangalore Page 5


Data structures using C BECL456D

if(ch==1)

printf("enter student USN to search the data base \n");

scanf("%d",&usn);

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

if((s1+i)->USN == usn)

printf("students record id Found \n");

display(s1,i);

break;

if(i==N)

printf("No records found\n");

else

break;

free(s1);

OUTPUT:

Department of ECE, CITECH Bangalore Page 6


Data structures using C BECL456D

Enter information of students:

Enter students database S[0] =

For roll number111

Enter first name: madhu

Enter marks: 98.5

Enter students database S[1] =

For roll number222

Enter first name: manu

Enter marks: 85.4

Enter students database S[2] =

For roll number333

Enter first name: Alex

Enter marks: 65.5

Displaying Information:

Database of student S[0]=

Roll number = 111

Name = madhu

Marks= 98.500000

Database of student S[1]=

Department of ECE, CITECH Bangalore Page 7


Data structures using C BECL456D

Roll number = 222

Name = manu

Marks= 85.400002

Database of student S[2]=

Roll number = 333

Name = Alex

Marks= 65.500000

enter 1 to continue 0 to exit1

enter student USN to search the data base

111

students record id Found

Roll number = 111

Name = madhu

Marks= 98.500000

enter 1 to continue 0 to exit1

enter student USN to search the data base

454

No records found

Department of ECE, CITECH Bangalore Page 8


Data structures using C BECL456D

Experiment-2:

Write a C Program to construct a stack of integers and to perform the following


operations on it: a. Push b. Pop c. Display The program should print appropriate
messages for stack overflow, stack underflow, and stack empty.

#include<stdio.h>
int stack[100]; /*Declaring a 100 element stack array*/
int top=-1; /*Declaring and initializing the stack pointer*/
void push(int); /*Declaring a function prototype for inserting an element into the stack*/
int pop(); /*Declaring a function prototype for removing an element from the stack*/
void display(); /*Declaring a function prototype for displaying the elements of a stack*/
int main()
{
int choice;
int num1=0,num2=0;
while(1)
{

/*Creating an interactive interface for performing stack operations*/


printf("Select a choice from the following:\n");
printf("1- Push, 2- POP, 3-Display, 4- exit\n");

printf("Your choice:\n");
scanf("%d",&choice);

switch(choice)
{
case 1:
{
printf("Enter the element to be pushed into the stack:\n");
scanf("%d",&num1);
push(num1); /*Inserting an element*/
break;
}

Department of ECE, CITECH Bangalore Page 9


Data structures using C BECL456D

case 2:
{
num2=pop(); /*Removing an element*/
printf("element popped out of the stack = %d\n",num2);
break;
}
case 3:
{
display(); /*Displaying stack elements*/
break;
}
case 4:
exit(1);
break;
default:
printf("Invalid choice!\n");
break;
}
}
return 0;
}
/*Push function*/
void push(int element)
{
if(top==99) /*Checking whether the stack is full*/
{
printf("Stack is Full/stack overflow.\n\n");
exit(1);
}
top=top+1; /*Incrementing stack pointer*/
stack[top]=element; /*Inserting the new element*/
}
/*Pop function*/
int pop()

Department of ECE, CITECH Bangalore Page 10


Data structures using C BECL456D

{
if(top==-1) /*Checking whether the stack is empty*/
{
printf("Stack is Empty/ stack under flow\n");
exit(1);
}
return(stack[top--]); /*Returning the top element and decrementing the stack pointer*/
}
void display()
{
int i;
printf("The various stack elements are:\n");
for(i=top;i>=0;i--)
printf("%d\n",stack[i]); /*Printing stack elements*/
}
OUTPUT:

Select a choice from the following:


1- Push, 2- POP, 3-Display, 4- exit
Your choice:
1
Enter the element to be pushed into the stack:
78
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
1
Enter the element to be pushed into the stack:
85
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
1
Enter the element to be pushed into the stack:

Department of ECE, CITECH Bangalore Page 11


Data structures using C BECL456D

56
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
1
Enter the element to be pushed into the stack:
55
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
1
Enter the element to be pushed into the stack:
65
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
3
The various stack elements are:
65
55
56
85
78
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
element popped out of the stack = 65
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
3
The various stack elements are:
55

Department of ECE, CITECH Bangalore Page 12


Data structures using C BECL456D

56
85
78
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
element popped out of the stack = 55
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
element popped out of the stack = 56
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
element popped out of the stack = 85
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
element popped out of the stack = 78
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
Stack is Empty/ stack under flow

Department of ECE, CITECH Bangalore Page 13


Data structures using C BECL456D

Experiment-3:
Write a C Program to convert and print a given valid parenthesized infix arithmetic
expression to postfix expression. The expression consists of single character operands
and the binary operators + (plus), - (minus), * (multiply) and / (divide).

Program:
#include<stdio.h>
#include<ctype.h>

char stack[100];
int top = -1;

void push(char x)
{
stack[++top] = x;
}

char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}

int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;

Department of ECE, CITECH Bangalore Page 14


Data structures using C BECL456D

return 0;
}

int main()
{
char exp[100];
char *e, x;
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;

while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}

while(top != -1)
{

Department of ECE, CITECH Bangalore Page 15


Data structures using C BECL456D

printf("%c ",pop());
}return 0;
}

OUTPUT:

Enter the expression : (a*(b-c)+d/e)

abc-*de/+

Experiment-4:
Write a C Program to simulate the working of a queue of integers using an array. Provide
the following operations: a. Insert b. Delete c. Display

/*Program for demonstrating implementation of queues using arrays*/


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

int queue[100]; /*Declaring a 100 element queue array*/


int front=-1; /*Declaring and initializing the front pointer*/
int rear=-1; /*Declaring and initializing the rear pointer*/

void insert(int); /*Declaring a function prototype for inserting an element into the queue*/
int del(); /*Declaring a function prototype for removing an element from the queue*/
void display(); /*Declaring a function prototype for displaying the queue elements*/

void main()
{
int choice;
int num1=0,num2=0;
while(1)
{

Department of ECE, CITECH Bangalore Page 16


Data structures using C BECL456D

/*Creating an interactive interface for performing queue operations*/

printf("\nSelect a choice from the following:\n");


printf("1- insert, 2- delete, 3-Display, 4- exit\n");
printf("\n\tYour choice: ");
scanf("%d",&choice);

switch(choice)
{
case 1:
{
printf("\n\tEnter the element to be added to the queue:");
scanf("%d",&num1);
insert(num1); /*Adding an element*/
break;
}

case 2:
{
num2=del(); /*Removing an element*/
if(num2 == -9999)
printf( "Element is not deleted \n");
else
printf("\n\t%d element removed from the queue\n\t",num2);
break;
}

case 3:
{
display(); /*Displaying queue elements*/
break;
}

case 4:

Department of ECE, CITECH Bangalore Page 17


Data structures using C BECL456D

exit(1);
break;

default:
printf("\nInvalid choice!\n");
break;
}
}
}

/*Insert function*/
void insert(int element)
{
if(front==-1) /*Adding element in an empty queue*/
{
front = rear = front+1;
queue[rear] = element;
return;
}

if(rear==99) /*Checking whether the queue is full*/


{
printf("Queue is Full.\n");
return;
}
rear=rear+1; /*Incrementing rear pointer*/
queue[rear]=element; /*Inserting the new element*/
}
/*Delete function*/
int del()
{
int i;
if(front==-1 && rear==-1) /*Checking whether the queue is empty*/
{

Department of ECE, CITECH Bangalore Page 18


Data structures using C BECL456D

printf("\n\tQueue is Empty.\n");
return (-9999);
}
if(front!=-1 && front==rear) /*Checking whether the queue has only one element
left*/
{
i=queue[front];
front=-1;
rear=-1;
return(i);
}
return(queue[front++]); /*Returning the front most element and incrementing the front
pointer*/
}

/*Display function*/
void display()
{
int i;
if(front==-1)
{
printf("\n\tQueue is Empty!\n");
return;
}

printf("\n\tThe various queue elements are:\n");


for(i=front;i<=rear;i++)
printf("\t%d",queue[i]); /*Printing queue elements*/
}

OUTPUT:

Select a choice from the following:


1- insert, 2- delete, 3-Display, 4- exit

Department of ECE, CITECH Bangalore Page 19


Data structures using C BECL456D

Your choice: 1

Enter the element to be added to the queue:78

Select a choice from the following:


1- insert, 2- delete, 3-Display, 4- exit

Your choice: 1

Enter the element to be added to the queue:45

Select a choice from the following:


1- insert, 2- delete, 3-Display, 4- exit

Your choice: 3

The various queue elements are:


78 45
Select a choice from the following:
1- insert, 2- delete, 3-Display, 4- exit

Your choice: 1

Enter the element to be added to the queue:23

Select a choice from the following:


1- insert, 2- delete, 3-Display, 4- exit

Your choice: 3

The various queue elements are:


78 45 23
Select a choice from the following:

Department of ECE, CITECH Bangalore Page 20


Data structures using C BECL456D

1- insert, 2- delete, 3-Display, 4- exit

Your choice: 1

Enter the element to be added to the queue:77

Select a choice from the following:


1- insert, 2- delete, 3-Display, 4- exit

Your choice: 3

The various queue elements are:


78 45 23 77
Select a choice from the following:
1- insert, 2- delete, 3-Display, 4- exit

Your choice: 2

78 element removed from the queue

Select a choice from the following:


1- insert, 2- delete, 3-Display, 4- exit

Your choice: 3

The various queue elements are:


45 23 77
Select a choice from the following:
1- insert, 2- delete, 3-Display, 4- exit

Your choice: 1

Enter the element to be added to the queue:55

Department of ECE, CITECH Bangalore Page 21


Data structures using C BECL456D

Select a choice from the following:


1- insert, 2- delete, 3-Display, 4- exit

Your choice: 3

The various queue elements are:


45 23 77 55
Select a choice from the following:
1- insert, 2- delete, 3-Display, 4- exit

Experiment-5

Write a C Program using dynamic variables and pointers to construct a stack of integers using
singly linked list and to perform the following operations: a. Push b. Pop c. Display The
program should print appropriate messages for stack overflow and stack empty.

Program:

#include <stdio.h>

#include <conio.h>

#include<stdlib.h>

struct stack /*Declaring the

structure for stack elements*/

int element;

struct stack *next; /*Stack element pointing to another stack element*/

}*top;

void push(int); /*Declaring a function prototype for inserting an element

into the stack*/

int pop(); /*Declaring a function prototype for removing an element from

Department of ECE, CITECH Bangalore Page 22


Data structures using C BECL456D

the stack*/

void display(); /*Declaring a function prototype for displaying the

elements of a stack*/

void main()

int num1, num2, choice;

while(1)

/*Creating an interactive interface for performing stack operations*/

printf("Select a choice from the following:\n");

printf("1- Push, 2- POP, 3-Display, 4- exit\n");

printf("Your choice:\n");

scanf("%d",&choice);

switch(choice)

case 1:

printf("Enter the element to be pushed into the stack:\n");

scanf("%d",&num1);

push(num1); /*Inserting an element*/

break;

Department of ECE, CITECH Bangalore Page 23


Data structures using C BECL456D

case 2:

num2=pop(); /*Removing an element*/

printf("element popped out of the stack = %d\n",num2);

break;

case 3:

display(); /*Displaying stack elements*/

break;

case 4:

exit(1);

break;

default:

printf("Invalid choice!\n");

break;

/*Push function*/

void push(int value)

Department of ECE, CITECH Bangalore Page 24


Data structures using C BECL456D

struct stack *ptr;

ptr=(struct stack*)malloc(sizeof(struct stack)); /*Dynamically allocating memory


space to store stack element*/

ptr->element=value; /*Assigning value to the newly allocated stack element*/

/*Updating stack pointers*/

ptr->next=top;

top=ptr;

return;

/*Pop function*/

int pop()

if(top==NULL) /*Checking whether the stack is empty*/

printf("\n STACK is Empty/Stack overflow.");

exit(1);

else

int temp=top->element; /* Retrieving the top element*/

top=top->next; /*Updating the stack pointer*/

return (temp); /*Returning the popped value*/

/*display */

Department of ECE, CITECH Bangalore Page 25


Data structures using C BECL456D

void display()

struct stack *ptr1=NULL;

ptr1=top;

printf("\nThe various stack elements are:\n");

while(ptr1!=NULL)

printf("%d\t",ptr1->element); /*Printing stack elements*/

ptr1=ptr1->next;

OUTPUT:

Select a choice from the following:


1- Push, 2- POP, 3-Display, 4- exit
Your choice:
1
Enter the element to be pushed into the stack:
78
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
1
Enter the element to be pushed into the stack:
85
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
1

Department of ECE, CITECH Bangalore Page 26


Data structures using C BECL456D

Enter the element to be pushed into the stack:


56
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
1
Enter the element to be pushed into the stack:
55
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
1
Enter the element to be pushed into the stack:
65
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
3
The various stack elements are:
65
55
56
85
78
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
element popped out of the stack = 65
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
3
The various stack elements are:

Department of ECE, CITECH Bangalore Page 27


Data structures using C BECL456D

55
56
85
78
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
element popped out of the stack = 55
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
element popped out of the stack = 56
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
element popped out of the stack = 85
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
element popped out of the stack = 78
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
Stack is Empty/ stack under flow

Department of ECE, CITECH Bangalore Page 28


Data structures using C BECL456D

Experiment – 6:

Write a C Program to support the following operations on a doubly linked list where each
node consists of integers:

a. Create/Insert a doubly linked list by adding each node at the front.

b. Delete the node of a given data, if it is found, otherwise display appropriate message.

c. Display the contents of the list.

Algorithm:

The following structure declaration defines the node of a doubly linked list:

➢ The above structure declaration defines a new data type called NODE that represents
a doubly linked list node.

➢ The node structure contains three members, INFO for storing integer data values,
NEXT for storing address of the next node, and PREVIOUS for storing the address of
the previous node.

INSERT:

➢ The insert operation in a doubly linked list is performed in the same manner as a
singly linked list.

➢ The only exception is that the additional node pointer PREVIOUS is also required to
be updated for the new node at the time of insertion.

Department of ECE, CITECH Bangalore Page 29


Data structures using C BECL456D

DELETE:

➢ Delete The delete operation in a doubly linked list is performed in the same
manner as a singly linked list.
➢ The only exception is that the additional node pointer PREVIOUS of the adjacent
node is also required to be updated at the time of deletion.

SEARCH:

Department of ECE, CITECH Bangalore Page 30


Data structures using C BECL456D

PRINT:

➢ The print operation prints or displays the linked list elements on the screen.

➢ To print the elements, the linked list is traversed from start till end using NEXT
pointers.

Department of ECE, CITECH Bangalore Page 31


Data structures using C BECL456D

PROGRAM:

#include<stdio.h>

#include<stdlib.h>

struct dl_node /*Doubly linked list declaration*/

int INFO;

struct dl_node *NEXT;

struct dl_node *PREVIOUS;

};

struct dl_node *FIRST = NULL;

struct dl_node *LAST = NULL;

void insert(int value)/*Insert function*/

/*Creating a new node*/

Department of ECE, CITECH Bangalore Page 32


Data structures using C BECL456D

struct dl_node *PTR =(struct dl_node *)malloc(sizeof(struct dl_node )); /*Storing the
element to be inserted in the new node*/

PTR->INFO = value;/*Linking the new node to the doubly linked list*/

if(FIRST==NULL)

FIRST = LAST = PTR;

PTR->NEXT=NULL;

PTR->PREVIOUS=NULL;

else

LAST->NEXT = PTR;

PTR->NEXT = NULL;

PTR->PREVIOUS = LAST;

LAST = PTR;

/*Search function*/

struct dl_node *search (int value)

struct dl_node *PTR;

if(FIRST==NULL) /*Checking for empty list*/

return(NULL);

if(FIRST==LAST && FIRST->INFO==value) /*Checking if there is only one


element in the list*/

Department of ECE, CITECH Bangalore Page 33


Data structures using C BECL456D

return(FIRST);

/*Searching the linked list*/

for(PTR=FIRST;PTR!=LAST;PTR=PTR->NEXT)

if(PTR->INFO==value)

return(PTR); /*Returning the location of the searched element*/

if(LAST->INFO==value)

return(LAST);

else

return(NULL); /*Returning NULL value indicating unsuccessful search*/

/*Delete function*/

int Delete(int value)

struct dl_node *LOC,*TEMP;

int i;

i=value;

LOC=search(i); /*Calling the search() function*/

if(LOC==NULL) /*Element not found*/

return(-9999);

if(LOC==FIRST)

if(FIRST==LAST)

FIRST=LAST=NULL;

Department of ECE, CITECH Bangalore Page 34


Data structures using C BECL456D

else

FIRST=FIRST->NEXT;

FIRST->PREVIOUS=NULL;

return(value);

for(TEMP=FIRST;TEMP->NEXT!=LOC;TEMP=TEMP->NEXT);

if(LOC==LAST)

LAST=TEMP;

TEMP->NEXT=NULL;

else

TEMP->NEXT=LOC->NEXT;

LOC->NEXT->PREVIOUS=TEMP;

return(LOC->INFO);

/*print function*/

void print()

Department of ECE, CITECH Bangalore Page 35


Data structures using C BECL456D

struct dl_node *PTR;

if(FIRST==NULL) /*Checking whether the list is empty*/

printf("Empty List\n");

return;

printf("Doubly linked list elements: are \n ");

/*Printing the list elements*/

for(PTR=FIRST;PTR!=NULL;PTR=PTR->NEXT)

printf("%d\t",PTR->INFO);

int main()

int num1, num2, choice;

struct dl_node *location;

/*Displaying a menu of choices for performing list operations*/

while(1)

printf("Select an option\n ");

printf("1 - Insert, 2-Delete, 3- Print, 4-Exit \n");

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

Department of ECE, CITECH Bangalore Page 36


Data structures using C BECL456D

scanf("%d",&choice);

switch(choice)

case 1:

printf("Enter the element to be inserted into the doubly linked list:\n");

scanf("%d",&num1);

insert(num1); /*Calling the insert() function*/

printf(" %d successfully inserted into the linked list! \n",num1);

break;

case 2:

printf("Enter the element to be deleted from the doubly linked list: \n");

scanf("%d",&num1);

num2=Delete(num1); /*Calling the delete() function */

if(num2==-9999)

printf(" %d is not present in the doubly linked list \n",num2);

else

printf(" %d Element successfully deleted from the doubly linked list


\n",num2);

break;

case 3:

Department of ECE, CITECH Bangalore Page 37


Data structures using C BECL456D

print(); /*Printing the list elements*/

break;

case 4:

exit(1);

break;

default:

printf("Incorrect choice. Please try again \n");

break;

return 0;

OUTPUT:

Select an option

1 - Insert, 2-Delete, 3- Print, 4-Exit

Enter your choice:

Enter the element to be inserted into the doubly linked list:

Department of ECE, CITECH Bangalore Page 38


Data structures using C BECL456D

55

55 successfully inserted into the linked list!

Select an option

1 - Insert, 2-Delete, 3- Print, 4-Exit

Enter your choice:

Enter the element to be inserted into the doubly linked list:

35

35 successfully inserted into the linked list!

Select an option

1 - Insert, 2-Delete, 3- Print, 4-Exit

Enter your choice:

Enter the element to be inserted into the doubly linked list:

89

89 successfully inserted into the linked list!

Select an option

1 - Insert, 2-Delete, 3- Print, 4-Exit

Enter your choice:

Enter the element to be inserted into the doubly linked list:

78

78 successfully inserted into the linked list!

Select an option

Department of ECE, CITECH Bangalore Page 39


Data structures using C BECL456D

1 - Insert, 2-Delete, 3- Print, 4-Exit

Enter your choice:

Enter the element to be deleted from the doubly linked list:

-9999 is not present in the doubly linked list

Select an option

1 - Insert, 2-Delete, 3- Print, 4-Exit

Enter your choice:

Doubly linked list elements: are

55 35 89 78 Select an option

1 - Insert, 2-Delete, 3- Print, 4-Exit

Enter your choice:

Enter the element to be deleted from the doubly linked list:

35

35 Element successfully deleted from the doubly linked list

Select an option

1 - Insert, 2-Delete, 3- Print, 4-Exit

Enter your choice:

Doubly linked list elements: are

55 89 78

Department of ECE, CITECH Bangalore Page 40


Data structures using C BECL456D

Experiment -7:

Write a C Program a. To construct a binary search tree of integers. b. To traverse the


tree using all the methods i.e., inorder, preorder and postorder. c. To display the
elements in the tree.

Algorithm: Insert(root, ele)

if tree is empty then insert a node as root node otherwise go to next step if ele is less than the
root node the insert left sub tree of root node otherwise insert right sub tree of root node

Algorithm inorder(ptr)

Initially top=-1; if(ptr==NULL) return;

else

while(ptr!=NULL||top>=0)

if(ptr!=NULL)

top++;//push the value in stack stack[top]=ptr;

ptr=ptr->left;

else

ptr=stack[top]; display ptr->data top--; //pop the stack ptr=ptr->right;

Department of ECE, CITECH Bangalore Page 41


Data structures using C BECL456D

Source Code:

#include <stdio.h>

#include<stdlib.h>

struct node

int INFO;

struct node *LEFT, *RIGHT;

};

struct node *insert(struct node *r, int n)

if(r==NULL)

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

r->LEFT = r->RIGHT = NULL;

r->INFO = n;

else if(n<r->INFO)

r->LEFT = insert(r->LEFT, n);

else if(n>r->INFO)

Department of ECE, CITECH Bangalore Page 42


Data structures using C BECL456D

r->RIGHT = insert(r->RIGHT, n);

else if(n==r->INFO)

printf("Insert Operation failed: Duplicate Entry!!\n");

return(r);

//inorder traversal

void inorder(struct node *r)

if(r->LEFT!=NULL)

inorder(r->LEFT);

printf("%d\t",r->INFO);

if(r->RIGHT!=NULL)

inorder(r->RIGHT);

//preorder traversal

void preorder(struct node *r)

printf("%d\t",r->INFO);

if(r->LEFT!=NULL)

preorder(r->LEFT);

Department of ECE, CITECH Bangalore Page 43


Data structures using C BECL456D

if(r->RIGHT!=NULL)

preorder(r->RIGHT);

//postorder traversal

void postorder(struct node *r)

if(r->LEFT!=NULL)

postorder(r->LEFT);

if(r->RIGHT!=NULL)

postorder(r->RIGHT);

printf("%d\t",r->INFO);

int main()

struct node *root = NULL;

int element, choice, num, flag;

/*Displaying a menu of choices*/

while(1)

printf("Select an option \n");

printf("1 Insert, 2 -Inorder 3- preorder 4- postorder 5-exit, \n");

Department of ECE, CITECH Bangalore Page 44


Data structures using C BECL456D

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

scanf("%d",&choice);

switch(choice)

case 1:

printf("Enter the node value: \n");

scanf("%d",&element);

root = insert(root,element); /*Calling the insert function for inserting a new


element into the tree*/

break;

case 2:

inorder(root);

break;

case 3:

preorder(root);

break;

case 4:

Department of ECE, CITECH Bangalore Page 45


Data structures using C BECL456D

postorder(root);

break;

case 5:

exit(1);

break;

default:

printf("Incorrect choice. Please try again \n");

break;

return 0;

OUTPUT:

Select an option

1 Insert, 2 -Inorder 3- preorder 4- postorder 5-exit,

Enter your choice:

Enter the node value:

Department of ECE, CITECH Bangalore Page 46


Data structures using C BECL456D

55

Select an option

1 Insert, 2 -Inorder 3- preorder 4- postorder 5-exit,

Enter your choice:

Enter the node value:

35

Select an option

1 Insert, 2 -Inorder 3- preorder 4- postorder 5-exit,

Enter your choice:

Enter the node value:

65

Select an option

1 Insert, 2 -Inorder 3- preorder 4- postorder 5-exit,

Enter your choice:

Enter the node value:

25

Select an option

1 Insert, 2 -Inorder 3- preorder 4- postorder 5-exit,

Enter your choice:

Enter the node value:

Department of ECE, CITECH Bangalore Page 47


Data structures using C BECL456D

75

Select an option

1 Insert, 2 -Inorder 3- preorder 4- postorder 5-exit,

Enter your choice:

25 35 55 65 75 Select an option

1 Insert, 2 -Inorder 3- preorder 4- postorder 5-exit,

Enter your choice:

55 35 25 65 75 Select an option

1 Insert, 2 -Inorder 3- preorder 4- postorder 5-exit,

Enter your choice:

25 35 75 65 55 Select an option

1 Insert, 2 -Inorder 3- preorder 4- postorder 5-exit,

Enter your choice:

Department of ECE, CITECH Bangalore Page 48


Data structures using C BECL456D

Experiment -8:

Write recursive C Programs for

a) Searching an element on a given list of integers using the Binary Search method.

Program:

Algorithm:

Step 1: Start
Step 2: Initialize
low = 1 high = n
Step 3: Perform Search While(low <= high)
Step 4: Obtain index of midpoint of interval Middle = (low + high) / 2
Step 5: Compare
if(X < K[middle]) high = middle - 1 else
print “Element found at position” Return(middle)
goto: step 2
Step 6: Unsuccessful Search
print “Element found at position” Return (middle)
Step 7: Stop

#include<stdio.h>
int main()
{
int a[10], i, n, key, low, high, mid;
printf("enter the array size \n");
scanf("%d",&n);

printf("Enter the array elements in ascending order \n");


for(i = 0; i < n; i++)
scanf("%d",&a[i]);

printf("Enter the key element \n");


scanf("%d",&key);

Department of ECE, CITECH Bangalore Page 49


Data structures using C BECL456D

low = 0; high = n - 1;
while(high >= low)
{
mid = (low + high) / 2;
if(key == a[mid])
break;
else
{
if(key > a[mid])
low = mid + 1;
else
high = mid - 1;
}
}
if(key == a[mid])
printf("The key element is found at location %d \n",mid + 1);
else
printf("the key element is not found \n");
}

Output:

Enter the size of the array 7


Enter the array elements in ascending order 23 45 68 90
100 789 890 Enter the key element 789
The key Element is found at location 6

b. Solving the Towers of Hanoi problem.

Department of ECE, CITECH Bangalore Page 50


Data structures using C BECL456D

Tower of Hanoi using Recursion:


The idea is to use the helper node to reach the destination using recursion. Below is the
pattern for this problem:
• Shift ‘N-1’ disks from ‘A’ to ‘B’, using C.
• Shift last disk from ‘A’ to ‘C’.
• Shift ‘N-1’ disks from ‘B’ to ‘C’, using A.

Follow the steps below to solve the problem:


• Create a function towerOfHanoi where pass the N (current number of
disk), from_rod, to_rod, aux_rod.
• Make a function call for N – 1 th disk.
• Then print the current the disk along with from_rod and to_rod
• Again make a function call for (N – 1)th disk.

Program:

#include <stdio.h>

// C recursive function to solve tower of hanoi puzzle

void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)

Department of ECE, CITECH Bangalore Page 51


Data structures using C BECL456D

if (n == 1)

printf("\n Move disk 1 from rod %c to rod %c", from_rod, to_rod);

return;

towerOfHanoi(n-1, from_rod, aux_rod, to_rod);

printf("\n Move disk %d from rod %c to rod %c", n, from_rod, to_rod);

towerOfHanoi(n-1, aux_rod, to_rod, from_rod);

int main()

int n = 4; // Number of disks

towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods

return 0;

OUTPUT:

Move disk 1 from rod A to rod C

Move disk 2 from rod A to rod B

Move disk 1 from rod C to rod B

Move disk 3 from rod A to rod C

Move disk 1 from rod B to rod A

Move disk 2 from rod B to rod C

Department of ECE, CITECH Bangalore Page 52


Data structures using C BECL456D

Move disk 1 from rod A to rod C

Experiment -9:

Write a program to traverse a graph using BFS method. Write a program to check
whether given graph is connected or not using DFS method.

BFS Algorithm

1. Start with the initial node.


2. Mark the initial node as visited and enqueue it.
3. While the queue is not empty:

• Dequeue a node from the queue.


• If the dequeued node is the goal node, stop the search.
• Otherwise, enqueue any successors (nodes that are directly connected to the
dequeued node) that have not yet been visited.
1. If the queue is empty, every node on the graph has been visited, or there is no path
from the initial node to the goal node.
5. If the goal node was found, return the path that was followed.

Department of ECE, CITECH Bangalore Page 53


Data structures using C BECL456D

Depth First Search:

▪ Unlike the BFS traversal method, which visits the graph nodes level by level, the DFS
method visits the graph nodes along the different paths.

▪ It begins analyzing the nodes from the start to the end node and then proceeds along
the next path from the start node.

▪ This process is repeated until all the graph nodes are visited.

▪ The DFS method also requires frequent backtracking to the already analyzed nodes.

▪ It uses the stack data structure for storing information related to the previous nodes.

▪ Let us again consider the graph shown in Fig. 9.6.

▪ The DFS traversal sequence for this graph will be: v1, v2, v4, v8, v5, v7, v3, v6.

▪ Another DFS traversal sequence can be: v1, v3, v6, v7, v8, v2, v5, v4.

Department of ECE, CITECH Bangalore Page 54


Data structures using C BECL456D

Experiment -10:

Design and develop a program in C that uses Hash Function H:K->L as H(K)=K mod
m(reminder method) and implement hashing technique to map a given key K to the address
space L. Resolve the collision (if any) using linear probing.

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.

Department of ECE, CITECH Bangalore Page 55


Data structures using C BECL456D

Program:

#include<stdio.h>

#include<stdlib.h>

int key[20],n,m;

int *ht,index;

int count = 0;

void insert(int key)

index = key % m;

while(ht[index] != -1)

index = (index+1)%m;

ht[index] = key;

count++;

void display()

int i;

if(count == 0)

printf("\nHash Table is empty");

return;

Department of ECE, CITECH Bangalore Page 56


Data structures using C BECL456D

printf("\nHash Table contents are:\n ");

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

printf("\n T[%d] --> %d ", i, ht[i]);

void main()

int i;

printf("\nEnter the number of employee records (N) : ");

scanf("%d", &n);

printf("\nEnter the two digit memory locations (m) for hash table: ");

scanf("%d", &m);

ht = (int *)malloc(m*sizeof(int));

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

ht[i] = -1;

printf("\nEnter the four digit key values (K) for N Employee Records:\n ");

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

scanf("%d", &key[i]);

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

if(count == m)

printf("\n~~~Hash table is full. Cannot insert the record %d key~~~",i+1);

break;

Department of ECE, CITECH Bangalore Page 57


Data structures using C BECL456D

insert(key[i]);

//Displaying Keys inserted into hash table

display();

OUTPUT:

Enter the number of employee records (N) : 4

Enter the two digit memory locations (m) for hash table: 10

Enter the four digit key values (K) for N Employee Records:

1240

1890

1211

1289

Hash Table contents are:

T[0] --> 1240

T[1] --> 1890

T[2] --> 1211

T[3] --> -1

T[4] --> -1

Department of ECE, CITECH Bangalore Page 58


Data structures using C BECL456D

T[5] --> -1

T[6] --> -1

T[7] --> -1

T[8] --> -1

T[9] --> 1289

Department of ECE, CITECH Bangalore Page 59

You might also like