Arrays and List: A List Is A Set of Items. Anarrayisasetof Variables That Each Store An Item
Arrays and List: A List Is A Set of Items. Anarrayisasetof Variables That Each Store An Item
An array is a set of
variables that each
store an item.
Arrays and Lists
In a list, the missing spot is filled in when something
is deleted.In an array, an empty variable is left
behind when something is deleted.
Linked list
• A linked list is a dynamic data structure
• Each node/element comprises of two items
– Data /Information
– Next(Reference to the next node)
• Doubly linked list is a list that has two references, one to the next node
and another to previous node
• Circular linked list where last node of the list points back to the first node
(or the head) of the list
Singly linked List operations
• In C, we can implement a linked list using the
following code:
struct node
{
int info;
struct node *next;
}*p,*start=NULL;
Insertion/Deletion in singly linked list
• At the beginning
• Intermediate node
• At the end
Functions:
• addbeg()
• addmid()
• addend()
At the beginning(insertion)
add at end add as an intermediate node
• p=getnode() • p=getnode()
• info(p)=9 • info(p)=9
• next(start)=p • next(p)=next(start)
• next(p)=NULL • next(start)=p
Insertion at the beginning(contd...)
void addbeg()
{
temp=(struct node*)malloc(sizeof(struct node
• p=getnode() ));
• info(p)=9 printf("\nEnter the elements to be inserted ");
scanf("%d",&temp->data);
• next(p)=start temp->next=NULL;
• start=p if(head==NULL)
{
head=temp;
}
else
{
temp->next=head;
head=temp;
}}
void addend()
{
temp=(struct node*)malloc(sizeof(struct node ));
printf("\nEnter the element to be inserted ");
scanf("%d",&temp->data);
temp->next=NULL;
if(head==NULL)
{
head=temp;
head->next=head;
return;
}
q=head;
while (q->next!=NULL)
{
q=q->next;
}
q->next=temp;
}
void addmid()
{
int i,j,x;
if(head==NULL)
{
addbeg();
return;
}
printf("\nEnter the position ");
scanf("%d",&j);
if(j==1)
{
addbeg();
return;
}
q=head;
for(i=1;i<j-1;i++)
{
q=q->next;
}
temp=(struct node *)malloc(sizeof(struct node ));
printf("\nEnter the element to be inserted ");
scanf("%d",&temp->data);
temp->next=q->next;
q->next=temp;}
At the beginning(Deletion)
Deletion(contd...)
• p=start if(x==1)
{
• start=next(p) q=head;
head=head->next;
• x=info(p) free(q);
void del() }
• free(p) else
{ {
int x,i; q=head;
for(i=1;i<x-1;i++)
if(head==NULL) {
q=q->next;
{ }
printf("\nList is empty"); temp=q->next;
q->next=temp->next;
exit(0); free(temp);
}}
}
printf("\nEnter the position to be
deleted");
scanf("%d",&x);
Display(contd...)
void display()
{
if(head==NULL)
{
printf("\n The list is empty \n");
return;
}
printf("The elements are\n");
q=head;
while(q!=NULL)
{
printf("%d",q->data);
printf(" ");
q=q->next;}}
Disadvantage
• One disadvantage of a linked list against an array
is that it does not allow direct access to the
individual elements. If you want to access a
particular item then you have to start at the head
and follow the references until you get to that
item.
• Another disadvantage is that a linked list uses
more memory compare with an array - we extra 4
bytes (on 32-bit CPU) to store a reference to the
next node.
stack and queue using linked list
creation/ Implementation/
Representation of stack and queue
using linked list
• deleteafter()
insertafter(struct node deleteafter(struct node *p, int x)
*p, int x) {
if (p==NULL)
{ {
q=getnode() printf(“void deletion”)
q->info=x }
q=p->next
p->next=q->next p->next=q->next
p->next=q x=q->info
} free(q)
}
stack as circular linked list(insertion)
push(struct stack *pstack,int x) singly LL
{ push()
p=getnode() {
p->info=x; p=malloc(sizeof(struct node))
if(pstack==NULL) printf(“Enter the info of p”)
{ scanf(“%d”,&p->info)
pstack=p; p->next=NULL;
pstack=pstack->next; if(start==NULL)
} {
else start=p;
{ }
p->next=pstack; else
pstack->next=p; {
}} p->next=start;
start=p
}}
stack as CLL Deletion(contd...)
pop(pstack) Singly LL
{ pop()
if(pstack==NULL) {
{ if(start==NULL)
list empty {
} stack empty
else }
{ else
p=pstack->next {
pstack->next=p->next p=start
printf(“ info of p%d”,p->info) start=p->next
free(p) printf(“ info of p%d”,p->info)
}} free(p)
}}
WORK OUT QUEUE AS CIRCULAR LIST
Header node
• info and next
• No real data is stored in info field
• Info(header node)=number of nodes in linked list