Complete Practical Data Structures Assignment
Complete Practical Data Structures Assignment
1. Write a C program to implement a stack using an array. Create a menu-driven program for
that.
1. Initialize the stack with a maximum size and set top = -1.
2. Push operation:
3. Pop operation:
4. Peek operation:
#include <stdio.h>
if (top == MAX - 1)
printf("Stack Overflow\n");
else
stack[++top] = value;
}
void pop() {
if (top == -1)
printf("Stack Underflow\n");
else
void peek() {
if (top == -1)
printf("Stack is empty\n");
else
int main() {
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
return 0;
default:
printf("Invalid choice\n");
return 0;
2. Push operation:
3. Pop operation:
- Else, remove the top node and update top to the next node.
4. Peek operation:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = value;
newNode->next = top;
top = newNode;
void pop() {
if (top == NULL)
printf("Stack Underflow\n");
else {
top = top->next;
free(temp);
}
void peek() {
if (top == NULL)
printf("Stack is empty\n");
else
int main() {
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
return 0;
default:
printf("Invalid choice\n");
return 0;
2. Insertion:
- Create a new node and add it to the beginning, end, or a specific position.
3. Deletion:
4. Traversal:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = value;
newNode->next = NULL;
if (*head == NULL)
*head = newNode;
else {
temp = temp->next;
temp->next = newNode;
head = head->next;
printf("NULL\n");
int main() {
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
displayList(head);
return 0;
2. Enqueue operation:
3. Dequeue operation:
- Remove a node from the front of the queue and update the front pointer.
4. Traversal:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = value;
newNode->next = NULL;
if (rear == NULL)
else {
rear->next = newNode;
rear = newNode;
void dequeue() {
if (front == NULL)
printf("Queue Underflow\n");
else {
front = front->next;
if (front == NULL)
rear = NULL;
free(temp);
void displayQueue() {
temp = temp->next;
printf("NULL\n");
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
displayQueue();
dequeue();
displayQueue();
return 0;
5. Write a C program to implement a circular queue using an array and a linked list.
2. Enqueue operation:
3. Dequeue operation:
4. Traversal:
#include <stdio.h>
#define SIZE 5
printf("Queue Overflow\n");
else {
queue[rear] = value;
void dequeue() {
if (front == -1)
printf("Queue Underflow\n");
else {
if (front == rear)
else
void displayQueue() {
if (front == -1)
printf("Queue is empty\n");
else {
int i = front;
printf("Queue: ");
while (1) {
i = (i + 1) % SIZE;
printf("\n");
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
displayQueue();
dequeue();
displayQueue();
return 0;
6. Write a C program to insert a node at the beginning, end, and any position in a singly
linked list.
2. Insert at Beginning:
3. Insert at End:
- Traverse to the end and point the last node's next to the new node.
4. Insert at Position:
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = value;
newNode->next = *head;
*head = newNode;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL)
*head = newNode;
else {
temp = temp->next;
temp->next = newNode;
}
}
newNode->data = value;
if (position == 1) {
newNode->next = *head;
*head = newNode;
return;
temp = temp->next;
if (temp == NULL)
else {
newNode->next = temp->next;
temp->next = newNode;
head = head->next;
printf("NULL\n");
}
int main() {
insertAtBeginning(&head, 10);
insertAtEnd(&head, 20);
displayList(head);
return 0;