linked list
linked list
Lecture 10
By: Dr. Dawlat Mustafa Sulaiman
Duhok Polytechnic University
Technical College of Administration
Outline
Linked List
Linked List Representation
Types of Linked List
Basic Operations
How to create a LinkedList?
Array versus Linked Lists
Applications of linked list
What is a Linked List?
A linked list is a sequence of data structures, which
are connected together via links.
Successive elements are connected by pointers.
◦ The last element points to NULL.
◦ It can grow or shrink in size during the execution
of a program.
◦ It can be made just as long as required.
◦ It does not waste memory space.
head
A B C
Linked List Representation
Linkedlist can be visualized as a chain of nodes,
where every node points to the next node.
head
A B C
2- Circular linked list
• The pointer from the last element in the list
points back to the first element.
head
A B C
3- Doubly linked list
• Pointers exist between adjacent nodes in
both directions.
• The list can be traversed either forward or
backward.
• Usually two pointers are maintained to keep
track of the list, head and tail.
head tail
A B C
Circular Doubly Linked List
head tail
A B C
Basic Operations
Following are the basic operations supported by a list:
A B C
Item to be
X inserted
X A B C
tmp curr
Single Linked List(add at the
beginning)
/* Program of Link list */
#include<iostream.h>
struct node
{ int data;
node *link;
};
node *start;
void addbeg(int x)
{node *p;
p=start;
start=new node;
start->data=x;
start->link=p;
Insertion node to the specific position in Linked List
A B C
Item to be
tmp X inserted
A B C
curr
X
Insert node to the specific position in Linked List
void insertafter(int x)
{node *p,*t;
int z;
p=start;
cout<<"insert after:";
cin>>z;
while(p->data!=z)
p=p->link;
t=new node;
t->data=x;
t->link=p->link;
p->link=t;
}
Add node to end of Linked
void addlast(int x)List
{node *p,*t;
if(start==NULL) // empty list
{ start=new node;
start->data=x;
start->link=NULL;}
else
{ p=start; // not empty list
while(p->link!=NULL)
{ p=p->link; }
t=new node;
t->data=x;
t->link=NULL;
p->link=t;
}
Display the Linked List
void display()
{node *p;
p=start;
if(p==NULL)
{cout<<"no data in the list";}
while(p!=NULL)
{
cout<<" "<<p->data;
p=p->link;
}
cout<<endl;
}
Delete first node
void delbeg()
{node *p;
p=start;
if(p==NULL)
{cout<<"no data in the
list";}
start=p->link;
delete p;
}
Delete first
node( cont.)
Delete last node
void dellast()
{ node *p;
p=start;
if(p==NULL) // empty list
{ cout<<"no data in the list";}
if(p->link==NULL) // only one node in
the list
{ start=p->link;
delete p;
}
while(p->link->link!=NULL) // more than one node in
the list
{
p=p->link;
}
delete node from the specific position in Linked List
Item to be deleted
A B C
tmp
curr
A B C
delete node from the specific position in Linked
List
void deletevalue(int value)
{node *p,*q;
p=start;
while(p->data!=value)
{
q=p;
p=p->link;
}
q->link=p->link;
delete p;
}
int i,k,c;
do{cout<<"enter your choice:\n";
cout<<"1- enter the number in the beg:\n";
cout<<"2- enter the number in the last:\n";
cout<<"3- delete from the beging:\n";
cout<<"4- delete from the last:\n";
cout<<"5- enter the number after selected node:\n";
cout<<"6- delete value:\n";
cout<<"7- dispaly:\n";
cout<<"8- Exit:\n";
cin>>c;
if(c==1) {cout<<"enter the number in beg:"; cin>>k;
addbeg(k); }
if(c==2) { cout<<"enter the number in the last:"; cin>>k;
addlast(k);}
if(c==3) { delbeg(); }
if(c==4) { dellast(); }
if(c==5) {cout<<"enter the number to add after selected node:";
cin>>k; insertafter(k); }
if(c==6) { cout<<"enter the value to delete:";
cin>>k; deletevalue(k); }
if(c==7) {display(); }
Array versus Linked Lists
S.N
o. ARRAY LINKED LIST
An array is a grouping of
data elements of equivalent A linked list is a group of entities called a node. The
1. data type. node includes two segments: data and address.
It stores the data elements
in a contiguous memory It stores elements randomly, or we can say anywhere in
2. zone. the memory zone.
2. Implementation of graphs
6. Image viewer – Previous and next images are linked, hence can be accessed by the next and
previous buttons.
7. Previous and next page in a web browser – We can access the previous and next url searched
in the web browser by pressing the back and next buttons since they are linked as a linked list.
8. Music Player – Songs in the music player are linked to the previous and next songs. you can