DATASTRUCTURE
DATRUCTURE IS THE WAY OF
ORGANIZING ALL DATA ITEMS IN
ORDER THAT NOT ONLY ELEMENTS TO
BE STORED BUT ALSO THE RELATION
BETWEEN THE ELEMENTS
DATASTRUCTURE
LINEAR
LINKLIST
ARRAY
STACK
QUEUE
NONLINEAR
TREES&
GRAPH
Linked List
What are the problems with Arrays
- Size is fixed
-Array Items are stored contiguously
-Insertions and deletion at particular position is complex
Why Linked list ?
-Size is not fixed
-Data can be stored at any place
-Insertions and deletions are simple and faster
What is Linked List?
A linked list is a collection of nodes with various fields
It contains data field and Address field or Link field
Info field
Link Field/
Address Field
Pointer to the
first node
Linked List Types
Singly Linked list
Circular singly linked list
Doubly linked lists
Circular doubly linked lists
Graphical Representation
10
1000
1000
2000
2000
15 NULL
20
4000
Singly Linked List
First
Graphical Representation
10
1000
1000
2000
2000
15 4000
20
4000
Circular Singly Linked List
Last node contains the address of the first node
First
Doubly Linked list
Contains the address of previous node
and next node
NULL
2000 3000
1000
10 15 20
2000 1000 2000 NULL
3000
First
Circular Doubly Linked list
Contains the address of first node and
last node
3000
2000 3000
1000
10 15 20
2000 1000 2000 1000
3000
First
Representation of Singly Linked list
struct node
{
int info;
struct node *link
};
typedef struct node *NODE;
Meaning of this: A node is of the type struct and
contains info field and link filed
What is typedef ?
typedef is a keyword in the C Language used to give
the new name to data types
The intent is to make it easier for programmers
Example typedef int x;
Later on you can use x as a data type
x a, b;
Means a and b are variables of the type integer
Graphical Representation
10
1000
1000
2000
2000
15 NULL
20
4000
Operations on Singly Linked List
First
Insert
Delete
Display the contents
Allocation of memory to newnode
newnode =( NODE ) malloc (sizeof(struct node));
It Returns address of the location with piece of
memory of the size struct. That is for information
field and address field
Algorithm to Insert an Element from the front
1 temp=allocate memory
2 temp(info)=item;
3 link(temp)=first;
4 call temp as first
5 temp <- newnode
6 return first;
temp
30
Temp node with item
NULL
NULL NULL
10
1000
1000
2000
2000
15 NULL
20
4000
Operations on Singly Linked List
Insert from the front
First
temp
temp->link=first,
10
1000
1000
2000
2000
15 NULL
20
4000
Operations on Singly Linked List
Insert from the front
First
first=temp
Algorithm to Display the content
// Algorithm Display
if (first==NULL)
write list empty
return;
Write “ Contents of the List ”
temp=first
While(temp!=NULL)
{
write (info(temp))
temp=link(temp)
}
C Function to Insert an Element from
the front
Insertfront(NODE first int item)
{
NODE temp;
temp=(NODE) malloc(sizeof(struct node));
temp->info=item;
temp->link=NULL ;
temp->link=first;
first=temp;
return(temp);
}
Algorithm to Delete from the front
1 if (first==NULL)
2 Write list empty and return first
3 temp=first
4 first=link(first)
5 free(temp)
6 return(first)
C Function to Delete the element from the
front
NODE deletefront(NODE first)
{
if (first==NULL)
{ printf(List is empty..n”);
return first;
}
temp=first;
first=first->link;
printf(“ The ietm deleted is %d”,temp->info);
free(temp);
return(first);
}
10
1000
1000
2000
2000
15 NULL
20
4000
Operations on Singly Linked List
Delete from the front
Temp/
First
temp=first
10
1000
1000
2000
2000
15 NULL
20
4000
Operations on Singly Linked List
Delete from the front
First
temp
temp=first, first=first->link
1000 2000
2000
15 NULL
20
Operations on Singly Linked List
Delete from the front
First
Stack Using Linked List
CONCEPT : LIFO
Inserting an element from front and
deleting an element from front is nothing
but STACK
30 Null
top
3000
Push item 30
S
T
A
C
K
L
I
N
K
E
D
L
I
S
T
30 Null
top
20
3000
2000
3000
Push item 20
30 Null
top
20
3000
2000 3000
10
2000
1000
Push item 10
30 Null
top
20
3000
2000 3000
POP 10
30 Null
top
3000
POP 20
POP 30
STACK EMPTY
Inserting an Element from the rear and deleting
An element from the front is nothing but Q
Implementation of Q using Linked List
20
temp
Front=null
Front=rear=temp
NULL
Insert 20
20
Inserting 20
NULL
Front/
Rear
20
front/rear
Inserting 30
NULL
rear->link=temp
temp
30 null
2000
20
front
Inserting 30
2000
rear->link=temp
rear=temp
rear
30 null
2000
1000
20
front
Inserting 40
2000
rear->link=temp
rear=temp
rear
30 null
2000
1000
40 Null
20
front
Inserting 40
2000
rear->link=temp
rear=temp
rear
30 3000
2000
1000
40 Null
3000
20
front
Deleting 20
2000
temp=front
front=front->link
free(temp)
rear
30 3000
2000
1000
40 Null
3000
Delete Operation
front
Deleting 30
rear
30 3000
2000
40 Null
3000
front
Deleting 40
rear
40 Null
3000
Q Empty
10
1000
1000
2000
2000
15 NULL
20
4000
Searching an Item in a
Singly Linked List
first
Cur=first
Pos=1;
while(cur!=NULL && item!=cur->info)
cur=cur->link;
pos++;
key=20
cur
Cur->info=10
10
1000
1000
2000
2000
15 NULL
20
4000
Searching an Item in a
Singly Linked List
key =30
First
Cur=first
Pos=1;
while(cur!=NULL && item!=cur->info)
cur=cur->link;
pos++;
Cur
Cur->info = 15 Key=30
10
1000
1000
2000
2000
15 NULL
20
4000
Searching an Item in a
Singly Linked List
First
Cur=first
Pos=1;
while(cur!=NULL && item!=cur->info)
cur=cur->link;
pos++;
Cur
Now Item = 20 , key found at position =3
C Function to search an Item in the list
NODE search(NODE first, int key)
{
NODE cur;
int pos;
if(first==NULL)
{ printf(“ List Empty Search Not Possible…..n”);
return;
}
Cur=first;
pos=1;
while( cur != NULL && key!=cur->info)
{
cur=cur->link;
If(cur==NULL)
printf(“ Item not found…n”);
else
printf(“ Item found at position .. %d”,pos);
}
Inserting an Element at any Position
10 20
2000
2000 NULL
21 ANIL 03
2000
2000 22 sunil 3 NULL
first
Each Student Record in a Node
1000
Representation of the Record using Linked List
struct student
{
int id;
char name[20];
int sem;
struct student link;
};
typedef struct student *NODE;
dokumen.tips_linked-list-ppt-5584a44be6115.pptx

dokumen.tips_linked-list-ppt-5584a44be6115.pptx