Singly Linked List Implementation

Linked List is an another Abstract Data type
  1. A linked list is a data structure which contain the list of the elements
  2. The elements are stored anywhere in the memory and linked with the address.
  3. Linked list is used when the quantity of the data is unknown
  4. Data is stored in the form of node and at the run-time memory is allocate for creating a node
  5. Data is accessed using the starting pointer of the list

Array versus Linked List

Array suitable for 
  1.        Randomly access the elements
  2.        Searching the list of particular elements
  3.        Inserting or Deleting the particular elements
Linked List suitable for
  1.        Inserting/Deleting the elements
  2.        Application where sequential access is required
  3.        In situation where number of elements are unknown

Types of Linked List

  1. Singly linked List
  2. Doubly Linked List
  3. Circular Linked List

Application of Linked List

  1. Linked list is used for implementation of other ADT (Stack,Queue,etc)
  2. Linked list is used for Implementation of graph
  3. Linked list allows dynamic allocation
  4. Linked list efficiently manages the memory utilization
  5. Linked list (circular) help in multi programming 
  6. Undo operation of MS word and Photoshop is manage by Linked list.

Implementation of Singly Linked List

  1. Singly linked list is collection of the node with connected links
  2. Data field hold the data and address field store the address of next node
  3. Address field of the last node hold the NULL.
  4. Head pointer always point to the first node of the list
First step of implementation
  1. Design the model of the node with data and address field using structure in C
  2. For each node implementation dynamically allocate the memory 
  3. Assign a Head Pointer to point to the first node of the list
  4. Decide the function for different types of operations (inserting,deleting, etc.)
#include
#include

struct node
{
int data;
struct node *next;
}*head=NULL,*temp=NULL;

void insertNode()
{
int n;
struct node *newNode;
newNode=(struct node*)malloc(sizeof(struct node));
printf("insert the data\n");
scanf("%d",&n);
newNode->data=n;
if(head==NULL)
{
head=newNode;
newNode->next=NULL;
}
else
{
newNode->next=head;
head=newNode;
}
}

void display()
{
if(head==NULL)
printf("linked list is empty");
else
{
temp=head;
while(temp->next!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("%d\n",temp->data);
}
}

void insertAtend()
{
int n;
struct node *newNode;
newNode=(struct node*)malloc(sizeof(struct node));
printf("insert the data\n");
scanf("%d",&n);
newNode->data=n;
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newNode;
newNode->next=NULL;
}

void delAtf()
{
if(head==NULL)
printf("linked list is empty\n");
else
{
if(head->next==NULL)
{
temp=head;
head=NULL;
free(temp);
}
else
{
temp=head;
head=temp->next;
free(temp);
}
}
}

void main()
{
int choice;
while(1)
{
printf("1:insert the node\n");
printf("2:display the data\n");
printf("3:insert at the end\n");
printf("4:delete from front\n");
printf("5: exit");
printf("------------------------\n");
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
    insertNode();
    break;
case 2:
    display();
    break;
case 3:
    insertAtend();
    break;
case 4:
    delAtf();
    break;
case 5:
    exit(0);
    break;
default :
    printf("invalid choice");
}
}
}

Time complexity Analysis for the Operations

  1. Print a list will take O(n)
  2. Find the K th element within the list will take O(n)
  3. Inserting and deleting will take O(1)
  4. Inserting/deletion of node is easier than finding the k the element in the list

Problem with Single Linked List

  1. Singly linked list allow the traversal of the list in single direction only
  2. Deleting a node need to keep the address of the previous node
  3. If link is lost between the node, then rest of the elements gets corrupted or untraceable
****************************************************************
This code you can get it through below link

https://drive.google.com/open?id=0B2O8tJ9QXc-3MHNFTzBDMjFvTVE

Comments

Popular posts from this blog

Types of Algorithm and Its Complexity Analysis

First Step of Problem Solving Using Data Structure and Algorithm

Asymptotic Notations