0% found this document useful (0 votes)
31 views14 pages

wk2 Lec1

1) Linked lists are a dynamic data structure that uses nodes containing values and pointers to link the nodes together in a sequence. 2) Common operations on linked lists like traversal, insertion, and deletion at the head have a time complexity of O(1) while operations at the end have O(n) complexity. 3) Linked lists have applications in implementing operating system processes, web browser back buttons, computer network packet routing, and memory management.

Uploaded by

Tejas Asija
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)
31 views14 pages

wk2 Lec1

1) Linked lists are a dynamic data structure that uses nodes containing values and pointers to link the nodes together in a sequence. 2) Common operations on linked lists like traversal, insertion, and deletion at the head have a time complexity of O(1) while operations at the end have O(n) complexity. 3) Linked lists have applications in implementing operating system processes, web browser back buttons, computer network packet routing, and memory management.

Uploaded by

Tejas Asija
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/ 14

Data Structures & Algorithms

Week 2 — Lists, Elements of Complexity

Subodh Sharma, Rahul Garg


{svs,rahulgarg}@iitd.ac.in
1
Recap — Week 1
• Introduction to C++
• Variables and Basic Types
• Functions and Selection operator
• Loops, Arrays
• Scopes — Global, Local and Block
• C++ Pointers
• Classes, Access Speci ers (Public, Private, Protected), Inheritance
• Assignment 1 is OUT! Start early and earnestly! Ask questions on Piazza!

2
fi
Clarifications - Process Memory
#include <iostream>
#include <stdlib.h> STACK
int globalVar; // BSS Segment - Uninitialized Global Variable

int globalVarInit = 10; // Data Segment - Initialized Global Variable


HEAP
int main() {

int localVar = 10; // Stack - Local Variable Globals/Static


int *ptr = new int; // Heap - Dynamically Allocated Variable
Code
std::cout << "Address of Text Segment: " << (void*)&main << std::endl;
std::cout << "Address of Data Segment: " << (void*)&globalVarInit << std::endl;
std::cout << "Address of BSS Segment: " << (void*)&globalVar << std::endl;
std::cout << "Address of Heap: " << ptr << std::endl;
std::cout << "Address of Stack: " << (void*)&localVar << std::endl;

delete ptr; // Remember to free the dynamically allocated memory


return 0;
}

3
Clarifications - Inheritance Keywords
• Public Inheritance:
• Public members of Base —> Public
members of Derived

• Protected members of Base —>


Protected members of Derived

• Protected Inheritance:
• Public and Protected members of
Base —> Protected members of
Derived

• Private Inheritance:
• All derived members become private
4
int cols);
Clarifications - Pointers & Arrays

5
Clarifications — Variables escaping scope

• This leads to unde ned behaviour int* dangerousFunction() {


int temp = 10;
• Bad programming practice return &temp;
}

• Safe option int* safeFunction() {


int* heapVariable = new int(5);
return heapVariable;
}

6
fi
Debug Challenge

7
Debug Challenge

8
Linked Lists Head

• An abstract data type which


represents values in a sequence Val Nxt Val Nxt Val NULL

• Homogeneous container
• Need not be contiguous
• Dynamic data structure:
• The list can grow and shrink
• E cient memory utilisation
• Inventors: Allen Newell, Cli
Shaw, Herbert Simon in 1955
(RAND Corp. + CMU)
9
ffi
ff
Insertion in a Linked List
Head
• Insert @ head

5 Nxt 9 Nxt 2 NULL

Data NULL

• Similarly, insert after a given


node

• Q: How to insert at the end?


10
Deletion in a Linked List
Head
• Delete from head:
• tmp = head;
5 Nxt 9 Nxt 2 NULL
• head = head->next;
• delete tmp;
• Practice Q:
• Delete after a speci ed node
• Delete from the end

11
fi
Complexity of Operations in Linked List
Head
• Singly Linked List:
• Traversal of all elements: O(n)
5 Nxt 9 Nxt 2 NULL
• Insert/Delete from head: O(1)
• Insert/Delete from end: O(n)

12
Circular Linked List
Head
• The end node is connected
to the rst node

• Because it is a cycle — no Val Nxt Val Nxt Val Nxt

need to maintain a head ptr

Doubly Linked List Head

• Pointers both for next and


previous nodes
NULL Val Nxt Prv Val Nxt Prv Val NULL
• E cient insertions/deletions
• Can retrieve previous node
in O(1)
13
ffi
fi
Linked List: Applications
• In implementing Operating Systems:
• Round-robin scheduling requires runnable processes to be kept in a linked list.
• Scheduled process is removed from the head and added to the tail
• Eg: Linux, FreeRTOS, Cisco IOS for pkt management
• Web Browsers:
• Eg: Implementing the back button!
• Computer Networks:
• Implementation of Routers: Pat management, Route planning etc.
• Memory Management: Keeping track of allocated and deallocated mem blocks.
14

You might also like