Data Structure Lab Manual
Data Structure Lab Manual
2. Write a C program
to implement queue
20/11/2021
using array with the
following
operations:
enqueue(),
dequeue(),
display(), peek().
3. Write a C program
to implement
20/11/2021
circular queue using
array.
4. Write a C
program to
20/11/2021
implement
queue using
link list with
the
following
operations:
enqueue(),
dequeue(),
display(),
peek().
5. Write a C
program to
20/11/2021
implement
stack using
array with
the
following
operations:
push(),
pop(),
display(),
peek().
6. Write a C
program to
20/11/2021
implement
stack using
link list with
the
following
operations:
push(),
pop(),
display(),
peek()
7. Write a C
program to
20/11/2021
create,
insert,
delete, count
and display
elements in a
singly link
list.
(consider all
cases - at
beg, end, at
specific
position)
8. Write a C
program to
20/11/2021
create, insert,
delete, count
and display
elements in a
doubly link list.
(consider all
cases - at beg,
end, at specific
position)
9. Write a C
program to
20/11/2021
insert,
delete, count
and display
elements in a
circular link
list.
(consider all
cases - at
beg, end, at a
position)
10. Write a C
program to
20/11/2021
reverse a
singly linked
list.
11. Write a C
program to
20/11/2021
implement
the concept
of Linear
search.
12. Write a C
program to
20/11/2021
implement
the concept
of Binary
search.
13. Write a C
program to
20/11/2021
implement
the concept
of Bubble
sort.
14. Write a C
program to
20/11/2021
implement
the concept
of Insertion
sort.
arr[p-1] = e;
return 0;
}
2. Write a C program to implement queue using array with the following
operations: enqueue(), dequeue(), display(), peek().
#include<stdio.h>
#include<stdlib.h>
struct queue
{
int size;
int f;
int r;
int* arr;
};
int main(){
struct queue q;
q.size = 4;
q.f = q.r = 0;
q.arr = (int*) malloc(q.size*sizeof(int));
if(isEmpty(&q)){
printf("Queue is empty\n");
}
if(isFull(&q)){
printf("Queue is full\n");
}
return 0;
}
void display(){
int f = front, r = rear;
if (front == -1) {
printf("Queue is empty");
return;
}
printf("Queue elements are :\n");
if (f <= r) {
while (f <= r){
printf("%d", cqueue[f]);
f++;
}
}
else {
while (f <= n - 1) {
printf("%d",cqueue[f]);
f++;
}
f = 0;
while (f <= r) {
printf("%d",cqueue[f]);
f++;
}
}
}
int menu(){
int choice;
printf("\n 1.Add value to the list");
printf("\n 2. Delete value to the list");
printf("\n 3. Travesre/View List");
printf("\n 4. exit");
printf("\n Please enter your choice: \t");
scanf("%d",&choice);
return(choice);
}
void main(){
int value;
while(1){
switch(menu()){
case 1:
printf("Input for insertion: ");
scanf("%d",&value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("invalid choice");
}
}
getch();
}
4. Write a C program to implement queue using link list with the following
operations: enqueue(), dequeue(), display(), peek().
#include <stdio.h>
#include <stdlib.h>
struct QNode {
int key;
struct QNode* next;
};
struct Queue {
struct QNode *front, *rear;
};
struct QNode* newNode(int k)
{
struct QNode* temp = (struct QNode*)malloc(sizeof(struct QNode));
temp->key = k;
temp->next = NULL;
return temp;
}
struct Queue* createQueue()
{
struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}
void enQueue(struct Queue* q, int k)
{
struct QNode* temp = newNode(k);
if (q->rear == NULL) {
q->front = q->rear = temp;
return;
}
q->rear->next = temp;
q->rear = temp;
}
void deQueue(struct Queue* q)
{
if (q->front == NULL)
return;
struct QNode* temp = q->front;
q->front = q->front->next;
if (q->front == NULL)
q->rear = NULL;
free(temp);
}
int main()
{
struct Queue* q = createQueue();
enQueue(q, 10);
enQueue(q, 20);
deQueue(q);
deQueue(q);
enQueue(q, 30);
enQueue(q, 40);
enQueue(q, 50);
deQueue(q);
printf("Queue Front : %d \n", q->front->key);
printf("Queue Rear : %d", q->rear->key);
return 0;
}
5. Write a C program to implement stack using array with the following
operations: push(), pop(), display(), peek().
#include <stdio.h>
#include <stdlib.h>
struct stack
{
int size;
int top;
int *arr;
}
//to check if empty
int isEmpty(struct stack* ptr)
{
if (ptr->top == -1)
{
printf ("the stack is empty\n");
return 1;
}
else
{
return 0;
}
}
int main ()
{
struct stack *sp = (struct stack *) malloc (sizeof (struct stack));
sp->size = 10;
sp->top = -1;
sp->arr = (int *) malloc (sp->size * sizeof (int));
printf ("stack has been created successfully\n");
printf ("before pushing,full:%d\n", isFull (sp));
printf ("before popping,empty:%d\n", isEmpty (sp));
push (sp, 1);
push (sp, 10);
push (sp, 11);
push (sp, 23);
push (sp, 45);
push (sp, 56);
push (sp, 78);
push (sp, 101);
push (sp, 33);
push (sp, 61);
push (sp, 70);
printf ("after pushing,full:%d\n", isFull (sp));
printf ("after popping,full:%d\n", isEmpty (sp));
printf ("popped%d from the stack\n", pop (sp));
for (int j = 1; j <= sp->top + 1; j++)
{
printf ("the value at position %d is %d\n", j, peek (sp, j));
}
printf ("Displaying elements of the stack -\n");
display ();
return 0;
}
6. Write a C program to implement stack using link list with the following
operations: push(), pop(), display(), peek().
7. Write a C program to create, insert, delete, count and display elements in a
singly link list. (consider all cases - at beg, end, at specific position)
8. Write a C program to create, insert, delete, count and display elements in a
doubly link list. (consider all cases - at beg, end, at specific position)
9. Write a C program to insert, delete, count and display elements in a circular
link list. (consider all cases - at beg, end, at a position)
10. Write a C program to reverse a singly linked list.
11. Write a C program to implement the concept of Linear search.
#include <stdio.h>
int linearSearch(int arr[],int sizearr,int element){
for(int i=0;i<sizearr;i++)
{
if(arr[i]==element){
return i;
}
}
return -1;
}
int main()
{
int arr[]={23,45,6,7,89,90,34,5,7,123,67,89};
int sizearr=sizeof(arr)/sizeof(int);
int element=6;
int searchIndex=linearSearch(arr,sizearr,element);
printf("the element %d was found at %d\n",element,searchIndex);
return 0;
}
12. Write a C program to implement the concept of Binary search.
#include <stdio.h>
}
13. Write a C program to implement the concept of Bubble sort.
#include <stdio.h>
int main()
scanf("%d", &num);
scanf("%d", &a[i]);
temp = a[j];
printf("\n");
return 0;