Create Database Using Singly Linked List with all operations like insert at any position,delete any node,display,display in reverse & count no of nodes.

  Data Structure : Singly Linked list


Singly linked list is a basic linked list. Singly linked list is a collection of nodes linked together in a sequential way where each node of singly linked list contains a data field and an address field which contains the reference of the next node. Singly linked list can contain multiple data fields but should contain at least single address field pointing to its connected nextnode.

Basic structure of a singly linked list

Each node of a singly linked list follows a common basic structure. In a node we can store more than one data fields but we need at least single address field to store the address of next connected node.
struct node   {
      int data; //data part        
      struct node * next; //next address part
  };

Advantages of Singly linked list:


  • Singly linked list is probably the most easiest data structure to implement.
  • Insertion and deletion of element can be done easily.
  • Insertion and deletion of elements doesn't requires movement of all elements when compared to an array.
  • Requires less memory when compared to doubly, circular or doubly circular linked list.
  • Can allocate or deallocate memory easily when required during its execution.
  • It is one of most efficient data structure to implement when traversing in one direction is required.

              Disadvantages of Singly linked list

After seeing the advantages of singly linked list. Singly linked list also has some disadvantages over other data structures.
  • It uses more memory when compared to an array.
  • Since elements are not stored sequentially hence requires more time to access each elements of list.
  • Traversing in reverse is not possible in case of Singly linked list when compared to Doubly linked list.
  • Requires O(n) time on appending a new node to end. Which is relatively very high when compared to array or other linked list.

    Program of singly linked list.
#include <stdio.h>#include <malloc.h>
#include <stdlib.h>
#include<conio.h>
typedef struct node
{
  int rollno;
  struct node *next;
};

void insert();
void display();
void displayrev();
void deleted();
int count();
typedef struct node n1;

n1 *head_node,*p, *first_node, *temp_node = 0, *prev_node, *next_node;
int data;

int main()
{
  int option = 0;
  clrscr();
  printf("Singly Linked List Example - All Operations\n");

  while (option < 6)
   {

    printf("\nOptions\n");
    printf("1 : Insert into Linked List \n");
    printf("2 : Delete from Linked List \n");
    printf("3 : Display Linked List\n");
    printf("4 : Count Linked List\n");
    printf("5 : display Linked List in reverse\n");
    printf("Others : Exit()\n");
    printf("Enter your option:");
    scanf("%d", &option);
    switch (option)
       {
   case 1: insert();
     break;
   case 2: deleted();
    break;
   case 3: display();
    break;
   case 4:count();
    break;
   case 5:displayrev();
    break;
   default:break;
       }
  }
  getch();
  return 0;
}

void insert()
{
 int n,a;
 printf("\nEnter Element for Insert Linked List : \n");
  scanf("%d", &data);

  temp_node = (n1*) malloc(sizeof (n1));

  temp_node->rollno = data;

  if (first_node == 0)
  {
    first_node = temp_node;
  }
  else
  {
  printf("\nEnter to insert");
  printf("\n1 to insert at first");
  printf("\n2 to insert at last");
  printf("\n3 to insert in between");
  scanf("%d",&n);
  switch(n)
  {
    case 1: temp_node->next=first_node;
     first_node=temp_node;
     head_node=first_node;
     break;
    case 2:p=first_node;
    while(p->next!=NULL)
       {
  p=p->next;
        }
    p->next=temp_node;
    temp_node->next=NULL;
    break;
    case 3:printf("\nEnter the position rollno  to insert a node\n");
    scanf("%d",&a);
    p=first_node;
    while(p->rollno!=a)
    {
    p=p->next;
    }
    temp_node->next=p->next;
    p->next=temp_node;
    break;

  }
  }
  fflush(stdin);
}

void deleted()
{
  int countvalue, pos, i = 0;
  countvalue = count();
  temp_node = first_node;
  printf("\nDisplay Linked List : \n");

  printf("\nEnter Position for Delete Element : \n");
  scanf("%d", &pos);

  if (pos > 0 && pos <= countvalue)
  {
    if (pos == 1)
    {
      temp_node = temp_node -> next;
      first_node = temp_node;
      printf("\nDeleted Successfully \n\n");
    }
     else
     {
      while (temp_node != 0)
  {
 if (i == pos)
  {
   prev_node->next = temp_node->next;
   if(i ==countvalue)
       {
     head_node = prev_node;
       }
     printf("\nDeleted Successfully \n\n");
     break;
   }
 else
  {
     i++;
     prev_node = temp_node;
     temp_node = temp_node -> next;
  }
      }
    }
  } else
    printf("\nInvalid Position \n\n");
}

void display()
{
  int count = 0;
  temp_node = first_node;
  printf("\nDisplay Linked List : \n");
  while (temp_node != 0)
  {
    printf("%d" , temp_node->rollno);
    count++;
    temp_node = temp_node -> next;
  }
  printf("\nNo Of Items In Linked List : %d\n", count);
}
void displayrev()
{
  int a[20]={0};
  int count = 0;
  temp_node = first_node;
  printf("\nDisplay Linked List : \n");
  while (temp_node != 0)
  {
   a[count]=temp_node->rollno;
    count++;
    temp_node = temp_node -> next;
  }
  --count;
 for(;count>=0;count--)
 {
 printf("%d",a[count]);
 }
}
int count()
{
  int count = 0;
  temp_node = first_node;
  while (temp_node != 0)
  {
    count++;
    temp_node = temp_node -> next;
  }
  printf("\nNo Of Items In Linked List : %d\n", count);
  return count;
}

No comments:

Post a Comment

If you have any problems related to solutions or any concept please let me know.

Copyright (c) 2020 Custom Programs All Right Reserved

Pages