0% found this document useful (0 votes)
9 views50 pages

Unit - 3

The document discusses linear data structures, specifically linked lists, detailing their storage representation, operations, and types. It highlights the advantages and disadvantages of linked allocation, including efficiency in insertion and deletion operations but increased memory usage. Additionally, it covers algorithms for manipulating singly linked lists and introduces circularly linked lists, outlining their operations and characteristics.

Uploaded by

ashokkmahato2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views50 pages

Unit - 3

The document discusses linear data structures, specifically linked lists, detailing their storage representation, operations, and types. It highlights the advantages and disadvantages of linked allocation, including efficiency in insertion and deletion operations but increased memory usage. Additionally, it covers algorithms for manipulating singly linked lists and introduces circularly linked lists, outlining their operations and characteristics.

Uploaded by

ashokkmahato2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Data Structures (DS)

GTU # 3130702

Unit-3
Linear Data Structure
(Linked List)

Dept. of B.Sc.CSIT
[email protected]
+977 – 9844071035
Linked Storage Representation
 There are many applications where sequential allocation method is unacceptable because of
following characteristics
 Unpredictable storage requirement
 Extensive manipulation of stored data

 One method of obtaining the address of node is to store address in computer’s main memory,
we refer this addressing mode as pointer of link addressing.
 A simple way to represent a linear list is to expand each node to contain a link or pointer to the
next node. This representation is called one-way chain or Singly Linked Linear List.
Linked Storage Representation
NULL
Last Node Value
A 1000 B 2050 C 3335 D
5000 1000 2050 3335
A linked List
 The linked allocation method of storage can result in both efficient use of computer storage
and computer time.
 A linked list is a non-sequential collection of data items.
 Each node is divided into two parts, the first part represents the information of the element and the second
part contains the address of the next mode.
 The last node of the list does not have successor node, so null value is stored as the address.
 It is possible for a list to have no nodes at all, such a list is called empty list.
Pros & Cons of Linked Allocation
 Insertion Operation
 We have an n elements in list and it is required to insert a new element between the first and second element,
what to do with sequential allocation & linked allocation?
 Insertion operation is more efficient in Linked allocation.

A 1000 B 2050 C 3335 D


5000 1000 2050 3335

A 2100 B 2050 C 3335 D


5000 1000 2050 3335

X 1000
2100
Pros & Cons of Linked Allocation
 Deletion Operation
 Deletion operation is more efficient in Linked Allocation

A 1000 B 2050 C 3335 D


5000 1000 2050 3335

A 2050 B 2050 C 3335 D


5000 1000 2050 3335
Pros & Cons of Linked Allocation
 Search Operation
 If particular node in the list is required, it is necessary to follow links from the first node onwards until the
desired node is found, in this situation it is more time consuming to go through linked list than a sequential
list.
 Search operation is more time consuming in Linked Allocation.

 Join Operation
 Join operation is more efficient in Linked Allocation.

A 1000 B 2050 C 3335 D 5050


5000 1000 2050 3335

12 580 52 5096 15 5145 100


5050 580 5096 5145
Pros & Cons of Linked Allocation
 Split Operation
 Split operation is more efficient in Linked Allocation

A 1000 B 2050 C 3335 D


5000 1000 2050 3335

A 1000 B 2050 C 3335 D


5000 1000 2050 3335

 Linked list require more memory compared to array because along with value it stores pointer
to next node.
 Linked lists are among the simplest and most common data structures. They can be used to
implement other data structures like stacks, queues, and symbolic expressions, etc…
Operations & Type of Linked List
Operations on Linked List Types of Linked List
 Insert  Singly Linked List
• Insert at first position  Circular Linked List
• Insert at last position  Doubly Linked List
• Insert into ordered list
 Delete
 Traverse list (Print list)
 Copy linked list
Singly Linked List

A next B next C next D


NULL
FIRST

 It is basic type of linked list.


 Each node contains data and pointer to next node.
 Last node’s pointer is null.
 First node address is available with pointer variable FIRST.
 Limitation of singly linked list is we can traverse only in one direction, forward direction.
Node Structure of Singly List

Info Link
Accessing Part

