23 Aug 2019

  • August 23, 2019
  • Amitraj

Singly Linked List Data Structure

-> Singly linked lists contain nodes which have a data part as well as an address part i.e. next, which points to the next node in the sequence of nodes.

-> Operations that can be performed on singly linked lists include creation, insertion, deletion and traversal.






/* C program to demonstrate SLL(singly linked list) and  perform create,insert,Delete,length and show operations..*/        
              

#include<stdlib.h>
#include<stdio.h>
struct node
{
int info;
struct node *link;

}*head=NULL,*t,*temp;


void create()

{
t=(struct node *)malloc(sizeof(struct node));
printf("Enter  a node value:");
scanf("%d",&t->info);
t->link=head;
head=t;
}

void insert()

{
int p,l,i=0;
printf("Enter a position to insert:");
scanf("%d",&p);
l=size();
if(p>l+1||p<=0)
printf("Not a valid position...:");
else
{
printf("Enter a new node to insert:");
temp=(struct node *)malloc(sizeof(struct node));
scanf("%d",&temp->info);
if(p==1)
{
temp->link=head;
head=temp;
}
else
{
t=head;
for(i=1;i<p-1;i++)
t=t->link;
temp->link=t->link;
t->link=temp;
}

}

printf("Node inserted..\n");
}
void del()
{
int p,l,i;
printf("Enter a  position to Delete:");
scanf("%d",&p);
l=size();
if(p>l||p<=0)
{
printf("Invalid position..\n");
return;
}
else
{
      if(p==1)
      {
      t=head;
      head=head->link;
      free(t);
  }
  else
  {
  t=head;
  for(i=1;i<p-1;i++)
  t=t->link;
  temp=t->link;
  t->link=temp->link;
  free(temp);
  }
}
printf("Node deleted..\n");

}

int size()

{
int i=0;
for(t=head;t!=NULL;t=t->link)
   i++;
   return i;
}

void show()

{
printf("SLL Elements Are:\n");
for(t=head;t!=NULL;t=t->link)
{
printf("%d ",t->info);
}

}


int main()

{
int n;
do
{
printf("::::SLL Operation::::\n");
printf("1.create 2.insert 3.Delete 4.Length 5.Show 6.exit\n");
printf("Enter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: create(); break;
case 2: insert(); break;
case 3: del(); break;
case 4: printf("SLL Size is= %d\n",size()); break;
case 5: show(); break;
case 6: return;

}
}while(n!=6);

}

//INPUT/OUTPUT:-

::::SLL Operation::::

1.create 2.insert 3.Delete 4.Length 5.Show 6.exit

Enter your choice:1

Enter  a node value:11
::::SLL Operation::::
1.create 2.insert 3.Delete 4.Length 5.Show 6.exit

Enter your choice:1

Enter  a node value:22
::::SLL Operation::::
1.create 2.insert 3.Delete 4.Length 5.Show 6.exit

Enter your choice:1

Enter  a node value:33
::::SLL Operation::::
1.create 2.insert 3.Delete 4.Length 5.Show 6.exit

Enter your choice:5

SLL Elements Are:
33 22 11 ::::SLL Operation::::
1.create 2.insert 3.Delete 4.Length 5.Show 6.exit

Enter your choice:4

SLL Size is= 3
::::SLL Operation::::
1.create 2.insert 3.Delete 4.Length 5.Show 6.exit

Enter your choice:3

Enter a  position to Delete:2
Node deleted..
::::SLL Operation::::
1.create 2.insert 3.Delete 4.Length 5.Show 6.exit

Enter your choice:4

SLL Size is= 2
::::SLL Operation::::
1.create 2.insert 3.Delete 4.Length 5.Show 6.exit

Enter your choice:5

SLL Elements Are:
33 11 ::::SLL Operation::::
1.create 2.insert 3.Delete 4.Length 5.Show 6.exit

Enter your choice:6

____...

Translate

Popular Posts