Data Structure Practical-3-1
Data Structure Practical-3-1
Experiment No: 3
3.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.
3.2 Write a program to implement stack using linked list
3.3 Write a program to implement queue using linked list.
Date: 6/10/23
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
Page No
Data Structure (3130702) Enrollment No
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.
✓ Insert
- Insert at first position
- Insert at last position
- Insert into ordered list
✓ Delete
✓ Traverse list (Print list)
✓ Copy linked list
Page No
Data Structure (3130702) Enrollment No
3.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;
} *h;
if (nn == NULL)
{
printf("-->!!memory not allocatted!!");
return;
}
printf("enter the data:");
scanf("%d", &nn->data);
nn->next = NULL;
if (h == NULL)
{
h = nn;
}
else
{
nn->next = h;
h = nn;
}
}
Page No
Data Structure (3130702) Enrollment No
struct node *cur = h;
struct node *nn = (struct node *)malloc(sizeof(struct node));
if (nn == NULL)
{
printf("-->!!memory not allocatted!!");
return;
}
printf("enter the data:");
scanf("%d", &nn->data);
nn->next = NULL;
if (h == NULL)
{
h = nn;
}
else
{
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = nn;
}
}
void insertasco(int v) // insert a new node to maintain link list in ascending order
{
struct node *cur = h;
struct node *nn = (struct node *)malloc(sizeof(struct node));
if (nn == NULL)
{
printf("-->!!memory not allocatted!!");
return;
}
printf("enter the data:");
scanf("%d", &nn->data);
nn->next = NULL;
if (h == NULL)
{
h = nn;
}
else
{
while (cur->next != NULL && cur->next->data <= nn->data)
{
Page No
Data Structure (3130702) Enrollment No
cur = cur->next;
}
nn->next = cur->next;
cur->next = nn;
}
}
Page No
Data Structure (3130702) Enrollment No
{
temp = h;
h = h->next;
free(prev);
free(temp);
return;
}
else
{
while (temp->next != NULL && temp->next->data != v)
{
prev = temp;
temp = temp->next;
}
if (temp->next->data == v)
{
prev->next = temp->next;
free(temp);
return;
}
else
{
printf("-->%d not found", v);
return;
}
}
}
}
Page No
Data Structure (3130702) Enrollment No
{
cur = cur->next;
}
if (cur->next->data == v)
{
temp = cur->next;
temp = temp->next;
cur = cur->next;
if (temp->next == NULL)
{
cur->next = NULL;
}
else
{
cur->next = temp->next;
}
free(temp);
}
else
{
printf("-->%d not found", v);
return;
}
}
}
Page No
Data Structure (3130702) Enrollment No
int main()
{
int ch;
do
{
printf("\n1.insert at front 2.insert at end 3.insert for ascending 4.delete front\n”);
printf(“5.delete before 6.delete after 7.display 0.stop\n");
printf("enter choice accordingly:");
scanf("%d", &ch);
int x, y;
switch (ch)
{
case 1:
insertbeg();
break;
case 2:
insertend();
break;
case 3:
insertasco(x);
break;
case 4:
deletebeg();
break;
case 5:
printf("enter data:");
scanf("%d", &x);
deletebsn(x);
break;
case 6:
printf("enter data:");
scanf("%d", &y);
deleteasn(y);
break;
case 7:
display();
break;
case 0:
printf("-->exited succssesfully");
return 0;
default:
printf("-->please enter correctly\n");
}
} while (ch != 0);
return 0;
Page No
Data Structure (3130702) Enrollment No
}
Output:
Page No
Data Structure (3130702) Enrollment No
-->Linked List: 43 44 45 46
Page No
Data Structure (3130702) Enrollment No
Program:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}*h;
if (nn == NULL)
{
printf("-->!!memory not allocatted!!");
return;
}
printf("enter element to push:");
scanf("%d", &nn->data);
nn->next = NULL;
if (h == NULL)
{
h = nn;
}
else
{
nn->next = h;
h = nn;
}
}
Page No
Data Structure (3130702) Enrollment No
printf("-->element popped\n");
h = h->next;
free(temp);
}
}
int main()
{
int ch;
do
{
printf("\n1.push 2.pop 3.display 0.stop\n");
printf("enter accordingly:");
scanf("%d", &ch);
switch (ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 0:
Page No
Data Structure (3130702) Enrollment No
printf("-->successfully exited");
break;
default:
printf("-->please enter correctly\n");
}
} while (ch != 0);
return 0;
}
Output:
Page No
Data Structure (3130702) Enrollment No
Page No
Data Structure (3130702) Enrollment No
Program:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} *h;
if (nn == NULL)
{
printf("-->!!memory not allocatted!!");
return;
}
printf("enter element to enque:");
scanf("%d", &nn->data);
nn->next = NULL;
if (h == NULL)
{
h = nn;
}
else
{
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = nn;
}
}
Page No
Data Structure (3130702) Enrollment No
printf("-->queue is empty\n");
}
else
{
h = h->next;
printf("-->element dequed\n");
free(temp);
}
}
int main()
{
int ch;
do
{
printf("\n1.enque 2.deque 3.display 0.stop\n");
printf("enter accordingly:");
scanf("%d", &ch);
int x;
switch (ch)
{
case 1:
enque();
break;
case 2:
deque();
Page No
Data Structure (3130702) Enrollment No
break;
case 3:
display();
break;
case 0:
printf("-->successfully exited");
break;
default:
printf("-->please enter correctly\n");
}
} while (ch != 0);
return 0;
}
Output:
Page No
Data Structure (3130702) Enrollment No
-->queue: 44 43
Observations:
Linked lists are dynamic data structures that can grow or shrink in size during runtime,
making them suitable for applications where the number of elements is unknown or changes
frequently.Link list is a linear and abstract data structure.Using link list we can implement data
structures like stack and queue.
Conclusion:
• In a singly linked list, each node points to the next node in the sequence. Traversal
is unidirectional, meaning you can only move from the head to the tail.This type of
linked list is simple and memory-efficient but has limitations when it comes to
reverse traversal.
Page No
Data Structure (3130702) Enrollment No
Quiz:
(1) Which are the operations on singly link list?
(2) State the limitation of singly link list
(3) Compare array and singly link list
1. 1.insert
2.delete
3.display
3. Arrays are better for situations where you need random access and have a
fixed data size, while singly linked lists are more suitable when you need
dynamic sizing and efficient insertions/deletions.
Suggested Reference:
Marks
Page No