0% found this document useful (0 votes)
12 views

EXP-6 & 7 (Autosaved)

The document outlines exercises related to data structures, specifically focusing on queue operations using arrays and linked lists, and includes sample C programs for each implementation. It also mentions additional exercises involving stacks, binary search trees, and hashing techniques. The exercises aim to enhance understanding of data structures through practical programming tasks.

Uploaded by

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

EXP-6 & 7 (Autosaved)

The document outlines exercises related to data structures, specifically focusing on queue operations using arrays and linked lists, and includes sample C programs for each implementation. It also mentions additional exercises involving stacks, binary search trees, and hashing techniques. The exercises aim to enhance understanding of data structures through practical programming tasks.

Uploaded by

hodcsdm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Exercise 6: Queue Operations

i) Implement a queue using arrays and linked lists.

(a).Program:-

#include <stdio.h>
#include <conio.h>
#define MAX 10
int queue[MAX];
int front = -1, rear = -1;
void insert(void);
int delete_element(void);
int peek(void);
void display(void);
int main()
{
int option, val;
clrscr();
do
{
printf("\n ***** MAIN MENU ****");
printf("\n 1. Insert an element");
printf("\n 2. Delete an element");
printf("\n 3. Display the queue");
printf("\n 4. EXIT");
printf("\n Enter your option :");
scanf("%d", &option);
switch(option)
{
case 1:
insert();
break;
case 2:
val = delete_element();
if (val != -1)
printf("\n The number deleted is : %d", val);
break;
case 3:
display();
break;
}
}while(option != 4);
getch();
return 0;
}
void insert()
{
int num;
printf("\n Enter the number to be inserted in the queue :");
scanf("%d", &num);
if(rear == MAX-1)
printf("\n OVERFLOW");
else if(front == -1 && rear == -1)
front = rear = 0;
else
rear++;
queue[rear] = num;
}
int delete_element()
{
int val;
if(front == -1 || front>rear)
{
printf("\n UNDERFLOW");
return -1;
}
else
{
val = queue[front];
front++;
if(front > rear)
front = rear = -1;
return val;
}
}

void display()
{
int i;
printf("\n");
if(front == -1 || front > rear)
printf("\n QUEUE IS EMPTY");
else
{
for(i = front;i <= rear;i++)
printf("\t %d", queue[i]);
}
}
Output:-

(b):-Program:-

#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct node
{
int data;
struct node *next;
};
struct queue
{
struct node front;
struct node *rear;
};
struct queue *q;
void create_queue(struct queue *);
struct queue *insert(struct queue ,int);
struct queue *delete_element(struct queue *);
struct queue *display(struct queue *);
int main()
{
int val, option;
create_queue(q);
clrscr();
do
{
printf("\n *****MAIN MENU*****");
printf("\n 1. INSERT");
printf("\n 2. DELETE");
printf("\n 3. DISPLAY");
printf("\n 4. EXIT");
printf("\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the number to insert in the queue:");
scanf("%d", &val);
q = insert(q,val); break;
case 2:
q = delete_element(q);
break;
case 3:
q = display(q);
break;
}
}
while(option <=3);
getch();
return 0;
}
void create_queue(struct queue *q)
{
q -> rear = NULL; q -> front = NULL;
}
struct queue *insert(struct queue *q,int val)
{
struct node *ptr;
ptr = (struct node*)malloc(sizeof(struct node));
ptr -> data = val;
if(q -> front == NULL)
{
q -> front = ptr;
q -> rear = ptr;
q -> front -> next = q -> rear -> next = NULL;
}
else
{
q -> rear -> next = ptr;
q -> rear = ptr;
q -> rear -> next = NULL;
}
return q;
}
struct queue *display(struct queue *q)
{
struct node *ptr;
ptr = q -> front;
if(ptr == NULL)
printf("\n QUEUE IS EMPTY");
else
{
printf("\n");
while(ptr!=q -> rear)
{
printf("%d\t", ptr -> data);
ptr = ptr -> next;
}
printf("%d\t", ptr -> data);
}
return q;
}
struct queue *delete_element(struct queue *q)
{
struct node *ptr;
ptr = q -> front;
if(q -> front == NULL)
printf("\n UNDERFLOW");
else
{
q -> front = q -> front -> next;
printf("\n The value being deleted is : %d", ptr -> data);
free(ptr);
}
return q;
}

Output:-
ii) ii) Develop a program to simulate a simple printer queue system.

iii) iii) Solve problems involving circular queues.

Exercise 7: Stack and Queue Applications

i) Use a stack to evaluate an infix expression and convert it to postfix.


ii) ii) Create a program to determine whether a given string is a palindrome or not.
iii) iii) Implement a stack or queue to perform comparison and check for symmetry

Exercise 8: Binary Search Tree

i) Implementing a BST using Linked List.


ii) ii) Traversing of BST.

Exercise 9: Hashing

i) Implement a hash table with collision resolution techniques.


ii) ii) Write a program to implement a simple cache using hashing.

You might also like