You are on page 1of 46

The Technological Institute of

Textile & Sciences


Bhiwani

DSA Practical File

Submitted To: Submitted By:


Ms. Satvika Aman Singh
(Ass. Professor) 19ce013
INDEX
S.no. Name of the Experiment Page Date
1 Linear Search 1 12/10/2020
2 Binary Search through Iteration 2 12/10/2020
3 Binary Search by Recursion 3 26/10/2020
4 Array Traversal 4 26/10/2020
5 Insertion of an element in an array at an index 5 02/11/2020
6 Deletion if an element in an array at an index 6 02/11/2020
7 Matrix addition 7 09/11/2020
8 Matrix multiplication 8 09/11/2020
9 Stack Operations 9-10 16/11/2020
10 Queue Operations 11-12 16/11/2020
11 Creation of a Linked List 13-14 23/11/2020
12 Traversing a linked list 15-16 23/11/2020
13 Searching an element in linked list 17-18 07/12/2020
14 Add a node at beginning of linked list 19-21 07/12/2020
15 Add a node after a given node of linked list 22-23 14/12/2020
16 Add a node at end of linked list 24-25 14/12/2020
17 Delete a node from beginning of Linked list 26-28 21/12/2020
18 Delete a node after a given node of Linked list 29-31 21/12/2020
19 Delete a node from end of Linked list 32-34 28/12/2020
20 Linked stack 35-37 28/12/2020
21 Lined Queue 38-40 13/01/2021
Exp 1 : Linear Search
#include<stdio.h>

int main(){

int a[10], i, item, flag=0;

printf("\n Enter data in array");

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

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

printf("\n Enter element to be searched");

scanf("%d", &item);

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

if (item == a[i])

{ flag =1;

break;

if (flag == 0)

printf("Element not found");

else

printf("Element found at index = %d",i);

return 0;

Exp 2 : Binary Search through Iteration


#include<stdio.h>

int main()

int n, arr[50], first, mid, last, key, loc, i;

int flag=0;

printf("\n Enter number of elements: ");

scanf("%d", &n);

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

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

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

first=0;

last=n;

printf("\n Enter the element to be searched: ");

scanf("%d", &key);

while(first<=last)

mid=(first + last) / 2;

if(arr[mid] == key)

{ flag=1;

printf("\n Element found at location %d", mid+1);

break;

}
else if(key>arr[mid])

first=mid+1;

else

last=mid-1;

if(flag==0)

printf("\n Element not found");

Exp 3 : Binary Search through Recursion


#include<stdio.h>
int RecBinSearch(int A[], int start, int end, int element)
{
if(start>end) return -1;
int mid = (start+end)/2;
if( A[mid] == element ) return mid;
else if( element < A[mid] )
RecBinSearch(A, start, mid-1, element);
else
RecBinSearch(A, mid+1, end, element);
}
int main() {
int A[] = {0,2,6,11,12,18,34,45,55,99};
int n;
printf("\nArray A : \n");
for(int i=0;i<10;i++)
printf("%d ",A[i]);
printf("\nEnter element to be searched : ");
scanf("%d",&n);
printf("\n%d is found at index %d \n",n,RecBinSearch(A,0,9,n));
return 0;
}

Exp 4 : Array Traversal


#include<stdio.h>
int main()
{ int a[10], i;
printf("\n Enter data in array");
for(i=0; i<10; i++)
{ scanf("%d", &a[i]); }
for(i=0; i<10; i++)
{ printf(“\n%d", a[i]); }
}

Exp 5 : Insertion of an element in an array at an index


#include <stdio.h>
int main()
{ int arr[20], i, x, pos, n;
printf("\n Enter total no. of elements");
scanf("%d", &n);
if (n>20)
printf("\n Array size cannot exceed 20");
printf("\n Enter elements of an
array");
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("\n Enter Data and Index");
scanf("%d%d",&x,&pos);
n++;
for (i = n-1; i >= pos; i--)
arr[i] = arr[i - 1];
arr[pos - 1] = x;
for (i = 0; i < n; i++)
printf("\n%d ", arr[i]);
return 0;
}

Exp 6 : Deletion of an element in an array at an index


#include <stdio.h>
int main()
{
int array[100], position, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete element\n");
scanf("%d", &position);
if (position >= n+1)
printf("Deletion not possible. \n");
else
{
for (c = position - 1; << n - 1; c++)
array[c] = array[c+1];
printf("Resultant array: \n");
for (c = 0; c < n - 1; C++)
printf("%d\n", array[c]);
}
return 0;
}
Exp 7 : Matrix Addition
#include <stdio.h>
int main()
{
int r,c,a[100][100],b[100][100],sum[100][100];
printf("Enter no of rows : ");
scanf("%d",&r);
printf("Enter no of columns : ");
scanf("%d",&c);
printf("Enter elements of array a : \n");
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
scanf("%d",&a[i][j]);
}
}
printf("Enter elements of array b : \n");
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
scanf("%d",&b[i][j]);
}
}
printf("Entered matrix a : \n");
for (int i = 0; i < r; ++i){
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
}
printf("\n");
}
printf("Entered matrix b : \n");
for (int i = 0; i < r; ++i){
for (int j = 0; j < c; ++j) {
printf("%d ", b[i][j]);
}
printf("\n");
}
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
sum[i][j]=a[i][j]+b[i][j];
}
}
printf("Sum of arrays\n");
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
printf("%d ",sum[i][j]);
}
printf("\n");
}
return 0;
}

