0% found this document useful (0 votes)
15 views6 pages

Linked List Stack and Queue Implementation

M3

Uploaded by

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

Linked List Stack and Queue Implementation

M3

Uploaded by

kavalesiddhi96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

B4:Stack or Queue using Linked List (Dynamic Implementation)

Design a service window system where customers arrive and are served in
order (FIFO), or a browser history system where the last visited page is
accessed first (LIFO). Use a linked list to implement a dynamic stack (push,
pop, display) or queue (add, delete, display) based on the given use case.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Structure definition for a linked list node
struct Node {
char data[50]; // To store name or webpage title
struct Node *next; // Pointer to next node
};
// Front and rear pointers for Queue
struct Node *front = NULL, *rear = NULL;
// Top pointer for Stack
struct Node *top = NULL;
/* ---------------- QUEUE FUNCTIONS (FIFO) ---------------- */
// Add customer to queue
void enqueue(char *name) {
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
strcpy(newNode->data, name);
newNode->next = NULL;
if (rear == NULL) { // If queue is empty
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
printf("Added '%s' to the service queue.\n", name);
}
// Delete customer from queue (serve next customer)
void dequeue() {
if (front == NULL) {
printf("Queue is empty! No customer to serve.\n");
return;
}
struct Node *temp = front;
printf("Serving customer: %s\n", front->data);
front = front->next;
if (front == NULL)
rear = NULL;
free(temp);
}
// Display queue (customers waiting)
void displayQueue() {
struct Node *temp = front;
if (temp == NULL) {
printf("Queue is empty!\n");
return;
}
printf("Customers in queue: ");
while (temp != NULL) {
printf("%s ", temp->data);
temp = temp->next;
}
printf("\n");
}
/* ---------------- STACK FUNCTIONS (LIFO) ---------------- */
// Push webpage onto stack
void push(char *page) {
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
strcpy(newNode->data, page);
newNode->next = top;
top = newNode;
printf("Visited page: %s\n", page);
}
// Pop the last visited page
void pop() {
if (top == NULL) {
printf("History is empty! No page to go back.\n");
return;
}
struct Node *temp = top;
printf("Going back from: %s\n", top->data);
top = top->next;
free(temp);
}
// Display browser history
void displayStack() {
struct Node *temp = top;
if (temp == NULL) {
printf("No pages in history.\n");
return;
}
printf("Browser History (Last visited first): ");
while (temp != NULL) {
printf("%s ", temp->data);
temp = temp->next;
}
printf("\n");
}

/* ---------------- MAIN FUNCTION ---------------- */


int main() {
int choice, subChoice;
char name[50];
printf("=== Service Window / Browser History System ===\n");
printf("Choose System Type:\n");
printf("1. Service Window (Queue - FIFO)\n");
printf("2. Browser History (Stack - LIFO)\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 1) {
// Queue-based system
do {
printf("\n--- Service Window System (Queue) ---\n");
printf("1. Add Customer\n2. Serve Customer\n3. Display Queue\n4.
Exit\n");
printf("Enter your choice: ");
scanf("%d", &subChoice);
switch (subChoice) {
case 1:
printf("Enter customer name: ");
scanf("%s", name);
enqueue(name);
break;
case 2:
dequeue();
break;
case 3:
displayQueue();
break;
case 4:
printf("Exiting Service Window System...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (subChoice != 4);
} else if (choice == 2) {
// Stack-based system
do {
printf("\n--- Browser History System (Stack) ---\n");
printf("1. Visit Page (Push)\n2. Go Back (Pop)\n3. Show History\n4.
Exit\n");
printf("Enter your choice: ");
scanf("%d", &subChoice);

switch (subChoice) {
case 1:
printf("Enter page name: ");
scanf("%s", name);
push(name);
break;
case 2:
pop();
break;
case 3:
displayStack();
break;
case 4:
printf("Exiting Browser History System...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (subChoice != 4);
} else {
printf("Invalid selection!\n");
}
return 0;
}

You might also like