0% found this document useful (0 votes)
32 views31 pages

Arrays and List: A List Is A Set of Items. Anarrayisasetof Variables That Each Store An Item

This document compares and contrasts arrays and lists, and then discusses linked lists. It explains that in a list, empty spots are filled in when deleting an item, while in an array an empty variable is left behind. It then defines a linked list as a dynamic data structure where each node contains a pointer to the next node. It discusses various operations like insertion and deletion in singly linked lists. It also discusses stacks and queues implemented using linked lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views31 pages

Arrays and List: A List Is A Set of Items. Anarrayisasetof Variables That Each Store An Item

This document compares and contrasts arrays and lists, and then discusses linked lists. It explains that in a list, empty spots are filled in when deleting an item, while in an array an empty variable is left behind. It then defines a linked list as a dynamic data structure where each node contains a pointer to the next node. It discusses various operations like insertion and deletion in singly linked lists. It also discusses stacks and queues implemented using linked lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Arrays and list

A list is a set of items.

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)

• Head->External pointer variable.It holds the


address of first node
• if(head==null)
List is empty
• The last node has a reference to null
Linled list in memory

In a linked list, every node contains a pointer to another node


which is of the same type, it is also
called a self-referential data type.
Two list are simultaneously maintained
in memory

Finout the roll nos of the


students who opted for biology
and computer science and
draw the linked list
Memory allocation and deallocation in
linked list
Types of linked list
• singly linked list

• 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

struct stack struct queue


{ {
int info; int info;
struct stack *next; struct queue *next,*front,*rear;
}*p,*start=NULL; }*p,*q;
Insertion(contd...)
• p=getnode() void enqueue()
push()
• info(p)=9 {
{
• next(p)=start p=malloc(sizeof(struct
p=malloc(sizeof(struct node))
• start=p node));
printf(“Enter the info of p”) printf(“Enter the info of p”)
scanf(“%d”,&p->info) scanf(“%d”,&p->info)
p->next=NULL; p->next=NULL;
if(start==NULL) if(q->rear==NULL)
{ {
start=p; q->front=p;
} q->rear=p;
else }
else
{
{
p->next=start;
else
start=p p->next=q->rear;
}} q->rear=p;
}}
Deletion(contd...)
• p=start
• start=next(p)
void dequeue()
• x=info(p) pop()
{
• free(p) int x;
{
if(start==NULL) if(q->front==NULL)
{
{ printf(“queue empty”)
stack empty }
} else
else {
{ p= q->front;
q->front=p->next;
p=start x=p->info;
start=p->next printf("\nDequeued element is
printf(“ info of p%d”,p->info) %d",x);
free(p) free(p);
}} }
Display(contd...)
display()
{ void display()
{
if(start==NULL) p=q->front;
{ if(p==NULL)
stack empty printf("\nQueue is empty");
} else
else {
while(p!=NULL)
{ {
p=start printf("%d-> ",p->info);
while(p!=NULL) p=p->next;
{ }
printf(“ %d”,p->info) }
}
p=p->next
}}
Circular linked list
primitive operations
• insertafter()

• 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

You might also like