Node
of Node
Typical Node Info (Node)
Data Pointer to Link (Node)

Next Node

struct node
{
C Structure to int info;
represent a node struct node *link;
};
Algorithms for singly linked list
1. Insert at first position
2. Insert at last position
3. Insert in Ordered Linked list
4. Delete Element
5. Copy Linked List
Function: INSERT(data, First)
 This function inserts a new node at the first position of Singly linked list.
 This function returns address of FIRST node.
 data is a new element to be inserted.
 head is a pointer to the first element of a Singly linked linear list.
 Typical node contains data and next fields.
 NEWNODE is pointer for new node
Function: INSERT(X,FIRST) Cont…
If(head==null)
{
printf(“linked list is empty”)
}
Else
{
struct node
{
int data;
struct node *head,*newnode;
}
Struct node *head, *newnode;
Newnode=(struct node*)malloc(size of(struct node));
Printf(“enter the data:”);
Scanf(“%d”, &newnode->data);
Newnode->next=head;
Head=newnode;
}
Example: INSERT(50, FIRST)

50 link 10 link 50 link 35 link 20


NEWNODE
HEAD

HEAD  INSERT (DATA, NEWNODE)


Function: INSEND(DATA, HEAD)
 This function inserts a new node at the last position of linked list.
 This function returns address of LAST node.
 DATA is a new element to be inserted.
 HEAD is a pointer to the first element of a Singly linked linear list.
 Typical node contains DATA and NEXT fields.
 TEMP is a temporary pointer variable.
Function: INSEND(X, First) Cont…
If(head==null)
{
printf(“linked list is empty”)
}
Else
{
struct node
{
int data;
struct node *head,*newnode;
}
Struct node *head, *newnode;
Newnode=(struct node*)malloc(size of(struct node));
Printf(“enter the data:”);
Scanf(“%d”, &newnode->data);
Newnode->next = 0;
temp = head;
while(temp->next!=0)
{
temp = temp -> next;
}
temp –> next = newnode;
}
Function: INSEND(50, FIRST)

TEMP

10 50 -1 8 35 44 50
NEW
HEAD
Function: INSORD(X, FIRST)
 This function inserts a new node such that linked list preserves the ordering of the terms in
increasing order of their DATA field.
 This function returns address of NEWNODE node.
 DATA is a new element to be inserted.
 HEAD is a pointer to the first element of a Singly linked linear list.
 Typical node contains DATA and NEXT fields.
 TEMP is a temporary pointer variable.
Predecessor

5 10 15 20 25 30

HEAD
22
NEWNODE
Function: INSORD(X, FIRST)(after given position)
If(head==null)
{
printf(“linked list is empty”);
}
Else
{
int pos; int i = 1;
struct node
{
int data;
struct node *next;
};
strcut node *head,*newnode,*temp;
printf(“enter the position:”);
scanf(“%d”,&pos);
if(pos>count)
{
printf(“invalid position”);
}
else
{
temp = head;
while(i<pos)
{
temp = temp -> next;
i++;
}
newnode = (struct node*) malloc(size of (struct node));
printf(“enter data:”);
scanf(“%d”, &newnode->data);
newnode -> next = temp->next;
temp->next=newnode;
}
}
Procedure: DELETE(data, FIRST)
 This algorithm delete a node whose address is given by variable data.
 HEAD is a pointer to the first element of a Singly linked linear list.
 Typical node contains DATA and NEXT fields.
 TEMP is a temporary pointer variable.
Procedure: del_from_beg( data, head)
If (head == null)
{
printf(“linked list is empty”);
}
Else
{
struct node *temp;
temp = head;
head = head -> next;
free(temp);
}
Function: del_from_end()
 LASTNODE is pointer for the node after the deleted node.
 HEAD is a pointer to the first element of a Singly linked linear list.
 Typical node contains DATA and NEXT fields.
 TEMPis a Temporary pointer variable.
