0% found this document useful (0 votes)
23 views14 pages

Experiment 5 DS Student

.

Uploaded by

my11cricle628
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)
23 views14 pages

Experiment 5 DS Student

.

Uploaded by

my11cricle628
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
You are on page 1/ 14

Experiment No – 5

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:

Competency and Practical Skills: Logic building and programming

Relevant CO: CO2, CO5

Objectives: (a) To understand the concepts of singly linked list


(b) To analyze different algorithms on singly link list
(c) To implement various operations on singly link list
Equipment/Instruments: Computer System Dev - C++ compiler

Safety and necessary Precautions:

✓ Operate computer system carefully and responsibly.


✓ Use required lab resources cautiously

Theory:

Singly link list

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)

Operations on singly linked list

• 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;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// (a) Insert a node at the front of the linked list


void insertAtFront(struct Node** head, int data) {
struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}

// (b) Insert a node at the end of the linked list


void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}

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;
}
}

// (d) Delete the first node of the linked list


void deleteFirstNode(struct Node** head) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = *head;
*head = (*head)->next;
free(temp);
}

// (e) Delete a node before a specified position


void deleteBeforePosition(struct Node** head, int pos) {
if (*head == NULL || pos <= 1) {
printf("Invalid position or list is empty.\n");
return;
}
if (pos == 2) {
deleteFirstNode(head);
return;
}
struct Node* temp = *head;
int i;
for (i = 1; i < pos - 2 && temp->next != NULL; i++) {
temp = temp->next;
}
if (temp->next == NULL || temp->next->next == NULL) {
printf("Position is out of bounds.\n");
return;
}
struct Node* delNode = temp->next;
temp->next = delNode->next;
free(delNode);
}
Page No
Data Structure (3130702)
// (f) Delete a node after a specified position
void deleteAfterPosition(struct Node** head, int pos) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = *head;
int i;
for (i = 1; i < pos && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL || temp->next == NULL) {
printf("Position is out of bounds.\n");
return;
}
struct Node* delNode = temp->next;
temp->next = delNode->next;
free(delNode);
}

// Function to display the linked list


