You are on page 1of 30

Department of Computer Science and Engineering (CSE)

LEARNING OUTCOMES
• Students will learn about the linked list data structures, their
advantages over arrays and disadvantages.
• Students can able to do the distinctions between arrays and link
lists.
• Students will learn about Linked lists creations , all types of
insertions and deletions in linked lists.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Linked List
• A Linked List is a linear data structure that consists of two
parts: one is the data part and the other is the address part.
• A list implemented by each item having a link to the next item.
• Linked Lists are used to create trees and graphs.
• Applications Areas
Music and Media Players
Symbol Table Implementation
Task Scheduling in Operating Systems
Browser History
GPS Navigation Systems

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

REPRESENTATIONS

[1]

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

ARRAYS VS LINKED LIST

Arrays
• have a pre-determined fixed size
• easy access to any element a[i] in constant time
• no space overhead
• Size = n x sizeof(element)

Linked lists
• no fixed size, grow one element at a time.
• space overhead
• each element must store an additional reference
• Size = n x sizeof (element) + n x sizeof(reference)
• no easy access to i-th element wrt the head of the list
• need to hop through all previous elements
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Types of Linked Lists

There are 3 different


implementations of Linked
List available, they are:
1. Singly Linked List
2. Doubly Linked List
3. Circular Linked List

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

SINGLY LINK LIST

[1]

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Circular Linked List


• The last element stores the address of the starting element

Application of Circular Linked List


• circular linked list is used in our Personal Computers, where multiple
applications are running. All the running applications are kept in a circular
linked list and the OS gives a fixed time slot to all for running. The Operating
System keeps on iterating over the linked list until all the applications are
completed.
• Multiplayer games. All the Players are kept in a Circular Linked List and the
pointer keeps on moving forward as a player's chance ends.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

OPERATIONS ON LINK LIST


 TRAVERSAL
 SEARCHING (IN UNSORTED LINK LIST AND
SORTED LINK LIST)
 DELETION
 INSERTION
 AT THE BEGINNING
 AFTER A GIVEN NODE
 IN THE SORTED LIST

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)
Link List Creation Simplest Program

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

TRAVERSAL OF LINKED LIST


1. Set PTR=Start
2. Repeat steps 3 & 4 while PTR!=null
3. Write info[PTR]
4. Set PTR=Link[PTR] //Link points to the Next Node
5. Exit

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)
TRAVERSAL OF LINKED LIST

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

SEARCHING IN UNSORTED LINKED LIST


1. Set PTR=START
2. Repeat Step 3 while PTR!=NULL
3. If ITEM=info[PTR],then
4. Set LOC=PTR and Exit
5. Else
6. Set PTR=LINK[PTR]
7. SET LOC=NULL //If Search is unsuccessful
8. Exit

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)
SEARCHING IN UNSORTED
int main() {
LINKED LIST
// Create nodes
#include <stdio.h>
struct Node node1, node2, node3;
#include <stdlib.h> struct Node *node1 = (struct Node *)malloc(sizeof(struct Node));
// Define a linked list node struct Node *node2 = (struct Node *)malloc(sizeof(struct Node));
struct Node *node3 = (struct Node *)malloc(sizeof(struct Node));
struct Node {
int data; node1->data = 10;
struct Node *next; node2 ->data = 20;
}; node3 ->data = 30;
int search(struct Node *head, int target) // Link nodes
node1->next = &node2;
{
node2->next = &node3;
struct Node *ptr = head; node3->next = NULL;
while (ptr != NULL) // Search for an element
{ int target = 20;
if (ptr->data == target) { if (search(&node1, target)) {
return 1; // Element found printf("Element %d found in the linked list.\n",
} target);
ptr = ptr->next; } else {
printf("Element %d not found in the linked list.\
}
n", target);
}
return 0; // Element not found return 0;
} }
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

SEARCHING IN SORTED LINK LIST


SEARCH(List, Link, Start, Item, LOC)
1. Set PTR=START
2. Repeat Step 3 while PTR!=NULL
3. If ITEM < info[PTR],then
4. Set PTR = LINK[PTR] //PTR now points to the next
node
Else If ITEM = info[PTR],then
Set LOC = PTR and Exit // Search is Successful
Else
Set LOC = NULL and Exit // ITEM Now Exceeds
info[PTR]
5. Set LOC=NULL and Exit
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

INSERTION IN LINK LIST-AT BEGINNING


INSFIRST(Info , Link, Start, Avail, Item)
1. If avail = NULL Write Overflow and Exit
2. Set New = avail & avail = Link[avail] // Remove First
Node From avail list
3. Set info[New] = Item // Copies New Data into New
Node
4. Set Link[New] = Start
5. Exit

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Insertion in a Singly Linked List

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)
Insertion in a Header Linked List (With
Header Node):
struct Node {
Insertion in a Singly Linked List (Without Header Node):
int data;
struct Node {

int data; struct Node* next;


struct Node* next; };
};

// Insert new node at the beginning


// Insert new node at the beginning
void insertAtBeginning(struct Node* header, int newData) {
struct Node* insertAtBeginning(struct Node* head, int newData) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = newData; newNode->data = newData;
newNode->next = head;
newNode->next = header->next;
head = newNode;
header->next = newNode;
return head;
}
}

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

INSERTION IN LINK LIST-IN SORTED LIST


FINDA(Info, Link, Start, Item, LOC)
1. If Start = NULL then Set LOC = NULL and Return
2. If Item < Info[Start] then Set LOC = NULL and Return
3. Set Save = Start and PTR = Link[Start] //Initializes
Pointer
4. Repeat Steps 5 and 6 while PTR != NULL
5. If Item < info[PTR],then
Set LOC = Save and Return
6. Set Save = PTR & PTR = Link[PTR] //Updates Pointer
7. Set LOC = Save
8. Exit
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

