Exp 8
Implement Stack / Linear Queue ADT using Linked List.
a)For stack using linked list
#include <stdio.h>
#include <stdlib.h>
// Node definition
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* top = NULL;
// Push operation
void push(int value) {
Node* newNode = (Node*) malloc(sizeof(Node));
newNode->data = value;
newNode->next = top;
top = newNode;
printf("Pushed %d\n", value);
}
// Pop operation
int pop() {
if (top == NULL) {
printf("Stack is empty!\n");
return -1;
}
Node* temp = top;
int value = temp->data;
top = top->next;
free(temp);
return value;
}
// Display stack
void display() {
if (top == NULL) {
printf("Stack is empty\n");
return;
}
Node* temp = top;
printf("Stack (top to bottom): ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
// Main menu
int main() {
int choice, value;
while (1) {
printf("\nStack Menu:\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
value = pop();
if (value != -1)
printf("Popped: %d\n", value);
break;
case 3:
display();
break;
case 4:
printf("Exiting Stack Program...\n");
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
Output
Stack Menu:
1. Push
2. Pop
3. Display
4. Exit
Enter choice: 1
Enter value to push: 50
Pushed 50
Enter choice: 3
Stack (top to bottom): 50 -> NULL
b) for Queue using linked list
#include <stdio.h>
#include <stdlib.h>
// Node definition
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* front = NULL;
Node* rear = NULL;
// Enqueue operation
void enqueue(int value) {
Node* newNode = (Node*) malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
printf("Enqueued %d\n", value);
}
// Dequeue operation
int dequeue() {
if (front == NULL) {
printf("Queue is empty!\n");
return -1;
}
Node* temp = front;
int value = temp->data;
front = front->next;
if (front == NULL)
rear = NULL;
free(temp);
return value;
}
// Display queue
void display() {
if (front == NULL) {
printf("Queue is empty\n");
return;
}
Node* temp = front;
printf("Queue (front to rear): ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
// Main menu
int main() {
int choice, value;
while (1) {
printf("\nQueue Menu:\n");
printf("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
value = dequeue();
if (value != -1)
printf("Dequeued: %d\n", value);
break;
case 3:
display();
break;
case 4:
printf("Exiting Queue Program...\n");
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
Output:-
Queue Menu:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: 1
Enter value to enqueue: 100
Enqueued 100
Enter choice: 3
Queue (front to rear): 100 -> NULL