Function: del_from_end()
If (head == null)
{
printf(“the linked list is empty”)
}
Else
{
Struct node *temp, *lastnode;
Temp = head;
While(temp->next != 0)
{
Lastnode = temp;
Temp = temp -> next;
}
If(temp==head)
{
head = 0;
}
Else
{
lastnode -> next = 0;
}
Free(temp);
}
Function: del_from_pos()
 nextnode is pointer for the node to be deleted
 HEAD is a pointer to the first element of a Singly linked linear list.
 Typical node contains DATA and NEXT fields.
 TEMPis a Temporary pointer variable.
 POS is the variable to point the position to delete the node.
Function: del_from_pos()
If(head==null)
{
printf(“linked list is empty”)
}
Else
{
Struct node
{
int data;
struct node *next;
};
Struct node *head, *temp;
del_frm_pos()
{
struct node *nextnode;
int pos,i=1;
temp=head;
Printf(“enter position”);
scanf(“%d”,&pos);
while(i<pos-1)
{
temp = temp->next;
i++;
}
nextnode=temp->next;
temp->next=nextnode->next;
free(nextnode);
}
}
Circularly Linked Linear List
 If we replace NULL pointer of the last node of Singly Linked Linear List with the address of its
first node, that list becomes circularly linked linear list or Circular List.
 FIRST is the address of first node of Circular List
 LAST is the address of the last node of Circular List
 Advantages of Circular List
 In circular list, every node is accessible from given node
 It saves time when we have to go to the first node from the last node. It can be done in single step because
there is no need to traverse the in between nodes. But in double linked list, we will have to go through in
between nodes LAST

5 10 15 20 25 30

FIRST
Circularly Linked Linear List Cont…
 Disadvantages of Circular List
 It is not easy to reverse the linked list.
 If proper care is not taken, then the problem of infinite loop can occur.
 If we at a node and go back to the previous node, then we can not do it in single step. Instead we have to
complete the entire circle by going through the in between nodes and then we will reach the required node.

 Operations on Circular List


 Insert at First
 Insert at Last
 Insert in Ordered List
 Delete a node
Procedure: CIR_INS_FIRST(DATA,HEAD,LAST)
 This procedure inserts a new node at the first position of Circular linked list.
 DATA is a new element to be inserted.
 HEAD and LAST are a pointer to the first & last elements of a Circular linked linear list,
respectively.
 Typical node contains DATA and NEXT fields.
 TEMP is a temporary pointer variable.
Procedure: CIR_INS_FIRST(DATA,HEAD,LAST)
1. [Creates a new empty node] IF HEAD = NULL
TEMP NODE THEN NEXT (TEMP)  TEMP
2. [Initialize fields of new node and HEAD  LAST  TEMP
its link] ELSE NEXT (TEMP)  HEAD
DATA (TEMP)  DATA NEXT (LAST)  TEMP
HEAD  TEMP
HEAD
Return
TEMP
HEAD LAST
50
LAST

50
TEMP 5 10 1 -5

HEAD
Procedure: CIR_INS_LAST(DATA,HEAD,LAST)
 This procedure inserts a new node at the last position of Circular linked list.
 DATA is a new element to be inserted.
 HEAD and LAST are a pointer to the first & last elements of a Circular linked linear list,
respectively.
 Typical node contains DATA and NEXT fields.
 TEMP is a temporary pointer variable.
Procedure: CIR_INS_LAST( DATA,HEAD,LAST)
1. [Creates a new empty node] IF HEAD = NULL
TEMP NODE THEN NEXT (TEMP)  TEMP
2. [Initialize fields of new node HEAD  LAST  TEMP
and its link] ELSE NEXT (TEMP)  HEAD
DATA (TEMP)  DATA NEXT (LAST)  TEMP
LAST  TEMP
Return
LAST
TEMP
HEAD LAST
50
LAST
50
TEMP
5 10 1 -5

HEAD
Procedure: CIR_INS_ORD(X,FIRST,LAST)
 This function inserts a new node such that circular linked list preserves the ordering of the
terms in increasing order of their DATA field.
 This function returns address of NEWNODE node.
 DATA is a new element to be inserted.
 HEAD is a pointer to the first element of a circular linked linear list.
 Typical node contains DATA and NEXT fields.
 TEMP is a temporary pointer variable.
