0% found this document useful (0 votes)
18 views30 pages

Linear Linked Lists

Uploaded by

frustatedmonk2
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)
18 views30 pages

Linear Linked Lists

Uploaded by

frustatedmonk2
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/ 30

1.

Concepts of linked list


• A linked list is a linear collection of data elements
called nodes in which linear representation is given by
links from one node to the next node.

• Similar to array, it is a linear collection of data


elements of the same type.

• Different from array, data elements of linked list are


generally not lined in consecutive memory space;
instead they are dispersed in various locations
Concepts of linked list
• Linked list is a data structure which in turn can be
used to implement other data structures. Thus, it acts
as building block to implement data structures like
stacks, queues and their variations.

• A linked list can be perceived as a train or a sequence


of nodes in which each node contains one or more
data fields and a pointer to the next node.
Element of linked list
• Linked list element (node) is user defined structure
data type, typically contains two parts
▪ data variables
▪ pointers to next elements, hold the addresses of
next elements

Example:

struct node {
int data; // data
struct node *next; // pointer to next element
};
How to create nodes
struct node *np = (struct node*) malloc(sizeof(struct node));

At run time, OS allocates consecutive sizeof(struct node)


bytes in the heap region,
return the address of the address of the first memory cell,

store the address to struct node type pointer np.


Need (struct node*) to cast the return address to struct
node pointer value.

Need use free(np) to release when deleting the node!!!


Otherwise cause memory leaking
Types of Lists
• Depending on the way in which the links are used to maintain
adjacency, several different types of linked lists are possible.

• Linear singly-linked list (or simply linear list)


• One we have discussed so far.
head

A B C

Spring 2012 Programming and Data Structure 6


• Circular linked list
• The pointer from the last element in the list points back to the first element.

head

A B C

Spring 2012 Programming and Data Structure 7


• 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

Spring 2012 Programming and Data Structure 8


Basic Operations on a List
• Creating a list
• Traversing the list
• Inserting an item in the list
• Deleting an item from the list
• Concatenating two lists into one

Spring 2012 Programming and Data Structure 9


Traversing Linked Lists
• We can traverse the entire linked list using a
single pointer variable called START.

• The START node contains the address of the first


node; the next part of the first node in turn
stores the address of its succeeding node.

• Using this technique the individual nodes of the


list will form a chain of nodes.

• If START = NULL, this means that the linked list is


empty and contains no nodes.
Traversal Singly Linked Lists
ALGORITHM FOR TRAVERSING A LINKED LIST

Step 1:
[INITIALIZE] SET PTR = START
Step 2:
Repeat Steps 3 and 4 while PTR != NULL
Step 3:
Apply Process to PTR->DATA
Step 4:
SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: EXIT

void display(struct node *ptr) {


while(ptr != NULL) {
printf("%d ", ptr->data); // process
ptr = ptr->next;
}
}
Call as display(START);
Searching for Val 4 in Linked List

1 7 3 4 2 6 5 X

PTR

1 7 3 4 2 6 5 X

PTR

1 7 3 4 2 6 5 X

PTR

1 7 3 4 2 6 5 X

PTR
Searching a Linked List
ALGORITHM TO SEARCH A LINKED LIST
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Step 3 while PTR != NULL
Step 3: IF VAL = PTR->DATA
SET POS = PTR
Go To Step 5
ELSE
SET PTR = PTR->NEXT
[END OF IF]
[END OF LOOP]
Step 4: SET POS = NULL // not found
Step 5: EXIT // found, output POS

struct node* search(struct node *ptr, int num) {


while((ptr != NULL) && (ptr->data != num)) {
ptr = ptr->next;
}
return ptr;
}
// call example, search(START, 4)
Inserting a Node at the Beginning
Inserting a Node at the End
Inserting a Node after an Element
Deleting the First Node
Deleting the Last Node
START

1 7 3 4 2 6 5 X

LOC
Deleting the Last Node
START

1 7 3 4 2 6 5 X

LOC
Deleting the Last Node
START

1 7 3 4 2 6 5 X

LOC
Deleting the Last Node
START

1 7 3 4 2 6 5 X

LOC
Deleting the Last Node
START

1 7 3 4 2 6 5 X

LOC
Deleting the Last Node
START

1 7 3 4 2 6 5 X

LOC
Deleting the Node After a Given Node

START

1 7 3 4 2 6 5 X

START

1 7 3 4 2 6 5 X

You might also like