Singly Linked List Implementation
Linked List is an another Abstract Data type
- A linked list is a data structure which contain the list of the elements
- The elements are stored anywhere in the memory and linked with the address.
- Linked list is used when the quantity of the data is unknown
- Data is stored in the form of node and at the run-time memory is allocate for creating a node
- Data is accessed using the starting pointer of the list
Array versus Linked List
Array suitable for
- Randomly access the elements
- Searching the list of particular elements
- Inserting or Deleting the particular elements
Linked List suitable for
- Inserting/Deleting the elements
- Application where sequential access is required
- In situation where number of elements are unknown
Types of Linked List
- Singly linked List
- Doubly Linked List
- Circular Linked List
Application of Linked List
- Linked list is used for implementation of other ADT (Stack,Queue,etc)
- Linked list is used for Implementation of graph
- Linked list allows dynamic allocation
- Linked list efficiently manages the memory utilization
- Linked list (circular) help in multi programming
- Undo operation of MS word and Photoshop is manage by Linked list.
Implementation of Singly Linked List
- Singly linked list is collection of the node with connected links
- Data field hold the data and address field store the address of next node
- Address field of the last node hold the NULL.
- Head pointer always point to the first node of the list
First step of implementation
- Design the model of the node with data and address field using structure in C
- For each node implementation dynamically allocate the memory
- Assign a Head Pointer to point to the first node of the list
- 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
- Print a list will take O(n)
- Find the K th element within the list will take O(n)
- Inserting and deleting will take O(1)
- Inserting/deletion of node is easier than finding the k the element in the list
Problem with Single Linked List
- Singly linked list allow the traversal of the list in single direction only
- Deleting a node need to keep the address of the previous node
- 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
This code you can get it through below link
https://drive.google.com/open?id=0B2O8tJ9QXc-3MHNFTzBDMjFvTVE
Comments
Post a Comment