Function: INSORD(DATA, FIRST)
If(head==null)
{
printf(“circular linked list is empty”);
}
Else
{
int pos; int i = 1;
struct node
{
int data;
struct node *next;
};
strcut node *head,*newnode,*temp;
printf(“enter the position:”);
scanf(“%d”,&pos);
if(pos>count||pos<0)
{
printf(“invalid position”);
}
else
{
temp = head -> next;
while(i<pos)
{
temp = temp -> next;
i++;
}
newnode = (struct node*) malloc(size of (struct node));
printf(“enter data:”);
scanf(“%d”, &newnode->data);
newnode -> next = temp->next;
temp->next=newnode;
}
}
Procedure: CIR_DELETE(DATA,DATA,LAST)
 This algorithm delete a node whose address is given by variable DATA.
 DATA & LAST are pointers to the First & Last elements of a Circular linked list, respectively.
 Typical node contains DATA and NEXT fields.
 TEMP is a temporary pointer variable.
Procedure: CIR_Del_begining(DATA,HEAD,LAST)
If(head==null)
{
printf(“circular linked
list is empty”);
}
Else
{
Temp=head;
Head=head->next;
Tail->next=head;
Temp->next=null;
Free(*temp);
}
Procedure: CIR_Del_last(DATA,HEAD,LAST)
If(head==null)
{
printf(“circular linked list is empty”);
}
Else
{
Temp=head;
While(temp->next!=tail)
{
temp=temp->next;
}
tail->next=null;
lastnode=tail;
temp->next=head;
tail=temp;
free(lastnode);
}
Procedure: CIR_del_specific_pos()
 This procedure delete a node from the specific position of Circular linked list with Head node.
 nextnode is a pointer for the node to be delete.
 HEAD and LAST are a pointer to the first & last elements of a Circular linked linear list,
respectively.
 Typical node contains Data and next fields.
 HEAD is pointer pointing to Head node of Linked List.
 temp and nextnode is a temporary pointer variable.
Procedure: CIR_del_specific_pos()
Struct node *temp, *nextnode;
Int pos, i=1, l;
Temp= head;
Printf(“enter the position”);
Scanf(“%d”,&pos);
l=getlenght();
If(pos<1||pos>l)
{
printf(“invalid position”);
}
Elseif(pos==1)
{
CIR_Del_begining();
}
Else
{
while(i<=pos-1)
{
temp=temp->next;
i++;
}
nextnode=temp->next;
temp->next=nextnode->next;
tail=temp;
nextnode->next=null;
free(nextnode);
}
Doubly Linked Linear List
 In certain Applications, it is very desirable that a list be traversed in either forward or reverse
direction.
 This property implies that each node must contain two link fields instead of usual one.
 The links are used to denote Predecessor and Successor of node.
 The link denoting its predecessor is called Left Link.
 The link denoting its successor is called Right Link.
 A list containing this type of node is called doubly linked list or two way chain.
Doubly Linked Linear List
 Typical node of doubly linked linear list contains INFO, LPTR RPTR Fields
 LPTR is pointer variable pointing to Predecessor of a node
 RPTR is pointer variable pointing to Successor of a node
 Left most node of doubly linked linear list is called L, LPTR of node L is always NULL
 Right most node of doubly linked linear list is called R, RPTR of node R is always NULL

LPTR INFO RPTR

Typical node of
Doubly Linked List L Doubly linked linear list R
Insert node in Doubly Linked List
Insertion in the middle of Doubly Linked Linear List
Before Insertion temp

L R
LPTR(NEW)  LPTR(temp)
NEW RPTR(NEW)  temp
LPTR(temp)  NEW
RPTR(LPTR(NEW))  NEW
After Insertion
temp

L R

NEW
Insert node in Doubly Linked List
Left most insertion in Doubly Linked Linear List
Before Insertion
temp

L R
LPTR(NEW)  NULL
RPTR(NEW)  temp
NEW LPTR(temp)  NEW
L  NEW

temp After Insertion

L
L R

NEW
Procedure: DOU_INS (L,R,M,X)
 This algorithm inserts a new node in doubly linked linear list.
 The insertion is to be performed to the left of a specific node with its address given by the
