You are on page 1of 27

COSC-2201

SinglyLinked Lists
Data Structures & Algorithms
Lecture by Ms. Hira Kanwal
Department of CS/IT
List Overview

• Operations on Linked Lists


• Traversing
• Insertion
• Deletion
• Display
Linked Lists ADT

• The following operations make linked lists an ADT:

• Main Linked Lists Operations – Insert: inserts an element into the list
• Delete: removes and returns the specified position element from the
list
Linked List Traversal

• The idea here is to visit through the list from beginning to end. The
algorithm for traversing a list

• Start with the head of the list. Access the content of the head node .
• Then go to the next node(if exists) and access the node information
• Continue until no more nodes (that is, you have reached the null
node)
Implementation of Node

• struct node
• {
• int info;
• struct node *next;
• };
Info/Data Next

Node
Algorithm for traversal

1. Set ptr= start;


2. Repeat step 3 & 4 until (ptr!= Null)
3. Print= info (ptr)
4. Ptr= link (ptr)
5. exit
Implementation

• struct node *temp,*ptr;


• While (temp!=NULL)
• {
• Print(temp-> data);
• Temp=temp->next;
• }
Linked List node Insertion

• There can be three cases that will occur when we are inserting a
node in a linked list.

• Insertion at the beginning


• Insertion at the end. (Append)
• Insertion after a given node
Insertion at the beginning

• Since there is no need to find the end of the list.


• If the list is empty, we make the new node as the head of the list.
• Otherwise, we we have to connect the new node to the current
head of the list and make the new node, the head of the list.
Insertion at the beginning

• struct node *temp;


• temp=(struct node *)malloc(sizeof(struct node));
• printf("\n Enter the data value for the node: " );
• scanf("%d",&temp->info);
• temp->next =NULL;
• if(start==NULL)
• {
• start=temp;
• }
• else
• {
• temp->next=start;
Insertion at end

We will traverse the list until we find the last node.

Then we insert the new node to the end of the list.

Note that we have to consider special cases such as list being empty.
In case of a list being empty, we will return the updated head of the
linked list because in this case, the inserted node is the first as well as
the last node of the linked list.
Insertion at end

• struct node *temp,*ptr;


• temp=(struct node *)malloc(sizeof(struct node));
• printf("\n Enter the data value for the node: " );
• scanf("%d",&temp->info );
• temp->next =NULL;
• if(start==NULL)
• {
• start=temp;
• }
Insertion at end

• else
•{
• ptr=start;
• while(ptr->next !=NULL)
•{
• ptr=ptr->next ;
•}
• ptr->next =temp;
•}
Insertion after a given node

• We are given the reference to a node, and the new node is inserted
after the given node.
Insertion after a given node

• struct node *ptr,*temp;


• int i,pos;
• temp=(struct node *)malloc(sizeof(struct node));
• printf("\n Enter the position for the new node to be inserted: ");
• scanf("%d",&pos);
• printf("\n Enter the data value of the node: ");
• scanf("%d",&temp->info) ;
• temp->next=NULL;
• if(pos==0)
•{
• temp->next=start;
• start=temp;
•}
• else
•{
• for(i=0,ptr=start;i<pos-1;i++) { ptr=ptr->next;
• if(ptr==NULL)
• {
• printf("\n Position not found:[Handle with care]\n");
• return;
• }
• }
• temp->next =ptr->next ;
• ptr->next=temp;
• }
Linked List node Deletion

• There can be three cases that will occur when we are deleting a node
in a linked list.

• Deletion at the beginning


• Deletion at the end. (Append)
• Deletion after a given node
Linked List node Deletion

• To delete a node from a linked list, we need to do these steps

• Find the previous node of the node to be deleted.


• Change the next pointer of the previous node
• Free the memory of the deleted node.

• In the deletion, there is a special case in which the first node is


deleted. In this, we need to update the head of the linked list.
Deletion at the beginning

• struct node *ptr;


• ptr=start;
• start=start->next ;
• printf("\n The deleted element is :%d ",ptr->info);
• }
Deletion at the end.

• struct node *temp,*ptr;


• if(start->next ==NULL)
• {
• ptr=start;
• start=NULL;
• printf("\n The deleted element is:%d ",ptr->info);
• }
• else
• {
• ptr=start;
• while(ptr->next!=NULL)
•{
• temp=ptr;
• ptr=ptr->next;
•}
• temp->next=NULL;
• printf("\n The deleted element is:%d ",ptr->info);
•}
•}
Deletion after a given node

• int i,pos;
• struct node *temp,*ptr;
• printf("\n Enter the position of the node to be deleted: ");
• scanf("%d",&pos);
• if(pos==0)
• {
• ptr=start;
• start=start->next ;
• printf("\n The deleted element is:%d ",ptr->info );
• }
• else
• {
• ptr=start;
• for(i=0;i<pos;i++) { temp=ptr; ptr=ptr->next ;
• if(ptr==NULL)
•{
• printf("\n Position not Found:\n");
• return;
•}
•}
• temp->next =ptr->next ;
• printf("\n The deleted element is:%d ",ptr->info );
• free(ptr);
•}
•}
•}

You might also like