DATA STRUCTURES

singly linked list

Singly Linked List

Singly linked list is a list where in every node there are two parts data part and a pointer to the next node in the list hence traversing of list is unidirectional means we can traverse a linked list in one direction. 

In singly linked list first node of the list is pointed by the start or head pointer and there is a NULL value in the next pointer of the last node.

syntax/structure

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

Memory representation:

//here upload image for MR.

 

Menu driven program for singly linked list:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *start=NULL;

//insert node at start
void insertnode_start(int data)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=data;
temp->link=NULL;
if(start==NULL)
{
start=temp;
temp->link=start;
}
else
{
temp->link=start;
start=temp;
}
}

//insert node at end

void insertnode_end(int data)
{
struct node *cur=start;
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=data;
temp->link=NULL;
if(start==NULL)
{
start=temp;
temp->link=start;
}
else
{
while(cur->link!=start)
{
cur=cur->link;
}
cur->link=temp;
temp->link=start;
}
}
//insert node at a given location

void insertnode_loc(int loc, int data)
{
int i;
struct node *cur=start;
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=data;
temp->link=NULL;
for(i=1;i<loc;i++)
cur=cur->link;
if(loc<1 || cur==NULL)
{
printf("nWrong Location - Insertion not possible n");
return;
}
temp->link=cur->link;
cur->link=temp;
}
//delete node at start
void deletenode_start()
{
struct node *cur=start;
if(start==NULL)
{
printf("nLinked is Empty ");
return;
}
start=start->link;
free(cur);
printf("nNode Deleted from Start");
}

//delete node end
void deletenode_end()
{
struct node *cur=start;
struct node *prev;
if(start==NULL)
{
printf("nLinked is Empty ");
return;
}
while(cur->link!=start)
{
prev=cur;
cur=cur->link;
}
prev->link=start;
free(cur);
printf("nNode Deleted from End");
}

//delete node at a given location
void deletenode_loc(int loc)
{
struct node *cur=start;
struct node *prev;
int i;
if(start==NULL)
{
printf("nLinked is Empty ");
return;
}
for(i=1;i<loc;i++)
{
prev=cur;
cur=cur->link;
}
if(loc<1 || cur==NULL)
{
printf("nWrong Location - Deletion not possible n");
return;
}
prev->link=cur->link;
free(cur);
printf("nNode %d Deleted ",loc);
}

//traverse list
void traverse_llist()
{
struct node *cur=start;
if(start==NULL)
printf("Linked List is empty");
else
{
while(cur!=start)
{
printf("%d --->",cur->data);
cur=cur->link;
}
}
}



void main()
{
int ch;
int data;
int l;
int choice;

do
{
printf("nn******** Various Operation on Linked List Data Structure********nn Main Menu:nn");
printf("n Enter 1 : Add Node to Linked List (Start, End or Location)");
printf("n Enter 2 : Delete a Node to Linked List (Start, End or Location).");
printf("n Enter 3 : Traverse all Nodes in Linked List");
printf("n Enter Any other Key to Exit");


printf("nEnter Your Choice: __nn");

scanf("%d", &choice);

switch(choice)
{
case 1:
//insert();
printf("Enter 1: to insert at start/begning nn");
printf("Enter 2: to insert at end nn");
printf("Enter 3: to insert in between nodes nn");
scanf("%d",&ch);
switch(ch)
{
case 1:

// insert at start
printf("Enter a value to addn");
scanf("%d",&data);
insertnode_start(data);


break;
case 2:
// insert at end
printf("Enter a value to addn");
scanf("%d",&data);
insertnode_end(data);

break;
case 3:
//insert in between nodes
printf("Enter the location where you want to add itemn");
scanf("%d",&l);
printf("Enter a value to addn");
scanf("%d",&data);
insertnode_loc(l,data);
break;
default:
printf("%d:invalid option. try again...n",ch);
}

break;
case 2:
//delete();

printf("Enter 1: to delete at start/begning nn");
printf("Enter 2: to delete at end nn");
printf("Enter 3: to delete in between nodes nn");
scanf("%d",&ch);
switch(ch)
{
case 1:

// delete at start
deletenode_start();
break;
case 2:
// delete at end
deletenode_end();

break;
case 3:
//delete in between nodes
printf("Enter the location where you want to delete itemn");
scanf("%d",&l);
deletenode_loc(l);
break;

default:
printf("%d:invalid option. try again...n",ch);
}

break;
case 3:
traverse_llist();
break;

default:
exit(0);
}
} while(choice);
}

 

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button