Write a C program that uses functions to perform the following:



a) Create a singly linked list of integers.
b) Delete a given integer from the above-linked list.
c) Display the contents of the above list after deletion.

Singly Linked List
Generally, a Linked List means "Singly Linked List". It is a chain of records known as Nodes. Each node has at least two members, one of which points to the next Node in the list and the other holds the data.

Basically, Single Linked Lists are uni-directional as they can only point to the next Node in the list but not to the previous. We use below structure for a Node in our example.
struct Node
{
int Data;
struct Node *Next;
};
Variable Data holds the data in the Node (It can be a pointer variable pointing to the dynamically allocated memory) while Next holds the address to the next Node in the list.
Head is a pointer variable of type struct Node which acts as the Head to the list. Initially, we set 'Head' as NULL which means the list is empty.
Basic Operations on a Singly Linked List
a. Traversing a List
b. Inserting a Node in the List
c. Deleting a Node from the List


Inserting a Node in Singly Linked List
1.Inserting a Node at the beginning of the List
2. Inserting a Node at the End of the List
3. Inserting a Node at position 'p' in the List

Deletion of a Node from a Singly Linked List
1. Deleting First Node in Singly Linked List
2. Deleting Last Node in the Singly Linked List
3. Deleting Node from position 'p' in the List

The algorithm used for implementation:
Initialize the first and last nodes with NULL values
struct node *first=NULL,*last=NULL,*next,*prev,*cur;

1. Algorithm creating a new node:
Step 1: if the list is empty then first==NULL
Step 2: Create a new node
cur=(struct node*) malloc (sizeof (struct node));
Step 3: Read the content of node
Step 4: Assign new node link to NULL
cur->link=NULL


Step 5: Assign new node to first & last node
first=cur
last=cur
Step 6: If the list is not empty call insert function insert ()
Step 7 : Stop

2. Algorithm for Inserting a new node:
Step 1 : Initialize count c to 1
Step 2 : Create inserting node
cur=(struct node*)malloc(sizeof (struct node));
Step 3: Read the content of node
Step 4: Read the position of insertion
Step 5: Inserting in first position
Check if the pos=1 and first!=NULL
cur->link=first;
first=cur;
Step 6: Inserting in a given position
next=first;
repeat the steps a to c until c < pos
a. prev=next;
b. next=prev->link;
c. c++;
cur->link=prev->link;
prev->link=cur;
Step 7 : Stop

3. Algorithm for Deleting a node:
Step 1 : Initialize count c to 1
Step 2 : Read the position for deletion
Step 3 : Check if first=NULL
print list is empty
Step 4 : If the list contains single element
Check if pos=1 and first->link=NULL
print deleted element is first->data
Step 5 : Assign first to NULL
first=NULL;
Step 6 : If the list contains more than one element and to delete first element
if pos=1 and first->link!=NULL
cur=first;
first=first->link;
cur->link=NULL;
print deleted element is cur->data
free(cur)
Step 7 : If the list contains more than one element and to delete an element at given position
next=first;
repeat the steps a to c until c < pos
a. cur=next;
b. next=next->link;
c. c++;
cur->link=next->link;
next->link=NULL;
print deleted element is next->data
free(next);
Step 8 : Stop



4. Algorithm for Displaying a node:
Step1 : Check if first node is NULL
print list is empty
Step2: If first node is not NULL then
cur=first;
repeat the steps a to b until cur!=NULL
a . print cur->data
b . cur=cur->link;
Step3 : Stop

Input & Output:
Insert() : 11 22 33 34 35


Display () : 11->22->33->34->35
Delete() : 34
Display() : 11->22->33->35


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments:

Thanks
Mukesh Rajput