Singly Linked List

Singly Linked List is dynamic data structure where we can store elements in dynamic way that is you can add any number of element at run-time.

Structure of Singly Linked List

Singly-Linked-List-Example
Here you can see that each node is connected with each other and starting node address(1000) is stored by head.

 

Structure of Node

Single Linked List is linear data structure where each element or node has following information.

  1. Data
  2. Address of Next Node/Element

Singly Linked List

Data

Data field contains the information to be stored like student name,age,dob,employee name etc.

Address

Address field contains the address of next node in the list so that they are connected to each other.

Advantage

  1. Singly Linked List supports traversal in forward direction only.
  2. Dynamic data structure
  3. Fast insertion and deletion than array where lots of shifting operations.

Disadvantage

  1. Traversal in only forward direction no backward.
  2. No random access facility like array.

Source Code

We have created source code to do the following operation with a singly linked list using c functions.

  1. CreateList
  2. Display Nodes
  3. Delete First Node
  4. Add Node at end of list
  5. Add Node at the beginning of list
  6. Add Node at specified position

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct student
{
int id;
struct student *next;
}*start,*last;
typedef struct student node;
void delete_First();
void create_Nodelist(int);
void displaylist();
void insertNodeAtEnd();
void insertNodeAtbegin();
void insertNodeAtPos(int);
void main()
{
int num,choice,pos;
clrscr();
printf("\nMenu:-");
printf("\n1-CreateList");
printf("\n2-To display list");
printf("\n3-To delete First node");
printf("\n4-Add/Append Node to the last of List");
printf("\n5-Add Node to the begin of List");
printf("\n6-Add Node to the specified position.");

printf("\nEnter your choice");
scanf("%d",&choice);
while(choice!=0){
switch(choice){
case 1:
printf("Enter how many node you want to create");
scanf("%d",&num);
create_Nodelist(num);
break;
case 2:
printf("\nNodes list");
displaylist();
break;
case 3:
delete_First();
break;
case 4:
insertNodeAtEnd();
break;
case 5:
insertNodeAtbegin();
break;
case 6:
printf("\nEnter position to add new node");
scanf("%d",&pos);
insertNodeAtPos(pos);
break;
default:
printf("\nInvalid Input");
break;
}
printf("\nDo you want to continue or exit(0);");
scanf("%d",&choice);
}
getch();
}
void create_Nodelist(int n)
{
node *temp=start,*current_node,*new_node;
int i;
for(i=0;i<n;i++) { new_node=(node *)malloc(sizeof(node)); printf("\nEnter id"); scanf("%d",&new_node->id);
new_node->next=NULL;
if(i==0)
{
start=new_node;
current_node=new_node;
}
else
{
current_node->next=new_node;
current_node=new_node;
}
}
last = new_node;
}

void delete_First()
{
node *temp=start;
if(temp==NULL)
{
printf("\nList is empty");
}
else
{
start=start->next;
free(temp);
}

}
void insertNodeAtEnd(){
int i,n;
node *new_node;
new_node = (node*)malloc(sizeof(node));
printf("\nEnter ID");
scanf("%d",&new_node->id);
new_node->next=NULL;
last->next=new_node;
last = new_node;
}

void displaylist()
{
node *temp=start;
while(temp!=NULL)
{
printf("\nId is=%d",temp->id);
temp=temp->next;
}
}
void insertNodeAtbegin()
{
node *new_node;
new_node=(node *)malloc(sizeof(node));
printf("\nEnter id");
scanf("%d",&new_node->id);
if(start==NULL){
start=new_node;
}else{
new_node->next = start;
start = new_node;
}
}
void insertNodeAtPos(int pos)
{
node *new_node,*temp=start,*current_node=NULL;
int counter=0;
new_node=(node *)malloc(sizeof(node));
printf("\nEnter id");
scanf("%d",&new_node->id);

//while list is empty
if(start==NULL){
printf("\nList is empty");
}else{
//need to reach to the requested position
while(temp!=NULL){
if(counter==pos-1){
current_node = temp;
}
temp = temp->next;
counter++;
}
if(current_node==NULL){
printf("\nPosition has maximum value than list size.");
}
else{
new_node->next = current_node->next;
current_node->next = new_node;
}

}

}

Output

Singly-Linked-List-Outpu

Leave a Reply