0% found this document useful (0 votes)
94 views24 pages

Linked List: Lab No. 3

The document discusses linked lists and operations on linked lists such as insertion. It defines a linked list as a linear data structure composed of nodes, where each node contains a data field and a reference to the next node. The document provides code to define Node and LinkedList classes, and describes how to insert nodes at the beginning, after a given node, and at the end of the linked list by traversing the list and updating next pointers.

Uploaded by

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

Linked List: Lab No. 3

The document discusses linked lists and operations on linked lists such as insertion. It defines a linked list as a linear data structure composed of nodes, where each node contains a data field and a reference to the next node. The document provides code to define Node and LinkedList classes, and describes how to insert nodes at the beginning, after a given node, and at the end of the linked list by traversing the list and updating next pointers.

Uploaded by

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

CSC-221 Data Structures and Algorithm

Bahria University Karachi Campus

Linked List Lab No. 3

ENGR. AYESHA KHAN


What is a Linked List?

• Linked List is a linear data structure


which consists of a group of nodes
in a sequence. Each node contains
two parts.
• Data− Each node of a linked list can
store a data.
• Address − Each node of a linked list
contains an address to the next
node, called "Next".
• The first node of a Linked List is
referenced by a pointer called Head

CSC-221 Data Structures and Algorithm Bahria University Karachi Campus


Singly Linked List

• Singly Linked List: Singly linked lists contain nodes which have a
data part and an address part, i.e., Next, which points to the next
node in the sequence of nodes. The next pointer of the last node
will point to null.

CSC-221 Data Structures and Algorithm Bahria University Karachi Campus


Creating a Linked List

• The node of a singly linked list contains a data part and a link
part. The link will contain the address of next node and is
initialized to null. So, we will create class definition of node for
singly linked list as follows -

CSC-221 Data Structures and Algorithm Bahria University Karachi Campus


NODE CLASS

• public class Node


• {
• public int data;
• public Node next;
• public Node(int d)
• {
• data = d;
• next = null;
• } // Constructor
• }
CSC-221 Data Structures and Algorithm Bahria University Karachi Campus
Linked List Class

• Now, our node has been


created, so, we will create a
linked list class now. When a
new Linked List is instantiated,
it just has the head, which is
Null.The SinglyLinkedList class
will contain nodes of type
Node class. Hence,
SinglyLinkedList class
definition will look like below.
CSC-221 Data Structures and Algorithm Bahria University Karachi Campus
Linked list class

• public class LinkedList


• {
• public Node head;
• // head of list
• }

CSC-221 Data Structures and Algorithm Bahria University Karachi Campus


 /* Start with the empty list. */
        LinkedList llist = new LinkedList();
 
        llist.head = new Node(1);
        Node second = new Node(2);
        Node third = new Node(3);
 
        llist.head.next = second; // Link first node with the second node
        second.next = third; // Link second node with the third node

CSC-221 Data Structures and Algorithm Bahria University Karachi Campus


• /* Three nodes have been allocated
dynamically.
• We have references to these three blocks as head,
• second and third

• llist.head second third
• | | |
• | | |
• +----+------+ +----+------+ +----+------+
• | 1 | null | | 2 | null | | 3 | null |
• +----+------+ +----+------+ +----+------+ */
CSC-221 Data Structures and Algorithm Bahria University Karachi Campus
• /* Now next of first Node refers to second. So
they
• both are linked.

• llist.head second third


• | | |
• | | |
• +----+------+ +----+------+ +----
+------+
• | 1 | o-------->| 2 | null | | 3 | null |
• +----+------+ +----+------+ +----
+------+ */

CSC-221 Data Structures and Algorithm Bahria University Karachi Campus


• // Link second node with the third node

• /* Now next of the second Node refers to


third. So all three
• nodes are linked.

• llist.head second third


• | | |
• | | |
• +----+------+ +----+------+ +----+------+
• | 1 | o-------->| 2 | o-------->| 3 | null |
• +----+------+ +----+------+ +----+------+
*/

CSC-221 Data Structures and Algorithm Bahria University Karachi Campus


traversing

• public void printList() • Call in main:


• {
• Node n = head;
• Llist.printList();
• while (n != null)
• {
• Console.Write(n.data + " ");
• n = n.next;
• }
• }

CSC-221 Data Structures and Algorithm Bahria University Karachi Campus


Various operations on Linked list-insertion

, methods to insert a new node


in linked list are discussed. A
node can be added in three
ways 
1) At the front of the linked list 
2) After a given node. 
3) At the end of the linked list.

CSC-221 Data Structures and Algorithm Bahria University Karachi Campus


INSERTION-At the front of the linked list

