0% found this document useful (0 votes)
0 views

linked list

The document is a lecture on Linked Lists by Dr. Dawlat Mustafa Sulaiman, covering their definition, representation, types, basic operations, and comparisons with arrays. It explains various types of linked lists such as singly, circular, doubly, and circular doubly linked lists, along with operations like insertion, deletion, and display. Additionally, it discusses applications of linked lists in data structures and real-world scenarios.

Uploaded by

fxhrvg6pj6
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)
0 views

linked list

The document is a lecture on Linked Lists by Dr. Dawlat Mustafa Sulaiman, covering their definition, representation, types, basic operations, and comparisons with arrays. It explains various types of linked lists such as singly, circular, doubly, and circular doubly linked lists, along with operations like insertion, deletion, and display. Additionally, it discusses applications of linked lists in data structures and real-world scenarios.

Uploaded by

fxhrvg6pj6
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/ 27

Data Structure

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.

As per the above illustration, following are the important


points to be considered.
•Linked List contains a link element called Head (first).
•Each link carries a data field(s) and a link field called next.
•Each link is linked with its next link using its next link.
•Last link carries a link as null to mark the end of the list.
Types of Linked Lists

1) Single Linked List


2) Circular Linked List
3) Doubly Linked List
4) Circular Doubly Linked List
1- Simple Linked List
o Linear singly-linked list (or simply linear list),
Item navigation is forward only.

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:

• Insertion − Adds an element at the beginning of the list.

• Deletion − Deletes an element at the beginning of the list.

• Display − Displays the complete list.

• Search − Searches an element using the given key.

• Delete − Deletes an element using the given key.


Insertion Operation
Adding a new node in the linked list is a more than
one step activity. First, create a node using the
same structure and find the location where it has to
be inserted.

Similar steps should be taken if the node is being


inserted at the beginning of the list. While
inserting it at the end, the second last node of the
list should point to the new node and the new node
will point to NULL.
Illustration: Insertion at the beginning

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.

In the case of an array,


memory size is fixed, and it
is not possible to change it In the linked list, the placement of elements is allocated
3. during the run time. during the run time.

The elements are not


4. dependent on each other. The data elements are dependent on each other.
The memory is assigned at
5. compile time. The memory is assigned at run time.
It is easier and faster to
access the element in an In a linked list, the process of accessing elements takes
6. array. more time.
In the case of an array,
memory utilization is In the case of the linked list, memory utilization is
7. ineffective. effective.

When it comes to executing


any operation like insertion,
deletion, array takes more When it comes to executing any operation like insertion,
8 time. deletion, the linked list takes less time.
Applications of linked list
1. Implementation of stacks and queues

2. Implementation of graphs

3. Dynamic memory allocation

4. Maintaining a directory of names

5. Performing arithmetic operations on long integers

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

play songs either from starting or end of the list


Thank You

You might also like