Experiment 5 DS Student
Experiment 5 DS Student
AIM: Implementation of Singly Linked List and Stack/Queue Operations Using Linked Lists..
5.1 Write a menu driven program to implement following operations on the singly
linked list.
(b) Insert a node at the front of the linked list.
(c) Insert a node at the end of the linked list.
(d) Insert a node such that linked list is in ascending order. (According to INFO field)
(e) Delete a first node of the linked list.
(f) Delete a node before specified position.
(g) Delete a node after specified position.
5.2 Write a program to implement stack using linked list
5.3 Write a program to implement queue using linked list.
Date:
Theory:
A linked list is a type of data structure that stores a collection of non-sequential data items. Unlike
arrays, linked lists are dynamic and their size can be changed during program execution. Each data
item in a linked list has a pointer that holds the memory address of the next data item in the list. The
data items in a linked list may not be stored in consecutive memory locations, but their pointers
make it easy to access them in any order.
A singly linked list, also known as a linear linked list, is a type of linked list in which all nodes are
connected together sequentially. Each node in a singly linked list contains data and a pointer to the
next node. The last node's pointer is set to null. The limitation of a singly linked list is that it can
only be traversed in one direction, in a forward direction.
Data Structure (3130702)
• Insert
• Insert at first position
• Insert at last position
• Insert into ordered list
• Delete
• Traverse list (Print list)
• Copy linked list
5.1 Write a menu driven program to implement following operations on the singly linked
list.
(a) Insert a node at the front of the linked list.
(b) Insert a node at the end of the linked list.
(c) Insert a node such that linked list is in ascending order. (According to INFO field)
(d) Delete a first node of the linked list.
(e) Delete a node before specified position.
(f) Delete a node after specified position.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
Page No
Data Structure (3130702)
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
// (c) Insert a node such that the linked list is in ascending order
void insertInOrder(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL || (*head)->data >= data) {
newNode->next = *head;
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL && temp->next->data < data) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
}
// Menu-driven program
int main() {
struct Node* head = NULL;
int choice, value, pos;
while (1) {
printf("\nMenu:\n");
printf("1. Insert at front\n");
printf("2. Insert at end\n");
printf("3. Insert in order\n");
printf("4. Delete first node\n");
printf("5. Delete before position\n");
printf("6. Delete after position\n");
printf("7. Display list\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at front: ");
scanf("%d", &value);
insertAtFront(&head, value);
break; Page No
Data Structure (3130702)
case 2:
printf("Enter value to insert at end: ");
scanf("%d", &value);
insertAtEnd(&head, value);
break;
case 3:
printf("Enter value to insert in order: ");
scanf("%d", &value);
insertInOrder(&head, value);
break;
case 4:
deleteFirstNode(&head);
break;
case 5:
printf("Enter position before which to delete node: ");
scanf("%d", &pos);
deleteBeforePosition(&head, pos);
break;
case 6:
printf("Enter position after which to delete node: ");
scanf("%d", &pos);
deleteAfterPosition(&head, pos);
break;
case 7:
displayList(head);
break;
case 8:
exit(0);
default:
printf("Invalid choice. Try again.\n");
}
}
return 0;
}
Page No
Data Structure (3130702)
Output:
Response 1
Menu:
Menu: 1. Insert at front
1. Insert at front 2. Insert at end
2. Insert at end 3. Insert in order
3. Insert in order 4. Delete first node
4. Delete first node 5. Delete before position
5. Delete before position 6. Delete after position
6. Delete after position 7. Display list
7. Display list 8. Exit
8. Exit Enter your choice: 6
Enter your choice: 1 Enter position after which to
Enter value to insert at front: 5 delete node: 2
Menu: Menu:
1. Insert at front 1. Insert at front
2. Insert at end 2. Insert at end
3. Insert in order 3. Insert in order
4. Delete first node 4. Delete first node
5. Delete before position 5. Delete before position
6. Delete after position 6. Delete after position
7. Display list 7. Display list
8. Exit 8. Exit
Enter your choice: 1 Enter your choice: 3
Enter value to insert at front: 6 Enter value to insert in order: 9
Menu: Menu:
1. Insert at front 1. Insert at front
2. Insert at end 2. Insert at end
3. Insert in order 3. Insert in order
4. Delete first node 4. Delete first node
5. Delete before position 5. Delete before position
6. Delete after position 6. Delete after position
7. Display list 7. Display list
8. Exit 8. Exit
Enter your choice: 2 Enter your choice: 7
Enter value to insert at end: 7 6 -> 5 -> 8 -> 9 -> NULL
Menu: Menu:
1. Insert at front 1. Insert at front
2. Insert at end 2. Insert at end
3. Insert in order 3. Insert in order
4. Delete first node 4. Delete first node
5. Delete before position 5. Delete before position
6. Delete after position 6. Delete after position
7. Display list 7. Display list
8. Exit 8. Exit
Enter your choice: 2 Enter your choice: 8
Enter value to insert at end: 8
Page No
Data Structure (3130702)
Program:
#include <stdio.h>
#include <stdlib.h>
// Pop operation: Remove and return the top element of the stack
int pop(struct Node** top) {
if (*top == NULL) {
printf("Stack underflow! No element to pop.\n");
return -1;
}
struct Node* temp = *top;
int poppedData = temp->data;
*top = (*top)->next;
free(temp);
printf("Popped %d from the stack.\n", poppedData);
return poppedData;
}
while (1) {
printf("\nMenu:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Check if stack is empty\n");
printf("5. Display stack\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(&top, value);
break;
case 2:
pop(&top);
break;
case 3:
value = peek(top);
if (value != -1)
printf("Top element is %d\n", value);
break;
case 4:
if (isEmpty(top))
printf("Stack is empty.\n");
else
printf("Stack is not empty.\n");
break;
case 5:
displayStack(top);
break;
case 6:
exit(0);
Page No
Data Structure (3130702)
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
Output:
Program:
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* next;
};
// Queue structure
struct Queue {
struct Node *front, *rear;
};
// If queue is empty
if (q->rear == NULL) {
q->front = q->rear = newNode;
return;
}
// Add the new node at the end of the queue and change rear
q->rear->next = newNode;
q->rear = newNode;
printf("Enqueued: %d\n", value);
}
q->front = q->front->next;
free(temp);
return value;
}
Page No
Data Structure (3130702)
if (q->front == NULL) {
printf("Queue is empty\n");
return;
}
int main() {
struct Queue* q = createQueue();
enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);
printf("Queue: ");
displayQueue(q);
return 0;
}
Output:
Enqueued: 20
Enqueued: 30
Queue: 10 20 30
Dequeued: 10
Queue after dequeue: 20 30
Conclusion:
Page No
Data Structure (3130702)
Quiz:
(1) What is the primary difference between using a linked list versus an array to implement a
queue?
(2) Explain what happens to the front and rear pointers when an element is enqueued and
dequeued.
Page No
Data Structure (3130702)
(3) What would happen if we attempted to dequeue from an empty queue, and how does the
program handle this scenario?
Suggested Reference:
Marks
Page No