0% found this document useful (0 votes)
40 views9 pages

DS Experiment No 06

The document describes implementing a singly linked list data structure in C by defining node structures containing data and pointer fields, demonstrating functions for insertion, deletion, and traversal of nodes, and providing examples of linked lists and the steps for performing operations like adding a node to the front of the list. It also discusses the advantages of linked lists over arrays in terms of dynamic memory allocation and different types of linked lists.

Uploaded by

jadhavkalpesh881
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)
40 views9 pages

DS Experiment No 06

The document describes implementing a singly linked list data structure in C by defining node structures containing data and pointer fields, demonstrating functions for insertion, deletion, and traversal of nodes, and providing examples of linked lists and the steps for performing operations like adding a node to the front of the list. It also discusses the advantages of linked lists over arrays in terms of dynamic memory allocation and different types of linked lists.

Uploaded by

jadhavkalpesh881
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/ 9

Experiment No.

- 6
Aim:
Implement Singly Linked List ADT.

Input specification :- User is supposed to enter the choice

Output specification :
Choices entered by an user are implement and results are displayed

Theory:
A linked list a non-sequential collection of data items called nodes. These nodes in principle are
structures containing fields. Each node in a linked list basically contains two fields.
1. Data field
2. Link field
The data field contains an actual value to be stored and processed. And, the link field contains the
address of the next data item (or node). In other words, it establishes a link to the next data item in
a linked list.

Physical view:

Data Link
Node
Logical view:

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

Example:

Start 10 2000 20 3000 30 Null

Null Pointer: The link field of the last node contains zero rather than a valid address. It is a Null
pointer and indicates the end of the list. .
External pointer: Start is an external pointer to the very first node in the linked list. It enables us
to access the entire linked list.
Types of Linked List
Following are the various types of linked list.
• Singly Linked List − Item Navigation is forward only.
• Doubly Linked List − Items can be navigated forward and backward way.
• Circular Linked List − Last item contains link of the first element as next.

Following are the basic operations supported by a list.


• Insertion − add an element at the beginning of the list.
• Deletion − delete an element at the beginning of the list.
• Display − displaying complete list.
• Search − search an element using given key.
Insertion Operation
Insertion is a three step process −
• Allocate memory for the new node.
• Add value into the data field of the new node.
• Adjust the pointers
There are three situation for inserting element in list.
1. Insertion at the front of list.
2. Insertion in the middle of the list.
3. Insertion at the end of the list.

Insertion at the front of list

Insertion in given location of Linked List


Insertion at the end of the list

Deletion Operation
Similar to insertion there are three situations for deleting an element in list.
1. Deletion at beginning of the list
2. Deletion at the middle of the list
3. Deletion at the end of the list

Deletion Procedure in Singly Linked list


Single Linked List before deletion

Deletion Node 30 in Single Linked List

After Deletion Linked List


Algorithm:
Inserting a New node at the beginning of the linked list
Step-1: Start
Step-2: Get the value for New_Node to be added to the list.
Step-3: Create a New_Node, empty node by calling malloc(). If malloc() returns no error then go to
step-4 or else say "Memory shortage".
Step-4: New_Node -> data = value.
Step-4: Set NEW node -> next = Head.
Step-5: Set the external pointer Head -> New_Node.

Deleting the first node of the linked list


Step-1:Start
Step-2: if Head = NULL then
Print “ Empty Linked List” and goto Step 6.
Step-3: Set p = Head
Step-4: Head = Head - > next
Step-5: Free the memory of node p.
Step-6: Stop

Search for Key element in Linked List


Step-1: If Head = NULL then
Print “ Empty Linked List” and goto Step 6.
Step-2: Read the key value to be searched
Step-3: Set p=Head
Step-4: Repeat while p !=NULL
If p-> data == key then
Print “ Element Found” and goto step 6.
Else
p = p -> next
Step-5: if p==NULL then
Print “ Element not found”
Step-6: Stop

Display the linked list


Step-1: If Head = NULL then
Print “ Empty Linked List” and goto Step 6.
Step-2: Set p=Head
Step-3: Repeat while p !=NULL
Print “p-> data”
p = p -> next
Step-4: Stop
Experiment -06: Implementation of Linear Linked List

#include<stdio.h>
#include<conio.h>
struct LL {
int data;
struct LL *next;
};
struct LL *start=NULL;
void inBeg(int value) {
struct LL *nn=(struct LL *)malloc(sizeof(struct LL));
nn->data=value;
nn->next=start;
start=nn;
}

void inLast(int value) {


struct LL *temp=start;
struct LL *nn=(struct LL *)malloc(sizeof(struct LL));
nn->data=value;
nn->next=NULL;

if(start==NULL)
start=nn;
else {
while(temp->next!=NULL)
temp=temp->next;
temp->next=nn;
}
}

void deBeg() {
if(start==NULL)
printf("\nLinked list empty\n");
else
start=start->next;
}
void deLast() {
struct LL *temp=start;

if(start==NULL)
printf("\nLinked list empty\n");
else if(temp->next==NULL)
start=NULL;
else while(temp->next->next!=NULL)
temp=temp->next;
}
void display() {
struct LL *temp=start;
if(start==NULL)
printf("\nLinked list empty\n");
else while(temp!=NULL) {
printf("%d ",temp->data);
temp=temp->next;
}
}
void main() {
int value,choice;
clrscr();
while(1) {
printf("\n1.Insert at Beginning\n2.Insert at Last position");
printf("\n3.delete from Beginning\n4.delete from Last position");
printf("\n5.Display\n6.Exit\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice) {
case 1: printf("\nEnter the element\t");
scanf("%d",&value);
inBeg(value);
break;
case 2: printf("\nEnter the element\t");
scanf("%d",&value);
inLast(value);
break;
case 3: deBeg();
break;
case 4: deLast();
break;
case 5: display();
break;
case 6: exit(1);
default: printf("Wrong Input.\n");
break;
}
}
}

//Output:
Conclusion:
Linked list is very useful for storing data at runtime. Its gives better memory utilization compare
to array. But linked list have drawback that it requires extra memory space for a pointer with each
element of the list.

You might also like