CONTINUED
INSERT(Info, Link, Start, Avail, Item)
1. Call FINDA(Info, Link, Start, Avail, Item)
2. Call INSLOC(Info, Link, Start, Avail, Item,LOC)
3. Exit

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

INSERTION IN LINK LIST-AFTER GIVEN LOC


INSLOC(Info, Link, Start, Avail, Item)
1. If avail = NULL Write Overflow and Exit
2. Set New = avail & avail = Link[avail] //Remove First
Node from Avail List
3. Set Info[NEW] = Item // Copies new data into
New Node
4. If LOC = NULL, then // Insert as First Node
Set Link[NEW] = Start and Start = NEW
Else //Insert after Node with Location LOC
Set Link[NEW] = Link[LOC] and Link[LOC] = NEW
5. Exit
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
INSERTION IN LINK LIST-AFTER GIVEN LOC

// Insert at nth position of LinkedList

node *insertpos(node *head, int position, int data)


{
node *temp = new node;

if(position = = 1)
{
temp -> next = head;
return temp;
}

node *curr = head;

for(int i = 1;i <= position-2 && curr != NULL; i++)


{
curr = curr -> next;
}

if(curr == NULL)
{
return head;
}

//Insertion and returning head


temp -> next = curr -> next;
curr -> next = temp;
return head;
}
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Deletion
• DEL(Info, Link, Start, Avail, LOC, LOCP)
1. If LOCP = NULL, then
Set Start = Link[Start] //Deletes First Node
Else
Set Link[LOCP] = Link[LOC] //Deletes Node N
2. Set Link[LOC] = Avail // Return Deleted
Node to the Avail List
and
Avail = LOC
3. Exit

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)
Delete First Node of a Linked List

• Head always points to


the first node.
• Head will move to
where the first node is
currently pointing to.
• First node can either
point to the second node void deleteFirstNode()

or to NULL in case the {


struct node *temp;

first node is the only if(head= =NULL)


{
node in the linked list. }
printf("List is already empty.");

else
• After moving the head, {
temp = head;
we can free up the head = head->next;

printf("\nData deleted = %d\n", temp->data);


memory of the first /* Clears the memory occupied by first node*/
node. free(temp);

printf("SUCCESSFULLY DELETED FIRST NODE FROM LIST\n");


University Institute
}
}
of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
Deletion at the end of the Singly Linked List
To delete the last node of a linked list, find the second last node and make the next pointer of that
node null
void delete_Lastnode()
{
struct node *temp, *ptr1;
if(head == NULL)
{
printf("\n List is empty");
}
else
{
temp = head;
while(temp->next != NULL)
{
ptr1 = temp;
temp = temp ->next;
}
ptr1->next = NULL;
free(temp);
printf("\n Deleted Node from the last ...");
}
}

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)
DELETION A NODE WITH GIVEN ITEM OF INFORMATION
FINDB(Info, Link, Start, Item, LOC, LOCP)
1. If Start = NULL then Set LOC = NULL & LOCP = NULL & Return
2. If Info[Start] = Item, then //Item in First Node?
Set LOC = Start & LOCP = NULL & Return
3. Set Save = Start & PTR = Link[Start] //Initializes Pointer
4. Repeat Steps 5 & 6 while PTR !=NULL
5. If Info[PTR] = Item,then
Set LOC = PTR & LOCP = Save & Return
6. Set Save = PTR & PTR = Link[PTR] //Updates Pointer
7. Set LOC = NULL //Search Unsuccessful
8. Exit
DELETE(Info, Link, Avail, Start, Item)
9. CALL FINDB(Info, Link, Start, Item, LOC, LOCP)
10. If LOC = NULL Write Item not in List and Exit
11. If = LOCP = NULL then
12. Set Start = Link[Start]
Else
Link[LOCP] = Link[LOC]
4. Set Link[LOC] = Avail & Avail = LOC
5. Exit

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)
void delete (int pos)
Delete node at particular {
struct node* temp = head; // Creating a temporary
position // variable pointing to head
int i;
if (pos == 0) {
printf("\nElement deleted is : %d\n", temp->data);
head = head->next; // Advancing the head pointer
temp->next = NULL;
free(temp); // Node is deleted
}
else {
for (i = 0; i < pos - 1; i++) {
temp = temp->next;
}
// Now temp pointer points to the previous node of
// the node to be deleted
struct node* del
= temp->next; // del pointer points to the node
// to be deleted
temp->next = temp->next->next;
printf("\nElement deleted is : %d\n", del->data);
del->next = NULL;
free(del); // Node is deleted
}
printf("\nUpdated Linked List is : \n");
display_List();
return;
}

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

REFERENCES
• https://www.cs.cmu.edu/~adamchik/15-121/lectures/Linked
%20Lists/linked%20lists.html
• https://www.geeksforgeeks.org/data-structures/linked-list/
• https://www.tutorialspoint.com/data_structures_algorithms/lin
ked_list_algorithms.htm

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Books Recommended
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series,
Tata McGraw Hill.
• Gilberg/Forouzan,” Data Structure with C ,Cengage Learning.
• Augenstein,Moshe J , Tanenbaum, Aaron M, “Data Structures
using C and C++”, Prentice Hall of India.
• Aho, Alfred V., Ullman, Jeffrey D., Hopcroft ,John E. “Data
Structures and Algorithms”, Addison Wesley.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Thanks

University Institute of Engineering (UIE)

You might also like