pointer variable temp.
 Typical node of doubly linked list contains following fields LPTR, RPTR and INFO.
 LPTR is pointer variable pointing to Predecessor of a node.
 RPTR is pointer variable pointing to Successor of a node.
 L & R are pointer variables pointing for Leftmost and Rightmost node of Linked List.
 NEW is the address of New Node.
 data is value to be inserted.
Procedure: DOU_INS (L,R,M,X)
1. [Create New Empty Node] 4. [Is left most insertion ?]
NEW NODE IF temp = L
2. [Copy information field] THEN LPTR(NEW)  NULL
INFO(NEW)  data RPTR(NEW)  temp
3. [Insert into an empty list] LPTR(temp) NEW
IF R = NULL L  NEW
THEN LPTR(NEW)  NULL Return
RPTR(NEW)  NULL 5. [Insert in middle]
L  R  NEW LPTR(NEW)  LPTR(temp)
Return RPTR(NEW)  temp
LPTR(temp)  NEW
RPTR(LPTR(NEW))  NEW
Return
PROCEDURE: DOU _DEL (L, R, OLD)
 This algorithm deletes the node whose address is contained in the variable OLD.
 Typical node of doubly linked list contains following fields LPTR, RPTR and INFO.
 LPTR is pointer variable pointing to Predecessor of a node.
 RPTR is pointer variable pointing to Successor of a node.
 L & R are pointer variables pointing for Leftmost and Rightmost node of Linked List.
Delete from Doubly Linked List

10 L  R  NULL
OLD
L R

OLD OLD OLD

L L R R

L  RPTR(L) RPTR(LTRP(OLD))  RPTR(OLD) R  LPTR(R)


LPTR (L)  NULL LPTR(RTRP(OLD))  LPTR(OLD) RPTR (R)  NULL
PROCEDURE: DOU _DEL (L, R, OLD)
1. [Is underflow ?]
IF R=NULL
THEN write (‘UNDERFLOW’)
Return
2. [Delete node]
IF L = R (single node in list)
THEN L  R  NULL
ELSE IF OLD = L (left most node)
THEN L  RPTR(L)
LPTR (L)  NULL
ELSE IF OLD = R (right most)
THEN R  LPTR (R)
RPTR (R)  NULL
ELSE RPTR(LPTR (OLD))  RPTR (OLD)
LPTR(RPTR (OLD))  LPTR (OLD)
3. [FREE deleted node ?]
FREE(OLD)
Stack representation of linked list
 A pool or list of free nodes, which we refer to as the stack is maintained in conjunction with
linked allocation.
 Whenever a node is to be inserted in a list or deleted from a list it should be done from
beginning only.
 While we are doing the both operation insertion and deletion from the same end it will follow
the procedure of LIFO that’s represent the stack properties.
Representation of polynomial functions using linked list
 A polynomial function is a function that involves only non-negative integer powers or only positive integer exponents of a
variable in an equation like the quadratic equation, cubic equation, etc. For example, 2x+5 is a polynomial that has exponent
equal to 1.
 A polynomial thus may be represented using arrays or linked list. Array representation assumes that the exponent of the given
expression are arranged from ‘0’ to the highest value(degree), which is represented by the subscript of the array beginning with
‘0’. The coefficient of the respective exponent are placed at an appropriate index in the array. Eg:- y = x2+2x-3.
 A polynomial may also represented using a linked list. A structure may be defined such that it contains two parts:-
(i) one is the coefficient
(ii) second is the corresponding exponent.
The node structure can be defined as:-
Struct node
{
Int coefficient;
Int exponent;
Struct polynomial *next;
};
Sparse matrix representation using linked list
 Sparse matrix
A matrix which contains more number of zeros than non-zeros elements is called sparse matrix
instead of displaying zeros.
We only display the actual number with their row and column index.
Col[0] Col[1] Col[2] (This matrix contains 12 values out of those only 5
Row[0] actual values.)
1 0 3
Row[1] 0 0 5
Row[2] 7 0 0
Row[3] 0 9 0
Row Col Value
0 0 1
0 2 3
1 2 5
2 0 7
3 1 9

You might also like