0% found this document useful (0 votes)
4 views19 pages

Data Structure Practical-3-1

The document outlines a practical experiment for implementing a singly linked list in C, detailing operations such as insertion, deletion, and traversal. It includes programs for a stack and a queue using linked lists, along with safety precautions and objectives for understanding linked list concepts. The document provides sample outputs for the implemented programs, demonstrating their functionality.

Uploaded by

vishvampandya74
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)
4 views19 pages

Data Structure Practical-3-1

The document outlines a practical experiment for implementing a singly linked list in C, detailing operations such as insertion, deletion, and traversal. It includes programs for a stack and a queue using linked lists, along with safety precautions and objectives for understanding linked list concepts. The document provides sample outputs for the implemented programs, demonstrating their functionality.

Uploaded by

vishvampandya74
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/ 19

Data Structure (3130702) Enrollment No

Experiment No: 3

AIM : Singly linked list

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

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 with turbo C/C++

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

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.

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

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;

void insertbeg() // insert a new node at beginning


{
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
{
nn->next = h;
h = nn;
}
}

void insertend() // insert a new node at last


{

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

void deletebeg() // delete first node in link list


{
struct node *temp = h;
if (h == NULL)
{
printf("-->link list is empty\n");
}
else
{
h = h->next;
free(temp);
}
}

void deletebsn(int v) // delete node before specified node


{
struct node *temp, *prev;
if (h == NULL)
{
printf("-->linked list is empty\n");
return;
}
else if (h->next == NULL)
{
printf("-->can not delete node only one node\n");
return;
}
else if (h->next->data == v)
{
temp = h;
h = h->next;
free(temp);
return;
}
else
{
prev = h;
temp = h->next;
if (temp->data == v)

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

void deleteasn(int v) // delete node after specified node


{
struct node *cur = h;
struct node *temp;
if (h == NULL)
{
printf("-->link list is empty\n");
}
else if (cur->data == v)
{
temp = cur->next;
cur->next = temp->next;
free(temp);
}
else
{
while (cur->next != NULL && cur->next->data != v)

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

void display() // display all node of link list


{
struct node *cur = h;
if (h == NULL)
{
printf("-->link list is empty\n");
}
else
{
printf("-->Linked List: ");
while (cur != NULL)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
}

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:

1.insert at front 2.insert at end 3.insert for ascending 4.delete front


5.delete before 6.delete after 7.display 0.stop
enter choice accordingly:2
enter the data:43

1.insert at front 2.insert at end 3.insert for ascending 4.delete front


5.delete before 6.delete after 7.display 0.stop
enter choice accordingly:2
enter the data:44

1.insert at front 2.insert at end 3.insert for ascending 4.delete front


5.delete before 6.delete after 7.display 0.stop
enter choice accordingly:2
enter the data:45

1.insert at front 2.insert at end 3.insert for ascending 4.delete front


5.delete before 6.delete after 7.display 0.stop
enter choice accordingly:2
enter the data:46

1.insert at front 2.insert at end 3.insert for ascending 4.delete front


5.delete before 6.delete after 7.display 0.stop
enter choice accordingly:7
-->Linked List: 43 44 45 46

1.insert at front 2.insert at end 3.insert for ascending 4.delete front


5.delete before 6.delete after 7.display 0.stop
enter choice accordingly:4

1.insert at front 2.insert at end 3.insert for ascending 4.delete front


5.delete before 6.delete after 7.display 0.stop
enter choice accordingly:7
-->Linked List: 44 45 46

1.insert at front 2.insert at end 3.insert for ascending 4.delete front


5.delete before 6.delete after 7.display 0.stop
enter choice accordingly:1
enter the data:43

1.insert at front 2.insert at end 3.insert for ascending 4.delete front


5.delete before 6.delete after 7.display 0.stop
enter choice accordingly:7

Page No
Data Structure (3130702) Enrollment No
-->Linked List: 43 44 45 46

1.insert at front 2.insert at end 3.insert for ascending 4.delete front


5.delete before 6.delete after 7.display 0.stop
enter choice accordingly:5
enter data:46

1.insert at front 2.insert at end 3.insert for ascending 4.delete front


5.delete before 6.delete after 7.display 0.stop
enter choice accordingly:7
-->Linked List: 43 44 46

1.insert at front 2.insert at end 3.insert for ascending 4.delete front


5.delete before 6.delete after 7.display 0.stop
enter choice accordingly:6
enter data:43

1.insert at front 2.insert at end 3.insert for ascending 4.delete front


5.delete before 6.delete after 7.display 0.stop
enter choice accordingly:7
-->Linked List: 43 46

1.insert at front 2.insert at end 3.insert for ascending 4.delete front


5.delete before 6.delete after 7.display 0.stop
enter choice accordingly:3
enter the data:44

1.insert at front 2.insert at end 3.insert for ascending 4.delete front


5.delete before 6.delete after 7.display 0.stop
enter choice accordingly:7
-->Linked List: 43 44 46

1.insert at front 2.insert at end 3.insert for ascending 4.delete front


5.delete before 6.delete after 7.display 0.stop
enter choice accordingly:0
-->exited succssesfully

Page No
Data Structure (3130702) Enrollment No

3.2 Write a program to implement stack using linked list

Program:

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

struct node
{
int data;
struct node *next;
}*h;

void push() //push element to stack


{
struct node *nn = (struct node *)malloc(sizeof(struct node));

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

void pop() //pop element from stack


{
struct node *temp = h;
if (h == NULL)
{
printf("-->stack is empty\n");
}
else
{

Page No
Data Structure (3130702) Enrollment No
printf("-->element popped\n");
h = h->next;
free(temp);
}
}

void display() //display all element of stack


{
struct node *cur = h;
if (h == NULL)
{
printf("-->stack is empty\n");
}
else
{
printf("-->stack: ");
while (cur != NULL)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
free(cur);
}

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:

1.push 2.pop 3.display 0.stop


enter accordingly:1
enter element to push:43

1.push 2.pop 3.display 0.stop


enter accordingly:1
enter element to push:44

1.push 2.pop 3.display 0.stop


enter accordingly:1
enter element to push:45

1.push 2.pop 3.display 0.stop


enter accordingly:1
enter element to push:46

1.push 2.pop 3.display 0.stop


enter accordingly:3
-->stack: 46 45 44 43

1.push 2.pop 3.display 0.stop


enter accordingly:2
-->element popped

1.push 2.pop 3.display 0.stop


enter accordingly:3
-->stack: 45 44 43

1.push 2.pop 3.display 0.stop


enter accordingly:2
-->element popped

1.push 2.pop 3.display 0.stop


enter accordingly:3
-->stack: 44 43

Page No
Data Structure (3130702) Enrollment No

1.push 2.pop 3.display 0.stop


enter accordingly:1
enter element to push:46

1.push 2.pop 3.display 0.stop


enter accordingly:3
-->stack: 46 44 43

1.push 2.pop 3.display 0.stop


enter accordingly:0
-->successfully exited

Page No
Data Structure (3130702) Enrollment No

3.3 Write a program to implement queue using linked list.

Program:

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

struct node
{
int data;
struct node *next;
} *h;

void enque() //enque element in queue


{
struct node *cur = h;
struct node *nn = (struct node *)malloc(sizeof(struct node));

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

void deque() //deque element from queue


{
struct node *temp = h;
if (h == NULL)
{

Page No
Data Structure (3130702) Enrollment No
printf("-->queue is empty\n");
}
else
{
h = h->next;
printf("-->element dequed\n");
free(temp);
}
}

void display() //display all element of queue


{
struct node *cur = h;
if (h == NULL)
{
printf("-->queue is empty\n");
}
else
{
printf("-->queue: ");
while (cur != NULL)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
free(cur);
}

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:

1.enque 2.deque 3.display 0.stop


enter accordingly:1
enter element to enque:46

1.enque 2.deque 3.display 0.stop


enter accordingly:1
enter element to enque:45

1.enque 2.deque 3.display 0.stop


enter accordingly:1
enter element to enque:44

1.enque 2.deque 3.display 0.stop


enter accordingly:1
enter element to enque:43

1.enque 2.deque 3.display 0.stop


enter accordingly:3
-->queue: 46 45 44 43

1.enque 2.deque 3.display 0.stop


enter accordingly:2
-->element dequed

1.enque 2.deque 3.display 0.stop


enter accordingly:2
-->element dequed

1.enque 2.deque 3.display 0.stop


enter accordingly:3

Page No
Data Structure (3130702) Enrollment No
-->queue: 44 43

1.enque 2.deque 3.display 0.stop


enter accordingly:2
-->element dequed

1.enque 2.deque 3.display 0.stop


enter accordingly:2
-->element dequed

1.enque 2.deque 3.display 0.stop


enter accordingly:2
-->queue is empty

1.enque 2.deque 3.display 0.stop


enter accordingly:3
-->queue is empty

1.enque 2.deque 3.display 0.stop


enter accordingly:0
-->successfully exited

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.

In singly list we can use function as below

• insertbeg() : insert a new node at beginning


• insertend() : insert a new node at last
• insertasco() : insert a new node to maintain link list in ascending order
• deletebeg() : delete first node in link list
• deletebsn() : delete node before specified node
• deleteasn() : delete node after specified node
• display() : display all node of link list

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

2. Limitation of singly link llist is we can not travel in reverse.reverse


traversal is not possible.

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:

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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy