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

Presentation On DataStructuresLinkedList

Uploaded by

Sneha Malik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Presentation On DataStructuresLinkedList

Uploaded by

Sneha Malik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Elementary Data Structures

Linked Lists
List Implementation using Linked Lists
Linked list
Linear collection of self-referential class objects, called
nodes
Connected by pointer links
Accessed via a pointer to the first node of the list
Link pointer in the last node is set to null to mark the list’s
end
Use a linked list instead of an array when
You have an unpredictable number of data elements
You want to insert and delete quickly.
Self-Referential Structures
Self-referential structures
Structure that contains a pointer to a structure of the same type
Can be linked together to form useful data structures such as lists, queues,
stacks and trees
Terminated with a NULL pointer (0)
Diagram of two self-referential structure
3
objects linked2 together
100 … 500

Data member and pointer NULL pointer (points to nothing)

struct node {
int data;
struct node *nextPtr;
}
nextPtr
Points to an object of type node
Referred to as a link
Linked Lists
Types of linked lists:
Singly linked list
Begins with a pointer to the first node
Terminates with a null pointer
Only traversed in one direction
Circular, singly linked
Pointer in the last node points
back to the first node
Doubly linked list
Two “start pointers” – first element and last element
Each node has a forward pointer and a backward pointer
Allows traversals both forwards and backwards
Circular, doubly linked list
Forward pointer of the last node points to the first node and
backward pointer of the first node points to the last node
Linked Representation of Data
In a linked representation, data
is not stored in a contiguous 358 797
manner. Instead, data is 561 501 724
stored at random locations 345
and the current data location 555 490 701
provides the information 513
regarding the location of the
next data. 358 797
561 501
345 724

Adding item 498 on to the linked list 555 490 701 513
Q: What is the cost of adding an item?
Q: how about adding 300 and 800 498
onto the linked list 358 797
561 501
345 724
Deleting item 358 from the linked list
555 490 701
Q: What is the cost of deleting an item? 513
Q: What is the cost of searching for an 498
item?
Linked List
How do we represent a linked list in the memory
Each location has two fields: Data Field and Pointer (Link)
Field.
Linked List Implementation

START Node
Element
Data Pointer (Link) Null Pointer
Field Field
struct node {
int data;
struct node *link; 1 300 5

};
2 500 0 NULL
struct node my_node; 3
3 100 4
Example:
4 200 1

5 400 2
Conventions of Linked List
There are several conventions for the link to indicate
the end of the list.
1. a null link that points to no node (0 or NULL)
2. a dummy node that contains no item
3. a reference back to the first node, making it a
circular list.
Singly Linked List

bat  cat  sat  vat NULL

*Figure 4.1: Usual way to draw a linked list (p.139)


Example: create a two-node list

ptr

10  20 NULL

typedef struct list_node *list_pointer;


typedef struct list_node {
int data;
list_pointer link;
};
list_pointer ptr =NULL
Two Node Linked List
list_pointer create2( )
{
/* create a linked list with two nodes */
list_pointer first, second;
first = (list_pointer) malloc(sizeof(list_node));
second = ( list_pointer) malloc(sizeof(list_node));
second -> link = NULL;
second -> data = 20;
first -> data = 10;
first ->link = second; ptr
return first;
}
10  20 NULL
Linked List Manipulation Algorithms
List Traversal
Let START be a pointer to a linked list in memory. Write an
algorithm to print the contents of each node of the list
Algorithm
1. set PTR = START
2. repeat step 3 and 4 while PTR NULL
3. print PTR->DATA
4. set PTR = PTR -> LINK
5. stop

10 20 30 40

10
1000 20
2000 30
3000 40
4000 50
START Data Link

PTR = LINK[PTR]
PTR
Search for an Item
Search for an ITEM
Let START be a pointer to a linked list in memory. Write an
algorithm that finds the location LOC of the node where ITEM
first appears in the list, or sets LOC=NULL if search is
unsuccessful.
Algorithm
1. set PTR = START
2. repeat step 3 while PTR NULL
3. if ITEM == PTR -> DATA, then
4. set LOC = PTR, and Exit
5. else
6. set PTR = PTR -> LINK
7. set LOC = NULL /*search unsuccessful */
8. Stop
1000 2000 3000 4000
START

PTR = LINK[PTR]
PTR
Insert an Item
Insertion into a Listed List
Let START be a pointer to a linked list in memory with successive
nodes A and B. Write an algorithm to insert node N between nodes
A and B.
Algorithm
1. Set PTR = START
2. Repeat step 3 while PTR NULL
3. If PTR == A, then
4. Set N->LINK = PTR -> LINK (or = B)
5. Set PTR->LINK = N
START Node A Node B
6. exit
1000 2000 3000 4000 5000
7. else
8. Set PTR=PTR->LINK
9. If PTR == NULL insertion unsuccessful
START Node A Node B
10. Stop
1000 2000 3000 4000 5000

3500
Node N
PTR
3 cases: first node, last node, in-between node. (ex: if ITEM = 500? if ITEM = 6000?)
Delete an Item
Deletion from a Linked List
Let START be a pointer to a linked list in memory that contains integer data.
Write an algorithm to delete note which contains ITEM.
Algorithm
1. Set PTR=START and TEMP = START
2. Repeat step 3 while PTR NULL
3. If PTR->DATA == ITEM, then
4. Set TEMP->LINK = PTR -> LINK, exit
5. else
6. TEMP = PTR
7. PTR = PTR -> LINK
8. Stop START Node A Node N Node B

1000 2000 3000 3500 4000 5000

START Node A Node N Node B

1000 2000 3000 3500 4000 5000

…..
3500

ITEM TEMP PTR

3 cases: first node, last node, in-between node. (ex: if ITEM = 1000? if ITEM = 5000?)

You might also like