Program code: 1
#include <stdio.h>
#include <stdlib.h>
#define MAX 5 // Adjust as needed
Int stack[MAX];
Int top = -1;
// Utility checks
Int isEmpty(void) { return top == -1;
}
Int isFull(void) { return top == MAX – 1;
}
// Operations
Void push(int value) {
If (isFull()) {
Printf(“Error: Stack OVERFLOW. Cannot push
%d\n”, value);
Return;
}
Stack[++top] = value;
Printf(“%d pushed.\n”, value);
}
Int pop(void) {
If (isEmpty()) {
Printf(“Error: Stack UNDERFLOW. Nothing to
pop.\n”);
Return -1; // sentinel
}
Return stack[top--];
}
Int peek(void) {
If (isEmpty()) {
Printf(“Stack is EMPTY. No top element.\n”);
Return -1; // sentinel
}
Return stack[top];
}
Void display(void) {
If (isEmpty()) {
Printf(“Stack is EMPTY.\n”);
Return;
}
Printf(“Stack (top -> bottom): “);
For (int I = top; I >= 0; --i) {
Printf(“%d “, stack[i]);
}
Printf(“\n”);
}
Int main(void) {
Int choice, value, popped;
While (1) {
Printf(“\n--- STACK MENU ---\n”);
Printf(“1. Push\n2. Pop\n3. Peek\n4. Display\n5.
Exit\n”);
Printf(“Enter your choice: “);
If (scanf(“%d”, &choice) != 1) {
// Clear invalid input
Printf(“Invalid input. Exiting.\n”);
Break;
}
Switch (choice) {
Case 1:
Printf(“Enter value to push: “);
If (scanf(“%d”, &value) != 1) {
Printf(“Invalid value.\n”);
// flush stdin in real apps if needed
Exit(0);
}
Push(value);
Break;
Case 2:
Popped = pop();
If (popped != -1) {
Printf(“Popped: %d\n”, popped);
}
Break;
Case 3: {
Int t = peek();
If (t != -1) printf(“Top element: %d\n”, t);
Break;
}
Case 4:
Display();
Break;
Case 5:
Printf(“Exiting…\n”);
Return 0;
Default:
Printf(“Invalid choice. Try again.\n”);
}
}
Return 0;
}
Output:-
--- STACK MENU ---
[Link]
[Link]
[Link]
[Link]
[Link]
Enter your choice: 1
Enter value to push: 10
10 pushed.
Enter your choice: 1
Enter value to push: 20
20 pushed.
Enter your choice: 4
Stack (top -> bottom): 20 10
Enter your choice: 3
Top element: 20
Enter your choice: 2
Popped: 20
Enter your choice: 4
Stack (top -> bottom): 10
Enter your choice: 5
Exiting…
Program code: 2
#include <stdio.h>
#include <ctype.h> // for isdigit()
#include <stdlib.h> // for atoi()
#define MAX 100
int stack[MAX];
int top = -1;
// Push an element to stack
void push(int value) {
if (top == MAX - 1) {
printf("Stack Overflow!\n");
return;
}
stack[++top] = value;
}
// Pop an element from stack
int pop() {
if (top == -1) {
printf("Stack Underflow!\n");
return -1;
}
return stack[top--];
}
// Evaluate postfix expression
int evaluatePostfix(char exp[]) {
int i;
for (i = 0; exp[i] != '\0'; i++) {
char ch = exp[i];
// If character is a digit, push to stack
if (isdigit(ch)) {
push(ch - '0'); // Convert char to int }
// If operator, pop two elements and apply
operation
else {
int val1 = pop();
int val2 = pop();
switch (ch) {
case '+': push(val2 + val1); break;
case '-': push(val2 - val1); break;
case '*': push(val2 * val1); break;
case '/': push(val2 / val1); break;
}
}
}
return pop(); // Final result
}
int main() {
char exp[100];
printf("Enter a postfix expression: ");
scanf("%s", exp);
int result = evaluatePostfix(exp);
printf("Result of postfix evaluation: %d\n",
result);
return 0;
}
Output:-
Enter a postfix expression: 53+82-*
Result of postfix evaluation: 48
Enter a postfix expression: 23*54*+9-
Result of postfix evaluation: 17
Program Code (C): 3
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h> // for isalnum
#include <string.h> // for strlen
#define MAX 100
char stack[MAX];
int top = -1;
// Push onto stack
void push(char c) {
if (top == MAX - 1) {
printf("Stack Overflow!\n");
return;
}
stack[++top] = c;
}
// Pop from stack
char pop() {
if (top == -1) {
return -1; // sentinel
}
return stack[top--];
}
// Peek top element
char peek() {
if (top == -1) return -1;
return stack[top];
}
// Check precedence of operators
int precedence(char op) {
switch (op) {
case '^': return 3;
case '*':
case '/': return 2;
case '+':
case '-': return 1;
default: return 0;
}
}
// Infix to Postfix Conversion
void infixToPostfix(char infix[], char postfix[]) {
int i, k = 0;
char ch;
for (i = 0; infix[i] != '\0'; i++) {
ch = infix[i];
// If operand → add to output
if (isalnum(ch)) {
postfix[k++] = ch;
}
// If '(' → push
else if (ch == '(') {
push(ch);
}
// If ')' → pop until '('
else if (ch == ')') {
while (top != -1 && peek() != '(') {
postfix[k++] = pop();
}
pop(); // remove '('
}
// Operator
else {
while (top != -1 && precedence(peek()) >=
precedence(ch)) {
postfix[k++] = pop();
}
push(ch);
}
}
// Pop remaining operators
while (top != -1) {
postfix[k++] = pop();
}
postfix[k] = '\0'; // Null-terminate string
}
int main() {
char infix[MAX], postfix[MAX];
printf("Enter an infix expression: ");
scanf("%s", infix);
infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);
return 0;}
Output:
Enter an infix expression: (A+B)*C
Postfix expression: AB+C*
Enter an infix expression: A+B*C
Postfix expression: ABC*+
Enter an infix expression: (5+3)*(8-2)
Postfix expression: 53+82-*
Program Code (C):- 4
#include <stdio.h>
#define SIZE 5 // Maximum size of Circular
Queue
int queue[SIZE];
int front = -1, rear = -1;
// Check if queue is full
int isFull() {
return ((rear + 1) % SIZE == front);
}
// Check if queue is empty
int isEmpty() {
return (front == -1);
}
// Enqueue operation
void enqueue(int value) {
if (isFull()) {
printf("Queue Overflow! Cannot insert %d\n",
value);
return;
}
if (isEmpty()) {
front = rear = 0; // First element
} else {
rear = (rear + 1) % SIZE;
}
queue[rear] = value;
printf("%d inserted into queue.\n", value);
}
// Dequeue operation
int dequeue() {
if (isEmpty()) {
printf("Queue Underflow! No element to delete.\
n");
return -1;
}
int data = queue[front];
if (front == rear) {
front = rear = -1; // Queue becomes empty
} else {
front = (front + 1) % SIZE;
}
return data;
}
// Peek operation
int peek() {
if (isEmpty()) {
printf("Queue is Empty.\n");
return -1;
}
return queue[front];
}
// Display queue
void display() {
if (isEmpty()) {
printf("Queue is Empty.\n");
return;
}
printf("Queue elements: ");
int i = front;
while (1) {
printf("%d ", queue[i]);
if (i == rear) break;
i = (i + 1) % SIZE;
}
printf("\n");
}
// Main function
int main() {
int choice, value;
while (1) {
printf("\n--- Circular Queue Menu ---\n");
printf("1. Enqueue\n2. Dequeue\n3. Peek\n4.
Display\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
value = dequeue();
if (value != -1)
printf("Deleted: %d\n", value);
break;
case 3:
value = peek();
if (value != -1)
printf("Front element: %d\n", value);
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice! Try again.\n");
}
}
}
Output
--- Circular Queue Menu ---
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter value to insert: 10
10 inserted into queue.
Enter your choice: 1
Enter value to insert: 20
20 inserted into queue.
Enter your choice: 4
Queue elements: 10 20
Enter your choice: 2
Deleted: 10
Enter your choice: 1
Enter value to insert: 30
30 inserted into queue.
Enter your choice: 1
Enter value to insert: 40
40 inserted into queue.
Enter your choice: 1
Enter value to insert: 50
50 inserted into queue.
Enter your choice: 1
Enter value to insert: 60
Queue Overflow! Cannot insert 60
Enter your choice: 4
Queue elements: 20 30 40 50
Program Code (C):- 5
#include <stdio.h>
#define SIZE 5
int deque[SIZE];
int front = -1, rear = -1;
// Check if deque is full
int isFull() {
return ((front == 0 && rear == SIZE - 1) ||
(front == rear + 1));
}
// Check if deque is empty
int isEmpty() {
return (front == -1);
}
// Insert at front
void insertFront(int value) {
if (isFull()) {
printf("Deque Overflow! Cannot insert %d at
front\n", value);
return;
}
if (isEmpty()) {
front = rear = 0;
} else if (front == 0) {
front = SIZE - 1;
} else {
front--;
}
deque[front] = value;
printf("%d inserted at front.\n", value);
}
// Insert at rear
void insertRear(int value) {
if (isFull()) {
printf("Deque Overflow! Cannot insert %d at
rear\n", value);
return;
}
if (isEmpty()) {
front = rear = 0;
} else if (rear == SIZE - 1) {
rear = 0;
} else {
rear++;
}
deque[rear] = value;
printf("%d inserted at rear.\n", value);
}
// Delete from front
void deleteFront() {
if (isEmpty()) {
printf("Deque Underflow! Cannot delete from
front.\n");
return;
}
printf("%d deleted from front.\n", deque[front]);
if (front == rear) {
front = rear = -1;
} else if (front == SIZE - 1) {
front = 0;
} else {
front++;
}
}
// Delete from rear
void deleteRear() {
if (isEmpty()) {
printf("Deque Underflow! Cannot delete from
rear.\n");
return;
}
printf("%d deleted from rear.\n", deque[rear]);
if (front == rear) {
front = rear = -1;
} else if (rear == 0) {
rear = SIZE - 1;
} else {
rear--;
}
}
// Display deque
void display() {
if (isEmpty()) {
printf("Deque is Empty.\n");
return;
}
printf("Deque elements: ");
int i = front;
while (1) {
printf("%d ", deque[i]);
if (i == rear) break;
i = (i + 1) % SIZE;
}
printf("\n");
}
// Main function
int main() {
int choice, value;
while (1) {
printf("\n--- DEQUE MENU ---\n");
printf("1. Insert Front\n2. Insert Rear\n3. Delete
Front\n4.
Delete Rear\n5. Display\n6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value: ");
scanf("%d", &value);
insertFront(value);
break;
case 2:
printf("Enter value: ");
scanf("%d", &value);
insertRear(value);
break;
case 3:
deleteFront();
break;
case 4:
deleteRear();
break;
case 5:
display();
break;
case 6:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice! Try again.\n");
}
}
}
Output:
--- DEQUE MENU ---
1. Insert Front
2. Insert Rear
3. Delete Front
4. Delete Rear
5. Display
6. Exit
Enter your choice: 1
Enter value: 10
10 inserted at front.
Enter your choice: 2
Enter value: 20
20 inserted at rear.
Enter your choice: 2
Enter value: 30
30 inserted at rear.
Enter your choice: 5
Deque elements: 10 20 30
Enter your choice: 3
10 deleted from front.
Enter your choice: 4
30 deleted from rear.
Enter your choice: 5
Deque elements: 20
Program Code (C) 6
#include <stdio.h>
#define SIZE 100
// Queue structure
typedef struct {
int arr[SIZE];
int front, rear;
} Queue;
// Initialize queue
void initQueue(Queue *q) {
q->front = q->rear = -1;
}
// Check if empty
int isEmpty(Queue *q) {
return q->front == -1;
}
// Enqueue
void enqueue(Queue *q, int val) {
if (q->rear == SIZE - 1) {
printf("Queue Overflow!\n");
return;
}
if (q->front == -1) q->front = 0;
q->arr[++q->rear] = val;
}
// Dequeue
int dequeue(Queue *q) {
if (isEmpty(q)) {
printf("Queue Underflow!\n");
return -1;
}
int val = q->arr[q->front];
if (q->front == q->rear) {
q->front = q->rear = -1;
} else {
q->front++;
}
return val;
}
// Stack using two queues
Queue q1, q2;
// Push operation (O(1))
void push(int val) {
enqueue(&q1, val);
printf("%d pushed to stack.\n", val);
}
// Pop operation (O(n))
int pop() {
if (isEmpty(&q1)) {
printf("Stack Underflow!\n");
return -1;
}
// Move elements except last from q1 to q2
while ([Link] != [Link]) {
enqueue(&q2, dequeue(&q1));
}
// Last element = popped
int popped = dequeue(&q1);
// Swap q1 and q2
Queue temp = q1;
q1 = q2;
q2 = temp;
return popped;
}
// Peek operation (top element)
int peek() {
if (isEmpty(&q1)) {
printf("Stack is empty.\n");
return -1;
}
int topElement;
while ([Link] != [Link]) {
enqueue(&q2, dequeue(&q1));
}
topElement = dequeue(&q1);
printf("Top element: %d\n", topElement);
enqueue(&q2, topElement); // put it back
Queue temp = q1;
q1 = q2;
q2 = temp;
return topElement;
}
// Display stack
void display() {
if (isEmpty(&q1)) {
printf("Stack is empty.\n");
return;
}
printf("Stack elements (bottom -> top): ");
for (int i = [Link]; i <= [Link]; i++) {
printf("%d ", [Link][i]);
}
printf("\n");
}
// Main function
int main() {
initQueue(&q1);
initQueue(&q2);
int choice, val;
while (1) {
printf("\n--- STACK USING TWO QUEUES ---\n");
printf("1. Push\n2. Pop\n3. Peek\n4. Display\n5.
Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value: ");
scanf("%d", &val);
push(val);
break;
case 2:
val = pop();
if (val != -1)
printf("Popped: %d\n", val);
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice!\n");
}
}
}
Output:
--- STACK USING TWO QUEUES ---
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter choice: 1
Enter value: 10
10 pushed to stack.
Enter choice: 1
Enter value: 20
20 pushed to stack.
Enter choice: 1
Enter value: 30
30 pushed to stack.
Enter choice: 4
Stack elements (bottom -> top): 10 20 30
Enter choice: 3
Top element: 30
Enter choice: 2
Popped: 30
Enter choice: 4
Stack elements (bottom -> top): 10 20
Program Code (C) 7
#include <stdio.h>
#define SIZE 100
// Queue structure
typedef struct {
int arr[SIZE];
int front, rear;
} Queue;
// Initialize queue
void initQueue(Queue *q) {
q->front = q->rear = -1;
}
// Check if empty
int isEmpty(Queue *q) {
return q->front == -1;
}
// Enqueue
void enqueue(Queue *q, int val) {
if (q->rear == SIZE - 1) {
printf("Queue Overflow!\n");
return;
}
if (q->front == -1) q->front = 0;
q->arr[++q->rear] = val;
}
// Dequeue
int dequeue(Queue *q) {
if (isEmpty(q)) {
printf("Queue Underflow!\n");
return -1;
}
int val = q->arr[q->front];
if (q->front == q->rear) {
q->front = q->rear = -1;
} else {
q->front++;
}
return val;
}
// Front element
int frontElement(Queue *q) {
if (isEmpty(q)) return -1;
return q->arr[q->front];
}
// Two queues for stack
Queue q1, q2;
// Push operation (O(n))
void push(int val) {
// Step 1: Enqueue new element into q2
enqueue(&q2, val);
// Step 2: Move all elements from q1 to q2
while (!isEmpty(&q1)) {
enqueue(&q2, dequeue(&q1));
}
// Step 3: Swap q1 and q2
Queue temp = q1;
q1 = q2;
q2 = temp;
printf("%d pushed to stack.\n", val);
}
// Pop operation (O(1))
int pop() {
if (isEmpty(&q1)) {
printf("Stack Underflow!\n");
return -1;
}
return dequeue(&q1);
}
// Peek operation
int peek() {
if (isEmpty(&q1)) {
printf("Stack is empty.\n");
return -1;
}
return frontElement(&q1);
}
// Display stack
void display() {
if (isEmpty(&q1)) {
printf("Stack is empty.\n");
return;
}
printf("Stack elements (top -> bottom): ");
for (int i = [Link]; i <= [Link]; i++) {
printf("%d ", [Link][i]);
}
printf("\n");
}
// Main function
int main() {
initQueue(&q1);
initQueue(&q2);
int choice, val;
while (1) {
printf("\n--- STACK USING TWO QUEUES (Costly
Push) ---\n");
printf("1. Push\n2. Pop\n3. Peek\n4. Display\n5.
Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value: ");
scanf("%d", &val);
push(val);
break;
case 2:
val = pop();
if (val != -1)
printf("Popped: %d\n", val);
break;
case 3:
val = peek();
if (val != -1)
printf("Top element: %d\n", val);
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice!\n");
}
}
}
Output:
--- STACK USING TWO QUEUES (Costly Push) ---
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter choice: 1
Enter value: 10
10 pushed to stack.
Enter choice: 1
Enter value: 20
20 pushed to stack.
Enter choice: 1
Enter value: 30
30 pushed to stack.
Enter choice: 4
Stack elements (top -> bottom): 30 20 10
Enter choice: 3
Top element: 30
Enter choice: 2
Popped: 30
Enter choice: 4
Stack elements (top -> bottom): 20 10
Program Code (C) 8
#include <stdio.h>
#define SIZE 100
// Stack implementation
typedef struct {
int arr[SIZE];
int top;
} Stack;
void init(Stack *s) {
s->top = -1;
}
int isEmpty(Stack *s) {
return s->top == -1;
}
int isFull(Stack *s) {
return s->top == SIZE - 1;
}
void push(Stack *s, int val) {
if (isFull(s)) {
printf("Stack Overflow!\n");
return;
}
s->arr[++s->top] = val;
}
int pop(Stack *s) {
if (isEmpty(s)) {
printf("Stack Underflow!\n");
return -1;
}
return s->arr[s->top--];
}
int peek(Stack *s) {
if (isEmpty(s)) return -1;
return s->arr[s->top];
}
// Two stacks for queue
Stack s1, s2;
// Enqueue operation (O(1))
void enqueue(int val) {
push(&s1, val);
printf("%d enqueued to queue.\n", val);
}
// Dequeue operation (O(n))
int dequeue() {
if (isEmpty(&s1)) {
printf("Queue Underflow!\n");
return -1;
}
// Move all elements from s1 to s2
while (!isEmpty(&s1)) {
push(&s2, pop(&s1));
}
// Pop from s2 (front of queue)
int front = pop(&s2);
// Move back elements to s1
while (!isEmpty(&s2)) {
push(&s1, pop(&s2));
}
return front;
}
// Display queue
void display() {
if (isEmpty(&s1)) {
printf("Queue is empty.\n");
return;
}
printf("Queue elements (front -> rear): ");
for (int i = 0; i <= [Link]; i++) {
printf("%d ", [Link][i]);
}
printf("\n");
}
// Main function
int main() {
init(&s1);
init(&s2);
int choice, val;
while (1) {
printf("\n--- QUEUE USING TWO STACKS (Costly
Dequeue) ---\n");
printf("1. Enqueue\n2. Dequeue\n3. Display\n4.
Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value: ");
scanf("%d", &val);
enqueue(val);
break;
case 2:
val = dequeue();
if (val != -1)
printf("Dequeued: %d\n", val);
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice!\n");
}
}
}
Output:
--- QUEUE USING TWO STACKS (Costly Dequeue)
---
[Link]
[Link]
[Link]
[Link]
Enter choice: 1
Enter value: 10
10 enqueued to queue.
Enter choice: 1
Enter value: 20
20 enqueued to queue.
Enter choice: 1
Enter value: 30
30 enqueued to queue.
Enter choice: 3
Queue elements (front -> rear): 10 20 30
Enter choice: 2
Dequeued: 10
Enter choice: 3
Queue elements (front -> rear): 20 30
Program Code (C) 9
(a) Singly Linked List
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* next;
};
struct Node* head = NULL;
// Insert at end
void insertEnd(int value) {
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
printf("%d inserted.\n", value);
}
// Delete from beginning
void deleteBegin() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = head;
head = head->next;
printf("%d deleted.\n", temp->data);
free(temp);
}
// Display list
void display() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = head;
printf("Singly Linked List: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
insertEnd(10);
insertEnd(20);
insertEnd(30);
display();
deleteBegin();
display();
return 0;
}
(b) Doubly Linked List:
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
struct Node* head = NULL;
// Insert at end
void insertEnd(int value) {
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
newNode->prev = NULL;
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
printf("%d inserted.\n", value);
}
// Display list forward
void displayForward() {
struct Node* temp = head;
printf("DLL (Forward): ");
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
// Display list backward
void displayBackward() {
if (head == NULL) return;
struct Node* temp = head;
while (temp->next != NULL) temp = temp-
>next;
printf("DLL (Backward): ");
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->prev;
}
printf("NULL\n");
}
int main() {
insertEnd(5);
insertEnd(15);
insertEnd(25);
displayForward();
displayBackward();
return 0;
}
(c) Circular Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* last = NULL;
// Insert at end
void insertEnd(int value) {
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = value;
if (last == NULL) {
last = newNode;
newNode->next = last;
} else {
newNode->next = last->next;
last->next = newNode;
last = newNode;
}
printf("%d inserted.\n", value);
}
// Display circular list
void display() {
if (last == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = last->next;
printf("Circular List: ");
do {
printf("%d -> ", temp->data);
temp = temp->next;
} while (temp != last->next);
printf("(back to start)\n");
}
int main() {
insertEnd(100);
insertEnd(200);
insertEnd(300);
display();
return 0;
}
Output:
Singly Linked List:
10 -> 20 -> 30 -> NULL
10 deleted.
20 -> 30 -> NULL
DLL (Forward): 5 <-> 15 <-> 25 <-> NULL
DLL (Backward): 25 <-> 15 <-> 5 <-> NULL
Circular List: 100 -> 200 -> 300 -> (back to
start)
Program Code (C) 10
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* next;
};
struct Node* top = NULL; // stack top
// Push operation
void push(int value) {
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Heap Overflow! Cannot allocate
memory.\n");
return;
}
newNode->data = value;
newNode->next = top;
top = newNode;
printf("%d pushed to stack.\n", value);
}
// Pop operation
int pop() {
if (top == NULL) {
printf("Stack Underflow!\n");
return -1;
}
struct Node* temp = top;
int popped = temp->data;
top = top->next;
free(temp);
return popped;
}
// Peek operation
int peek() {
if (top == NULL) {
printf("Stack is empty.\n");
return -1;
}
return top->data;
}
// Display stack
void display() {
if (top == NULL) {
printf("Stack is empty.\n");
return;
}
struct Node* temp = top;
printf("Stack (top -> bottom): ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
// Main function
int main() {
int choice, value;
while (1) {
printf("\n--- STACK USING LINKED LIST ---\n");
printf("1. Push\n2. Pop\n3. Peek\n4. Display\n5.
Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value: ");
scanf("%d", &value);
push(value);
break;
case 2:
value = pop();
if (value != -1)
printf("Popped: %d\n", value);
break;
case 3:
value = peek();
if (value != -1)
printf("Top element: %d\n", value);
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice!\n");
}
}
}
Output:
--- STACK USING LINKED LIST ---
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter value: 10
10 pushed to stack.
Enter your choice: 1
Enter value: 20
20 pushed to stack.
Enter your choice: 1
Enter value: 30
30 pushed to stack.
Enter your choice: 4
Stack (top -> bottom): 30 20 10
Enter your choice: 3
Top element: 30
Enter your choice: 2
Popped: 30
Enter your choice: 4
Stack (top -> bottom): 20 10