Exp 8 : Matrix Multiplication


#include <stdio.h>
int main()
{
int m, n, p, q, sum = 0;
int a[10][10], b[10][10],product[10][10];
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the elements of first matrix\n");
for (int i = 0 ; i < m ; i++ )
for (int j = 0 ; j < n ; j++ )
scanf("%d", &a[i][j]);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if ( n != p )
printf("Matrices with entered orders
can't be multiplied with each other. \n ");
else
{
printf("Enter the elements of second
matrix\n");
for ( int i = 0 ; i < p ; i++ )
for ( int j = 0 ; j < q ; j++ )
scanf("%d", &b[i][j]);

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


{
for (int j= 0 ; j< q ; j++ )
{
for (int k = 0 ; k < p ; k++ )
{
sum = sum + a[i][k]*b[k][j];
}
product[i][j] = sum;
sum = 0;
}
}
printf("Entered matrix a : \n");
for (int i = 0; i < m; ++i){
for (int j = 0; j < n; ++j) {
printf("%d ", a[i][j]);
}
printf("\n");
}
printf("Entered matrix b : \n");
for (int i = 0; i < p; ++i){
for (int j = 0; j < q; ++j) {
printf("%d ", b[i][j]);
}
printf("\n");
}
printf("Product of entered matrices:-\n");
for (int i = 0 ; i < m ; i++ )
{
for (int j = 0 ; j < q ; j++ )
printf("%d ", product[i][j]);
printf("\n");
}
}
return 0;
}

Exp 9 : Stack Operations


#include<stdio.h>
int stack[20], choice, n, top, x, i;
void push(void);
void pop(void);
void display(void);
int main()
{ top=-1;
printf("\n Enter the size of STACK[MAX=20]:");
scanf("%d",&n);
printf("\n\t Enter 1.PUSH \n\t 2.POP \n\t 3.DISPLAY \n\t 4.EXIT");
do
{ printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{ case 1:
{ push();
break; }
case 2:
{ pop();
break; }
case 3:
{ display();
break; }
case 4:

{ printf("\n\t EXIT POINT ");

break;

default:

{ printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");

}
}

while(choice!=4);

return 0;

}
void push()
{
if(top>=n-1)
{
printf("\n\t STACK is over flow"); }
else
{
printf(" Enter a value to be pushed:");
scanf("%d", &x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d", stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
printf("\n The STACK is empty");
}
Exp 10 : Queue Operations

#include<stdio.h>
#define n 5
int main()
{
int queue[n], ch=1, front=0, rear=0,i, j=1, x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d", &ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:", j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{ printf("\n Deleted Element is %d", queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{ for(i=front; i<rear; i++)
{ printf("%d",queue[i]);
printf("\n");
}
break;
case 4: printf("\n\t EXIT POINT ");
break;
default: printf("Wrong Choice: please see the options");
}
}
}
return 0;
}
Experiment -11
Creation of a Linked List:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *next;
};
struct node* head = NULL,*ptr=NULL;
void addNode(int data)
{
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->info = data;
newNode->next = NULL;
if(head==NULL)
{
head = newNode;
ptr = newNode;
}
else
{
ptr->next= newNode;
ptr = newNode;
}
}
void display()
{
struct node* current=head;
if (head==NULL)
{
printf("List is empty\n");
return;
}
printf("Nodes of singly linked list:\n");
printf("\n[head] =>");
while (current!= NULL)
{
printf("%d ->", current->info );
current = current->next;
}
printf(" [null]\n");

}
int main()
{
addNode(11);
addNode(27);
addNode(36);
addNode(48);
display();
return 0;
}
Experiment -12
Traversing a Linked List:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *next;
};
struct node* head = NULL,*ptr=NULL;
void addNode(int data)
{
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->info = data;
newNode->next = NULL;
if(head==NULL)
{
head = newNode;
ptr = newNode;
}
else
{
ptr->next= newNode;
ptr = newNode;
}
}
void display()
{
struct node* current=head;
if (head==NULL)
{
printf("List is empty\n");
return;
}
printf("\n Nodes of singly linked list:\n");
printf("\n Traversal in linked list.");
printf("\n[head] =>");
while (current!= NULL)
{
printf("%d ->", current->info );
current = current->next;
}
printf(" [null]\n");
}
int main()
{
addNode(11);
addNode(27);
addNode(36);
addNode(48);
display();
return 0;
}
Experiment -13
Searching an element in linked list:
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
} * head;
int createList(int n){
struct node *newNode, *temp;
int data, i;
head = (struct node* )malloc(sizeof(struct node));
if (head == NULL){
printf("Unable to allocate memory. Exiting from app.");
exit(0);
}
printf("Enter data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for (i = 2; i <= n; i++)
{
newNode = (struct node*)malloc(sizeof(struct node *));
if (newNode == NULL)
{
printf("Unable to allocate memory. Exiting from app.");
exit(0);
}
printf("Enter data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
return 0;
}
void displayList()
{
struct node *temp;
if (head == NULL)
{
printf("List is empty.\n");
return;
}
temp = head;
while (temp != NULL)
{
printf("%d, ", temp->data);
temp = temp->next;
}
printf("\n");
}
int search(int key)
{
int index;
struct node *curNode;
index = 0;
curNode = head;
while (curNode != NULL && curNode->data != key)
{
index++;
curNode = curNode->next;
}
return (curNode != NULL) ? index : -1;
}
int main()
{
int n, keyToSearch, index;
printf("Enter number of node to create: ");
scanf("%d", &n);
createList(n);
printf("\nData in list: \n");
displayList();
printf("\nEnter element to search: ");
scanf("%d", &keyToSearch);
index = search(keyToSearch);
if (index >= 0)
printf("%d found in the list at position %d\n", keyToSearch, index + 1);
else
printf("%d not found in the list.\n", keyToSearch);
return 0;
}
Experiment -14
Add a node at beginning of linked list:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void insertNodeAtBeginning(int data)
{
struct node *newNode;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data;
newNode->next = head;
head = newNode;
printf("DATA INSERTED SUCCESSFULLY\n");
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}
int main()
{
int n, data;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("\nEnter data to insert at beginning of the list: ");
scanf("%d", &data);
insertNodeAtBeginning(data);
printf("\nData in the list \n");
displayList();
return 0;
}
Experiment -15
Add a node after a given node of linked list:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} *head;
void initialize()
{
head = NULL;
}
void insert(int num)
{
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = num;
newNode->next = head;
head = newNode;
printf("Inserted Element : %d\n", num);
}
void insertAfter(struct node* prevNode, int num)
{
if (prevNode == NULL) {
printf("Error : Invalid node pointer !!!\n");
return;
}
struct node* newNode =(struct node*) malloc(sizeof(struct node));
newNode->data = num;
newNode->next = prevNode->next;
prevNode->next = newNode;
}
void printLinkedList(struct node *nodePtr)
{
printf("\nLinked List\n");
while (nodePtr != NULL) {
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("-->");
}
}
int main()
{
initialize();
insert(2);
insert(4);
insert(5);
insert(9);
printLinkedList(head);
insertAfter(head->next->next, 8);
printf("\n\nAfter Insertion\n");
printLinkedList(head);
return 0;
}
Experiment -16
Add a node at end of linked list:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void addLast(struct node **head, int val)
{
struct node *newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = val;
newNode->next = NULL;
if(*head == NULL)
*head = newNode;
else
{
struct node *lastNode = *head;
while(lastNode->next != NULL)
{
lastNode = lastNode->next;
}
lastNode->next = newNode;
}
}
void printList(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf("%d->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main()
{
struct node *head = NULL;
addLast(&head,10);
addLast(&head,20);
addLast(&head,30);
printList(head);
addLast(&head,456);
printf("\nAfter adding 456 in last node\n");
printList(head);
return 0;
}
Experiment -17
Delete a node from beginning of linked list:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void createList(int n);
void deleteFirstNode();
void displayList();
int main()
{
int n, choice;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("\nPress 1 to delete first node: ");
scanf("%d", &choice);
if(choice == 1)
deleteFirstNode();
printf("\nData in the list \n");
displayList();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void deleteFirstNode()
{
struct node *toDelete;
if(head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
head = head->next;
printf("\nData deleted = %d\n", toDelete->data);
free(toDelete);
printf("SUCCESSFULLY DELETED FIRST NODE FROM LIST\n");
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}
Experiment -18
Delete a node after a given node of Linked list:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} *head;
void createList(int n);
void deleteMiddleNode(int position);
void displayList();
int main()
{
int n, position;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("\nEnter the node position you want to delete: ");
scanf("%d", &position);
deleteMiddleNode(position);
printf("\nData in the list \n");
displayList();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED
SUCCESSFULLY\n");
}
}
void deleteMiddleNode(int position)
{
int i;
struct node *toDelete, *prevNode;
if(head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
prevNode = head;
for(i=2; i<=position; i++)
{
prevNode = toDelete;
toDelete = toDelete->next;
if(toDelete == NULL)
break;
}
if(toDelete != NULL)
{
if(toDelete == head)
head = head->next;
prevNode->next = toDelete->next;
toDelete->next = NULL;
free(toDelete);
printf("SUCCESSFULLY DELETED NODE FROM MIDDLE OF LIST\n");
}
else
{
printf("Invalid position unable to delete.");
}
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}
Experiment -19
Delete a node from end of Linked list:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void createList(int n);
void deleteLastNode();
void displayList();
int main()
{
int n, choice;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("\nPress 1 to delete last node: ");
scanf("%d", &choice);
if(choice == 1)
deleteLastNode();
printf("\nData in the list \n");
displayList();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED
SUCCESSFULLY\n");
}
}
void deleteLastNode()
{
struct node *toDelete, *secondLastNode;
if(head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
secondLastNode = head;
while(toDelete->next != NULL)
{
secondLastNode = toDelete;
toDelete = toDelete->next;
}
if(toDelete == head)
{
head = NULL;
}
else
{
secondLastNode->next = NULL;
}
free(toDelete);
printf("SUCCESSFULLY DELETED LAST NODE OF LIST\n");
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}
Experiment -20
Linked stack:
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;
int main ()
{
int choice=0;
while(choice != 4)
{
printf("\nChose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid choice ");
}
};
}
}
void push ()
{
int val;
struct node *ptr = (struct
node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element");
}
else
{
printf("Enter the value");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;
}
printf("Item pushed");
}
}
void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}
Experiment -21
Linked Queue:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};

struct node *front;


struct node *rear;
void insert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void del()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()

{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{
printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}
int main ()
{
int choice;
while(choice != 4)
{
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}

You might also like