Sunteți pe pagina 1din 12

Introducti

Data Structureconsisting
of a group ofnodeswhich together
on
represent a sequence.

Simplest and most common data structure used for creating


other types data structures.

What is linked list node ?


Linked list is nothing but series of nodes.

Data
Part

Pointer
Part

Node in Linked List

Node consist of two parts vizData Part & Pointer Part.


Pointer Part stores theaddress of the next node.

Let us now take a look at a linked list with three nodes :

Node 1 has two part one data part which consists of the 5
as data and the second part which contain the address of the
next node (i.e it contain the address of the next node)
Node 3, being last node of the list holds NULL value in its second part.

Doubly linked list


In Doubly Linked List ,each node contain two address
fields.
One address field for storing address ofnext nodeto be
followed and second address field contain address
to it.
ofprevious
SoTwo way nodelinked
access is possiblei.e
we can start accessing
nodes from start as well as from last .
Elements are accessed sequentially ,no direct accessis allowed.

Advantages

Insertion and Deletionoperations are easier.


Linked Listcan grow and shrink during run time.
Efficient Memory Utilization, i.e. no need to pre-allocate memory.
Faster access time ,can be expanded inconstant time without memory ove
Linear Data Structures such as Stack, Queue can beeasily
implementedusing linked list.

Limitations
Access time for individual element isO(n).
In linked list , each node should be accessed sequentially .
Difficulties arise in linked lists when it comes to reverse traversing.
Usage more memory due topointersrequiring extra storage space.

Creating a linked list


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
struct node{
int data;
struct node *next;
}*start=NULL;
void main( )
{
char ch;
do
{
struct node *new_node,*current;
new_node=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the data : ");
scanf("%d",&new_node->data);
new_node->next=NULL;

if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
current->next=new_node;
current=new_node;
}
printf("\nPress n to stop creating next node");
ch=getche();
}while(ch!='n');
}

Inserting node at front


void insert( )
{
struct node *new_node,*current;
new_node=(struct node *)malloc(sizeof(struct node));
if(new_node == NULL)
printf("nFailed to Allocate Memory");
printf(\nEnter the data : ");
scanf("%d",&new_node->data);
new_node->next=NULL;
if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
new_node->next=start;
start=new_node;
}
}

Deleting node from front


void delete( )
{
struct node *temp;
temp = start;
start = start->next;
free(temp);
printf(\nDeletion Successful");
}

Display linked list from fi rst


void display ( )
{
struct node *temp;
temp=start;
printf(The elements are:);
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->next;
}
}

Conclusion
Hence, linked list is a simplest form of data structure that could
be used to to implement several other commonabstract data
types,
includinglists(the
abstract
data
type),stacks,queues,associative arrays, etc.
Due to its dynamic nature , we only have to just create linked
list structure and memory will be allocated at run time. We
wont have to allocate memory at compile time. Memory is
allocated at run time as per requirement, so that Linked list
data structure provides us strong command on memory
utilization.

Reference
1.
2.
3.
4.

http://cse.iitkgp.ac.in/pds/semester/2009a/slides/l9-linkedlist.pdf (Accessed on 2015-07-24 )


http://www.bowdoin.edu/~ltoma/teaching/cs210/fall09/Slides/LinkedLists.pdf (Accessed on 2015http://www.cs.princeton.edu/courses/archive/spr01/cs126/lectures/P8-4up.pdf (Accessed on 2015
http://cslibrary.stanford.edu/103/LinkedListBasics.pdf (Accessed on 2015-07-27 )

S-ar putea să vă placă și