• Add a node at the front: (4 steps


process) 
The new node is always added before the
head of the given Linked List. And newly
added node becomes the new head of
the Linked List. For example, if the given
Linked List is 10->15->20->25 and we add
an item 5 at the front, then the Linked
List becomes 5->10->15->20->25. Let us
call the function that adds at the front of
the list is push(). The push() must
receive a pointer to the head pointer,
because push must change the head
pointer to point to the new node
CSC-221 Data Structures and Algorithm Bahria University Karachi Campus
INSERTION-At the front of the linked list

• public void push(int new_data)


• {
• /* 1 & 2: Allocate the Node &
• Put in the data*/
• Node new_node = new Node(new_data);

• /* 3. Make next of new Node as head */


• new_node.next = head;

• /* 4. Move the head to point to new Node


*/
• head = new_node;
• }
CSC-221 Data Structures and Algorithm Bahria University Karachi Campus
INSERTION-Add a node after a given node

• We are given a pointer to a


node, and the new node is
inserted after the given node.

CSC-221 Data Structures and Algorithm Bahria University Karachi Campus


INSERTION-Add a node after a given node

• public void insertAfter(Node prev_node, int • /* 2 & 3: Allocate the Node &
new_data) • Put in the data*/
• { • Node new_node = new Node(new_data);
• /* 1. Check if the given Node is null */
• if (prev_node == null) • /* 4. Make next of new Node as
• { • next of prev_node */
• Console.WriteLine("The given • new_node.next = prev_node.next;
previous" +
• " node cannot be null"); • /* 5. make next of prev_node as
• return; new_node */
• } • prev_node.next = new_node;
• }

CSC-221 Data Structures and Algorithm Bahria University Karachi Campus


INSERTION-Add a node at the end:

• The new node is always added after •


the last node of the given Linked
List. For example if the given Linked
List is 5->10->15->20->25 and we
add an item 30 at the end, then the
Linked List becomes 5->10->15->20-
>25->30. 
Since a Linked List is typically
represented by the head of it, we
have to traverse the list till the end
and then change the next to last
node to a new node.
CSC-221 Data Structures and Algorithm Bahria University Karachi Campus
INSERTION-Add a node at the end:



public void append(int new_data)
•• /* 4. This new node is going to be the last node,
{ • so make next of it as null */
• /* 1. Allocate the Node & • new_node.next = null;
• 2. Put in the data
• 3. Set next as null */ • /* 5. Else traverse till the last node */
• Node new_node = new Node(new_data);
• Node last = head;
• while (last.next != null)
• /* 4. If the Linked List is empty,
• last = last.next;
• then make the new node as head */
• if (head == null)
• { • /* 6. Change the next of last node */
• head = new Node(new_data); • last.next = new_node;
• return; • return;
• } • }
CSC-221 Data Structures and Algorithm Bahria University Karachi Campus
FINAL CALLING INSERTION METHODS IN MAIN()

/* Start with the empty list */


        GFG llist = new GFG();
   // Insert 4 at the end. So linked list becomes
        // Insert 6. So linked list becomes 6->NUllist         // 1->7->6->4->NUllist
        llist.append(6);         llist.append(4);
   
        // Insert 7 at the beginning.         // Insert 8, after 7. So linked list becomes
        // So linked list becomes 7->6->NUllist         // 1->7->8->6->4->NUllist
        llist.push(7);         llist.insertAfter(llist.head.next, 8);
   
        // Insert 1 at the beginning.         Console.Write("Created Linked list is: ");
        // So linked list becomes 1->7->6->NUllist         llist.printList();
        llist.push(1);
 

CSC-221 Data Structures and Algorithm Bahria University Karachi Campus


DELETION

• Iterative Method:
To delete a node from the linked list, we
need to do the following steps. 
1) Find the previous node of the node to be
deleted. 
2) Change the next of the previous node. 
3) Free memory for the node to be deleted.
 

CSC-221 Data Structures and Algorithm Bahria University Karachi Campus


DELETION

 // Search for the key to be


  // deleted, keep track of the
  // previous node as we need
void deleteNode(int key)   // to change temp.next
{   while (temp != null &&
  // Store head node          temp.data != key)
  Node temp = head, prev = null;   {
      prev = temp;
    temp = temp.next;
  // If head node itself holds
  }   
  // the key to be deleted
  if (temp != null &&  
      temp.data == key)   // If key was not present
  {   // in linked list
    // Changed head   if (temp == null)
    head = temp.next;     return;
    return;
  }  
  // Unlink the node from linked list
  prev.next = temp.next;
}

CSC-221 Data Structures and Algorithm Bahria University Karachi Campus


CALLING DELETION METHOD:

 llist.deleteNode(1);

CSC-221 Data Structures and Algorithm Bahria University Karachi Campus


Lab Tasks

1. Write a program to create a linked list and pertform


*traversing
* Insertion
*deletion

You might also like