void displayList(struct Node* head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// 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)

5.2 Write a program to implement stack using linked list.

Program:

#include <stdio.h>
#include <stdlib.h>

// Define a node structure


struct Node {
int data;
struct Node* next;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Push operation: Add element to the top of the stack


void push(struct Node** top, int data) {
struct Node* newNode = createNode(data);
newNode->next = *top;
*top = newNode;
printf("Pushed %d onto the stack.\n", data);
}

// 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;
}

// Peek operation: Get the top element without removing it


int peek(struct Node* top) {
if (top == NULL) {
printf("Stack is empty.\n");
return -1;
}
return top->data;
}

// Check if the stack is empty


int isEmpty(struct Node* top) {
return top == NULL;
}
Page No
Data Structure (3130702)

// Function to display the stack


void displayStack(struct Node* top) {
if (top == NULL) {
printf("Stack is empty.\n");
return;
}
printf("Stack elements: ");
struct Node* temp = top;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Menu-driven program to implement stack operations


int main() {
struct Node* top = NULL;
int choice, value;

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:

Menu: 4. Check if stack is empty


1. Push 5. Display stack
2. Pop 6. Exit
3. Peek Enter your choice: 2
4. Check if stack is empty Popped 8 from the stack.
5. Display stack
6. Exit Menu:
Enter your choice: 1 1. Push
Enter value to push: 5 2. Pop
Pushed 5 onto the stack. 3. Peek
4. Check if stack is empty
Menu: 5. Display stack
1. Push 6. Exit
2. Pop Enter your choice: 3
3. Peek Top element is 7
4. Check if stack is empty
5. Display stack Menu:
6. Exit 1. Push
Enter your choice: 1 2. Pop
Enter value to push: 6 3. Peek
Pushed 6 onto the stack. 4. Check if stack is empty
5. Display stack
Menu: 6. Exit
1. Push Enter your choice: 4
2. Pop Stack is not empty.
3. Peek
4. Check if stack is empty Menu:
5. Display stack 1. Push
6. Exit 2. Pop
Enter your choice: 1 3. Peek
Enter value to push: 7 4. Check if stack is empty
Pushed 7 onto the stack. 5. Display stack
6. Exit
Menu: Enter your choice: 5
1. Push Stack elements: 7 -> 6 -> 5 ->
2. Pop NULL
3. Peek
4. Check if stack is empty Menu:
5. Display stack 1. Push
6. Exit 2. Pop
Enter your choice: 1 3. Peek
Enter value to push: 8 4. Check if stack is empty
Pushed 8 onto the stack. 5. Display stack
6. Exit
Menu: Enter your choice: 2
1. Push Popped 7 from the stack.
2. Pop
3. Peek Menu:
Page No
Data Structure (3130702)
1. Push
2. Pop Menu:
3. Peek 1. Push
4. Check if stack is empty 2. Pop
5. Display stack 3. Peek
6. Exit 4. Check if stack is empty
Enter your choice: 1 5. Display stack
Enter value to push: 3 6. Exit
Pushed 3 onto the stack. Enter your choice: 2
Popped 5 from the stack.
Menu:
1. Push Menu:
2. Pop 1. Push
3. Peek 2. Pop
4. Check if stack is empty 3. Peek
5. Display stack 4. Check if stack is empty
6. Exit 5. Display stack
Enter your choice: 2 6. Exit
Popped 3 from the stack. Enter your choice: 5
Stack is empty.
Menu:
1. Push Menu:
2. Pop 1. Push
3. Peek 2. Pop
4. Check if stack is empty 3. Peek
5. Display stack 4. Check if stack is empty
6. Exit 5. Display stack
Enter your choice: 2 6. Exit
Popped 6 from the stack. Enter your choice: 6

5.3 Write a program to implement queue using linked list.

Program:

#include <stdio.h>
#include <stdlib.h>

// Node structure
struct Node {
int data;
struct Node* next;
};

// Function to create a new node


struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory error\n");
return NULL;
}
newNode->data = value;
newNode->next = NULL;
return newNode;
}
Page No
Data Structure (3130702)

// Queue structure
struct Queue {
struct Node *front, *rear;
};

// Function to create a queue


struct Queue* createQueue() {
struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
if (!q) {
printf("Memory error\n");
return NULL;
}
q->front = q->rear = NULL;
return q;
}

// Function to enqueue an element


void enqueue(struct Queue* q, int value) {
struct Node* newNode = createNode(value);
if (!newNode) return;

// 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);
}

// Function to dequeue an element


int dequeue(struct Queue* q) {
if (q->front == NULL) {
printf("Queue is empty\n");
return -1;
}

struct Node* temp = q->front;


int value = temp->data;

q->front = q->front->next;

// If front becomes NULL, set rear to NULL as well


if (q->front == NULL) {
q->rear = NULL;
}

free(temp);
return value;
}

// Function to display the queue


void displayQueue(struct Queue* q) {

Page No
Data Structure (3130702)
if (q->front == NULL) {
printf("Queue is empty\n");
return;
}

struct Node* temp = q->front;


while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

int main() {
struct Queue* q = createQueue();

enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);

printf("Queue: ");
displayQueue(q);

printf("Dequeued: %d\n", dequeue(q));

printf("Queue after dequeue: ");


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:

1. An Introduction to Data Structures with Applications. by Jean-Paul Tremblay & Paul G.


Sorenson Publisher-Tata McGraw Hill.
2. Data Structures using C & C++ -By Ten Baum Publisher – Prenctice-Hall International
3. Fundamentals of Computer Algorithms by Horowitz, Sahni,Galgotia Pub. 2001 ed.
4. http://www.geeksforgeeks.org/data-structures/
5. http://www.coursera.org/specializations/data-structures-algorithms

References used by the students:

Rubric-wise marks obtained:

Problem Coding Completeness


Logic
Understanding Standards and accuracy Q&A
Rubrics Building (2) Total
(2) (2) (2)
Avg. Good Avg. Good Avg. Good Avg. Good Avg. Good
(1) (2) (1) (2) (1) (2) (1) (2) (1) (2)

Marks

Page No

You might also like