0% found this document useful (0 votes)
16 views8 pages

Dsa L6

The document provides an overview of linked lists, including their definition, comparison with arrays, and applications. It details the implementation of singly linked lists, including adding and removing nodes at both the head and tail, as well as deleting the last node. Additionally, it discusses the use of linked lists to implement stacks and queues, and mentions an upcoming lab related to the topic.

Uploaded by

Alkit Sharma
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)
16 views8 pages

Dsa L6

The document provides an overview of linked lists, including their definition, comparison with arrays, and applications. It details the implementation of singly linked lists, including adding and removing nodes at both the head and tail, as well as deleting the last node. Additionally, it discusses the use of linked lists to implement stacks and queues, and mentions an upcoming lab related to the topic.

Uploaded by

Alkit Sharma
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/ 8

CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD

(2 ND SEMESTER 2024-25) Sr. Professor of Computer Sc.


BITS-Pilani Hyderabad Campus
LINKED LISTS hota[AT]hyderabad.bits-pilani.ac.in
LINKED LISTS
• What are these?

• Arrays Vs Linked lists

• What are some of the applications


of Linked lists?
IMPLEMENTING A SINGLY LINKED LIST
Step 1: Define a class for the Node Step 3: Define a set of member functions for the
class StringNode { Linked list class defined in Step 2
private: string elem;
StringNode* next;
StringLinkedList::StringLinkedList() : head(???){ }
friend class StringLinkedList;
StringLinkedList::~StringLinkedList() {
};
Step 2: Define a class for the Linked list while(!empty())
class StringLinkedList { ???;
public: StringLinkedList(); }
~StringLinkedList(); bool StringLinkedList::empty() const { //Is list empty?
bool empty() const; return head == NULL;
const string& front() const; }
void addFront(const string& e);
void removeFront(); const string& StringLinkedList::front() const {
private: StringNode* head; return ???;
}; }
INSERTING & REMOVING AT THE HEAD OF LINKED LIST
1. Create a new node
2. Store data into this node
3. Have new node point to old head X Goa
Dubai
4. Update head to point to new node
void StringLinkedList::addFront(const string& e)
{ Pilani
StringNode* v = new StringNode; Inserting at the head
v->elem = e;
v->next = head; void StringLinkedList::removeFront()
head = v; {
} StringNode* old = head;
head = old->next;
1. Save old head
delete old; 2. Advance head to the next
} node
Deleting at the head
3. Delete the old head node
INSERTING AT THE TAIL & INSIDE A LINKED LIST
1. Allocate a new node
2. Insert new element (Hyd)
3. Have new node point to null (v->next Pilani Dubai Goa 
= NULL)
4. Have old last node point to new node 
(last_node -> next = v)

Head Hyd 
Pilani Dubai Hyd 

void insertAfter(Node* prev_node, int new_data)


Node* new_node = new Node();
new_node->data = new_data;
Head Goa new_node->next = prev_node->next;
 prev_node->next = new_node;
DELETING THE LAST NODE
Algorithm:

1. If (headNode == null) //how many nodes in list?


then what should you do?
2. If (headNode.next == null) //how many nodes in list?
then what should you do?
3. While secondLast.next.next != null //traverse till secondLast
secondLast = secondLast.nextNode
4. Delete last node and set the pointer of secondLast to null.
STACK & QUEUE AS SINGLY LINKED LISTS
rear
nodes nodes
? front
 
elements elements

Stack: We can implement stack as a Queue: We can implement a queue as a linked


linked list. How will you implement? list. Front element is stored as first element of the
linked list, and rear element is stored as the last
Implementation in later chapters… element.
SWAPPING TWO NODES IN A LINKED LIST

prev1 prev2
X ?

X X

Lab 4 next week (week no:4)

You might also like