Data Structure Lab Manual
Data Structure Lab Manual
For
Computer programming
Laboratory
2020-2021
Submitted by
RUSHIKESH MANURKAR
2020UGEC017R
Submitted to
Theory:-
Traverse(): To see the contents of the linked list, it is necessary to
traverse the given linked list. The given traverse() function traverses and prints
the content of the linked list.
• insertAtFront(): This function simply inserts an element at the
front/beginning of the linked list.
{
int choice;
while(1)
{
printf("\n\n~~~~MENU~~~~");
printf("\n=>1. Create an array of N integers");
printf("\n=>2. Display of array elements");
printf("\n=>3. Insert ELEM at a given POS");
printf("\n=>4. Delete an element at a given POS");
printf("\n=>5. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert();
break;
case 4:del();
break;
case 5:exit(1);
break;
default:printf("\nPlease enter a valid choice:");
}
}
}
void create()
{
int i;
printf("\nEnter the number of elements: ");
scanf("%d", &n);
printf("\nEnter the elements: ");
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
}
void display()
{
int i;
if(n == 0)
{
printf("\nNo elements to display");
return;
}
printf("\nArray elements are: ");
for(i=0; i<n;i++)
printf("%d\t ", a[i]);
}
void insert()
{
int i;
if(n == 5)
{
printf("\nArray is full. Insertion is not possible");
return;
}
do
{
printf("\nEnter a valid position where element to be
inserted: ");
scanf("%d", &pos);
}
while(pos > n);
printf("\nEnter the value to be inserted: ");
scanf("%d", &elem);
for(i=n-1; i>=pos ; i--)
{
a[i+1] = a[i];
}
a[pos] = elem;
n = n+1;
display();
}
void del()
{
int i;
if(n == 0)
{
printf("\nArray is empty and no elements to
delete");
return;
}
do
{
printf("\nEnter a valid position from where
element to be deleted: ");
scanf("%d", &pos);
}
while(pos>=n);
elem = a[pos];
printf("\nDeleted element is : %d \n", elem);
for( i = pos; i< n-1; i++)
{
a[i] = a[i+1];
}
n = n-1;
display();
}
Output:-
EXPERIMENT 2
Aim:- Create Singly Linked List with three nodes
and traverse it.
/* Structure of a node */
struct node {
int data; // Data
struct node *next; // Address
}*head;
/*
* Functions to create and display list
*/
void createList(int n);
void traverseList();
int main()
{
int n;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
return 0;
}
/*
* Create a list of n nodes
*/
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
/*
* Display entire list
*/
void traverseList()
{
struct node *temp;
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data); // Print data of current node
temp = temp->next; // Move to next node
}
}
Output:-
Experiment 3
Aim:- Write a menu driven C program for Singly
Linked List (SLL) for given operations:
1.) Traverse
Code:-
#include <stdio.h>
#include <stdlib.h>
struct node{
int info;
struct node *next
};
struct node *head=NULL;
void insertatfront()
{
int data;
struct node *temp;
temp=malloc(sizeof(struct node));
printf("Enter The NO. to inserted ...!");
scanf("%d",&data);
temp->info=data;
temp->next=0;
temp->info=data; //change links of nodes
start=head;
while(head->next!=NULL);
{
head=head->next;
}
head->next=temp;
}
void insertat_position()
{
struct node *temp , *newnode;
int pos,data=1;
int i=1;
newnode=malloc(sizeof(struct node));
printf("Enter position & data");
scanf("%d%d",&pos,&data);
temp=head;
newnode->info=data;
newnode->next=0;
while(i < pos-1)
{
temp = temp -> next;
i++;
}
newnode->next=temp->next;
temp->next=newnode;
}
void delectatfront()
{
struct node *temp;
if(head==NULL)
{
printf("List is EMPTY \n");
}
else
{
temp=head;
head=head->next;
free(temp);
}
void delectatend()
{
struct node *temp,*prevnode;
if(head==NULL)
{
printf("List not EXIST\n");
}
temp=head;
while(temp->next!=0)
{
prevnode=temp;
temp=temp->next;
}
free(temp);
prevnode->next=0;
}
void delectatpostion()
{
struct node *temp,*position;
int i=1,pos;
if(head==NULL)
{
printf("The list is EXIST");
}
else
{
printf("Enter the Position");
scanf("%d",&pos);
position=malloc(sizeof(struct node));
temp=head;
}
void traverse()
{
struct node *temp;
if(head==NULL)
{
printf("List is Empty");
}
else
{
temp=head;
while(temp!=NULL)
{
printf("%d",temp->info);
temp=temp->next;
}
}
}
int main()
{
int choice;
while(1)
{
printf("\n1.Insert the node at beginning");
printf("\n2.Insert the node at end");
printf("\n3.Insert the node at any position");
printf("\n4.Delecte your node at beginning");
printf("\n5.Delecte your node at end");
printf("\n6.Delecte your node from any position");
printf("\n7.view your linked list");
printf("\n8.exist");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insertatfront();
break;
case 2:
insertatend();
break;
case 3:
insertat_position();
break;
case 4:
delectatfront();
break;
case 5:
delectatend();
break;
case 6:
delectatpostion();
break;
case 7:
traverse();
break;
case 8:
exit(0);
break;
default:
printf("Invalid input\n ");
}
}
return 0;
}
Output:-
Experiment 4
Aim:- Write a menu driven C program for Singly
Linked List (SLL) for given operations:
1.) Traverse
2.) Maximum
3.) Mean
4.) Sort
5.) Reversal
Theroy:-
• Traverse(): To see the contents of the linked list, it is necessary to
traverse the given linked list. The given traverse() function traverses
and prints the content of the linked list.
• InsertAtFront(): This function simply inserts an element at the
front/beginning of the linked list.
• InsertAtEnd(): This function inserts an element at the end of the
linked list.
• InsertAtPosition(): This function inserts an element at a specified
position in the linked list.
• DeleteFirst(): This function simply deletes an element from the
front/beginning of the linked list.
• DeleteEnd(): This function simply deletes an element from the end of
the linked list.
• DeletePosition(): This function deletes an element from a specified
position in the linked list.
• Maximum(): This function finds the maximum element in a linked list.
• Mean(): This function finds the mean of the elements in a linked list.
• Sort(): This function sort the given linked list in ascending order.
• ReverseLL(): This function reverses the given linked list.
Code:-
// C program for the all operations in
// the Singly Linked List
#include <stdio.h>
#include<stdlib.h>
// List is empty
if (start == NULL)
printf("\nList is empty\n");
// Changes links
temp->link = 0;
temp->info = data;
head = start;
while (head->link != NULL) {
head = head->link;
}
head->link = temp;
}
// Function to insert at any specified
// position in the linked list
void insertAtPosition()
{
struct node *temp, *newnode;
int pos, data, i = 1;
newnode = malloc(sizeof(struct node));
// Change Links
temp = start;
newnode->info = data;
newnode->link = 0;
while (i < pos - 1) {
temp = temp->link;
i++;
}
newnode->link = temp->link;
temp->link = newnode;
}
// If LL is empty
if (start == NULL)
printf("\nList is empty\n");
// Otherwise
else {
printf("\nEnter index : ");
// Position to be deleted
scanf("%d", &pos);
position = malloc(sizeof(struct node));
temp = start;
// Change Links
position = temp->link;
temp->link = position->link;
// Free memory
free(position);
}
}
// If LL is empty
if (start == NULL)
printf("\nList is empty\n");
// Otherwise
else {
temp = start;
int max = temp->info;
// If LL is empty
if (start == NULL)
printf("\nList is empty\n");
// Otherwise
else {
temp = start;
// Traverse the LL
while (temp != NULL) {
// Update the sum
sum = sum + temp->info;
temp = temp->link;
count++;
}
// If LL is empty
if (start == NULL) {
return;
}
// Else
else {
// Traverse the LL
while (current != NULL) {
index = current->link;
// If LL is empty
if (start == NULL)
printf("List is empty\n");
// Else
else {
// Traverse the LL
while (start != NULL) {
// reversing of points
t2 = start->link;
start->link = t1;
t1 = start;
start = t2;
}
start = t1;
// Print the LL
while (temp != NULL) {
printf("%d ", temp->info);
temp = temp->link;
}
}
}
// Driver Code
int main()
{
int choice;
while (1) {
switch (choice) {
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
maximum();
break;
case 9:
mean();
break;
case 10:
sort();
break;
case 11:
reverseLL();
break;
case 12:
exit(1);
break;
default:
printf("Incorrect Choice\n");
}
}
return 0;
}
Output:-
Experiment 5
Aim:- Write a menu driven C program for Doubly
Linked List (DLL) for given operations:
1.) Traverse
Theory:-
int main()
{
while(choice != 7)
{
printf("\nDOUBLY LINKED LIST PROGRAM\n");
switch(choice)
{
case 1:
printf("\n Enter data of first node : ");
scanf("%d",&data);
insertAtBeginning(data);
displayList();
revdisplaylist();
break;
case 2:
printf("\n Enter data of end node : ");
scanf("%d",&data);
insertAtEnd(data);
displayList();
revdisplaylist();
break;
case 3:
printf("\n Enter the position(0 to Totalnode+1) where you
want to insert new node: ");
scanf("%d",&n);
printf("\n Enter inserting data value: ");
scanf("%d",&data);
insertAtBN(data,n);
displayList();
revdisplaylist();
break;
case 4:
DlListDeleteFirstNode();
displayList();
revdisplaylist();
break;
case 5:
DlListDeleteendNode();
displayList();
revdisplaylist();
break;
case 6:
printf(" Enter the position(1 to Totalnode) to delete a node :
");
scanf("%d",&insPlc);
DlListDeleteAnyNode(insPlc);
displayList();
revdisplaylist();
break;
case 7:
printf("Stop program.Exit ");
break;
default:
printf("\n Error! Invalid choice...Try again. ");
}
}
return 0;
}
void createList(int n)
{
int i, data;
struct node *newNode;
head->data = data;
head->prev = NULL;
head->next = NULL;
end = head;
end->next = newNode;
end = newNode;
}
}
}
void displayList()
{
temp = head;
printf("\n Data in the list:\n");
while(temp != NULL)
{
printf("Data of %d node = %d\n", n, temp->data);
n++;
//Move the current pointer to next node
temp = temp->next;
}
}
}
while(list!=head)
{
printf("%4d",list->data);
list=list->prev;
}
printf("%4d\n",list->data);
}
newNode->data = data;
newNode->next = head; // Point to next node which is currently
head
newNode->prev = NULL; // Previous node of first node is NULL
end->next = newNode;
end = newNode;
}
}
//function to insert a node at nth position of a doubly linked list
void insertAtBN(int data, int position)
{
//declaration of variables
int i;
struct node * newNode, *temp;
temp = head;
i=1;
newNode->data = data;
newNode->next = temp->next; // Connect new node with
n+1th node
newNode->prev = temp; // Connect new node with n-1th
node
if(temp->next != NULL)
{
//Connect n+1th node with new node
temp->next->prev = newNode;
}
// Connect n-1th node with new node
temp->next = newNode;
}
else
{
//If the positon is invalid
printf("\n Error! Invalid position...Try again.\n");
}
}
}
void DlListDeleteendNode()
{
struct node * NodeToDel;
if(end == NULL)
{
printf(" Delete is not possible.List is Empty.\n");
}
else
{
NodeToDel = end;
end = end->prev; // move the previous address of the end
node to 2nd end node
end->next = NULL; // set the next address of end node to
NULL
free(NodeToDel); // delete the end node
}
}
if(pos == 1)
{
DlListDeleteFirstNode();
}
else if(curNode == end)
{
DlListDeleteendNode();
}
else if(curNode != NULL)
{
curNode->prev->next= curNode->next;
curNode->next->prev = curNode->prev;
1.) Traverse
Theory:-
struct node {
int data;
struct node *next;
}*top;
void initialize() {
top = NULL;
}
int isEmpty() {
if (top == NULL)
return 1;
else
return 0;
}
int peek() {
return top->data;
}
if (head == NULL) {
printf("Error : Invalid stack pointer !!!\n");
return;
}
int length = 0;
while(head != NULL){
head = head->next;
length++;
}
return length;
}
if (top == NULL) {
top = temp;
top->next = NULL;
} else {
temp->next = top;
top = temp;
}
}
void pop() {
struct node *temp;
if (isEmpty(top)) {
printf("\nStack is Empty\n");
return;
} else {
temp = top;
top = top->next;
printf("Removed Element : %d\n", temp->data);
free(temp);
}
}
void main()
{
initialize();
push(10);
push(12);
push(31);
push(41);
printf("Stack Size : %d\n", getStackSize(top));
printf("\nTop Element : %d\n", peek());
printf("Stack as linked List\n");
printStack(top);
pop();
pop();
pop();
pop();
pop();
printStack(top);
return;
}
Output:-
Experiment 8
Aim :- Write a C program to perform following
operations on stack using linked list:
1.) Traverse the stack.
2.) Insert a data on stack.
3.) Delete the data from stack.
4.) Access the top most element from the stack.
Theory :-
1.) Push Operation : We will dynamically allocate memory for a struct node
variable(let's say temp). Then we will attach new node in front of linked
list by setting temp- >next = top. Finally set top pointer to temp.(top =
temp;)
2.) Pop Operation : Remove head node(pointed by top pointer) of the
linked list. Store the top pointer in a temp variable. Now, move top
pointer to next node(top = top->next;). Deallocate memory of temp node
using free.
3.) Peek Operation : Returns the value of head node of linked list without
removing it.(return top->data;).
Code:-
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1.Dipslay");
printf("\n 2.Push");
printf("\n 3.Pop");
printf("\n 4.Top");
printf("\n 5.Exit");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
display();
break;
case 2:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 3:
pop();
break;
case 4:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
}
break;
case 5:
exit(0);
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}
void create()
{
top = NULL;
}
void stack_count()
{
printf("\n No. of elements in stack : %d", count);
}
/* Push data into stack */
void push(int data)
{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}
void display()
{
top1 = top;
if (top1 == NULL)
{
printf("Stack is empty");
return;
}
while (top1 != NULL)
{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}
void pop()
{
top1 = top;
if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
int topelement()
{
return(top->info);
}
Output:-
Experiment 9
Aim :- Write a C program to implement queue using linked list.
Theory :- The queue is a linear data structure used to represent a linear list. It
allows insertion of an element to be done at one end and deletion of an element to
be performed at the other end.
Code:-
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
} *front, *back;
void initialize() {
front = back = NULL;
}
int getQueueSize()
{
struct node *temp = front;
int count = 0;
if(front == NULL && back == NULL)
return 0;
while(temp != back){
count++;
temp = temp->next;
}
if(temp == back)
count++;
return count;
}
int getFrontElement() {
return front->data;
}
int getBackElement() {
return back->data;
}
void isEmpty() {
if (front == NULL && back == NULL)
printf("Empty Queue\n");
else
printf("Queue is not Empty\n");
}
void enqueue(int num) {
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = num;
temp->next = NULL;
if (back == NULL) {
front = back = temp;
} else {
back->next = temp;
back = temp;
}
}
void dequeue() {
struct node *temp;
if (front == NULL) {
printf("\nQueue is Empty \n");
return;
} else {
temp = front;
front = front->next;
if(front == NULL){
back = NULL;
}
printf("Removed Element : %d\n", temp->data);
free(temp);
}
}
void printQueue() {
struct node *temp = front;
Output:-
Experiment 10
Aim:- Write a C program to perform following
operations on Queue using linked list:
1.) Traverse the queue.
2.) Insert a data value.
3.) Delete the data value.
4.) Access the front item from the queue.
5.) Access the last item from the queue.
Theory :- Queue is type of data structure in which first come first out rule
held.
Code :-
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;
int frontelement();
void enq(int data);
void deq();
void empty();
void display(); //traverse
void create();
void queuesize();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1.Enque");
printf("\n 2.Deque");
printf("\n 3.Front element");
printf("\n 4.Empty");
printf("\n 5.Exit");
printf("\n 6.Display");
printf("\n 7.Queue size");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
printf("Front element : %d", e);
else
printf("\n No front element in Queue as queue is empty");
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
void create()
{
front = rear = NULL;
}
void queuesize()
{
printf("\n Queue size : %d", count);
}
void enq(int data)
{
if (rear == NULL)
{
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;
}
else
{
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;
rear = temp;
}
count++;
}
void display()
{
front1 = front;
if ((front1 == NULL) && (rear == NULL))
{
printf("Queue is empty");
return;
}
while (front1 != rear)
{
printf("%d ", front1->info);
front1 = front1->ptr;
}
if (front1 == rear)
printf("%d", front1->info);
}
void deq()
{
front1 = front;
if (front1 == NULL)
{
printf("\n Error: Trying to display elements from empty queue");
return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("\n Dequed value : %d", front->info);
free(front);
front = front1;
}
else
{
printf("\n Dequed value : %d", front->info);
free(front);
front = NULL;
rear = NULL;
}
count--;
}
int frontelement()
{
if ((front != NULL) && (rear != NULL))
return(front->info);
else
return 0;
}
void empty()
{
if ((front == NULL) && (rear == NULL))
printf("\n Queue empty");
else
printf("Queue not empty");
}
Output:-