Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Introduction and

Linked Lists
DSA.zip Week 1 Theory - 22/01/2020

Instructor: Anirudh S
First things first
➔ Who are we?
➔ What is DSA.zip about?
➔ Why did we start DSA.zip?
➔ What do we expect from you?
➔ What can you expect from us?
➔ Your intro

2
Definitions
● Data structure : A data structure is a way to store and organize data in order to facilitate access
and modifications.

● Algorithm : An algorithm is any well-defined computational procedure that takes some value, or
set of values, as input and produces some value, or set of values, as output.

3
Analogy
● We convey our thoughts to others using language
● Similarly, we tell the computer about the algorithm through programming language

That is,

Thought ↔ Algorithm Language ↔ Programming language

In this class, we use C++.

4
Recap - Structures
● A structure is a composite data types which can be used to group items of different data
types(simple, composite, pointers) as a single type.

Syntax: Example:

struct structName { struct student {


type1 x1; char name[100];
type2 x2; char rollNo[8];
. int age;
. char hostel[20];
. int roomNo;
}; };

5
C++
● C++ is C with classes and objects.
● Most of the syntax is the same as C. Use cin instead of scanf and cout instead of printf.

Example:
#include <bits/stdc++.h>
using namespace std;
int main () {
cout << “Enter two numbers.\n“;
int a, b;
cin >> a >> b;
cout << “Sum = “ << (a+b) << endl;
return 0;
}

6
Classes and Objects
● Class : In simple terms, class is a structure with methods.
● Object : An object is an instance of a class.

Example: Class Object


class Student { string getRollNo() {...}
private: string getAge() {...}
string name; }
string rollNo;
int age;
int main() {
public: Student s1, s2;
string getName() {...} …..
}
7
OOPs!
● OOP - Object oriented programming.
● Characteristics
a. Encapsulation: Wrapping up of data and methods into a single unit.
b. Abstraction: Exposing only relevant details and hiding the rest.
c. Polymorphism: Ability of something (here operators and functions) to take on different forms/meaning
depending on the context.
d. Inheritance: Deriving properties and characteristics from another class.

8
Constructor
● Constructor is a method which initialises an object.
● It is always defined in the public part of the code.
● It can have any random code ;-)
● It has the same name as that of the class and has no return type.
● Types of constructors:
○ Default: Takes no arguments
○ Parameterized: Takes arguments/parameters
○ Copy: Initialises an object with another object of the same class.

9
Constructor (examples)
class Point { Point(Point &p) {
private: x = p.x;
int x; y = p.y;
int y; }
.
Copy
public: Default .
Point() { }
x = 0;
y = 0; int main () {
} Point defPt;
Point parPt(10, 5);
Point (int x1, int y1) { Point copyPt = parPt;
x = x1; .
y = y1; Parameterized .
} return 0;
}

10
Standard Template Library (STL)
● The Standard Template Library (STL) is a set of C++ template classes to provide common
programming data structures and functions such as lists, stacks, arrays, etc.
● STL has four components:
○ Algorithms
○ Containers
○ Functions
○ Iterators
● Looking up STL for data structures covered will be your homework :-)

11
First data structure!
● Let us look at our first data structure.
● We want to store a sequence of elements.
● We want to support the following operations:
○ Insert
○ Delete
○ Find
● How can we do this? Anything which you have studied already?

12
Recap - Arrays
● An array is a collection of items of the same data type stored at contiguous memory location.
● Elements can be accessed randomly using respective indices.
● Advantages:
○ Quick and easy access. Takes constant time. (More elaboration in ‘Time complexity’ portion).
○ Blah blah blah
● Disadvantages:
○ Length is fixed.
○ Blah blah blah
● How to overcome this constraint?

13
Soln 1 - Dynamic array
● When the array is full, allocate twice the space and copy all the elements. Array size is now
doubled. Tadaa!!
● Similar implementation in vector. (Look up in STL)
● My implementation:

14
class Vector { void resize() {
private: int *newarr = new int[2 * max];
int *arr; for(int i = 0; i < size; i++) {
int sz, max; newarr[i] = arr[i];
}
public: max = 2 * max;
Vector() { arr = newarr;
arr = new int[1];
newarr = NULL;
max = 1;
}
sz = 0;
}
void delete(int n) {
void insert (int n, int x) { if(n < sz) {
if(n < sz) { for(j = n; j < sz -1; j--) {
for(int j = sz; j > n; j--) { arr[j] = arr[j+1];
arr[j] = arr[j-1]; }
} sz--;
arr[n] = x; }
sz++; }
}
else { int find(int n) {
arr[sz++] = x; if(n < sz)
} return arr[n];
if(sz == max) { else
resize(); return -1;
} }
}
15
Soln 2 - Linked Lists
● Store each element separately and keep a pointer to the next (and sometimes previous) element.
● Called singly linked list if it has only next ptr; called doubly linked list if it has both.
● Space efficient.
● Basic element(node) looks like:
struct Node {
int ele;
Node* next;
}

● You need a head node pointer to maintain the linked list (tail pointer is optional)
● You will be implementing singly linked list in the online practice question :-)

16
Singly Linked List - insert code
class LinkedList { void insert(int n, int el) { else { //Adding in between/to end
private: if(n > sz) { Node* curr = head;
struct Node { n = sz; for(int i = 0; i < n-1; i++) {
int x; } curr = curr->next;
Node* next; Node* nwNd = new Node; }
}; nwNd->x = el; nwNd->next = curr->next;
Node *head, *tail; nwNd->next = NULL; curr->next = nwNd;
int sz; if(n == sz) {
if(n == 0) { //Adding to beginning tail = nwNd;
public: nwNd = head; }
LinkedList() { head = nwNd; }
head = NULL; if(tail == NULL) {
tail = NULL; tail = nwNd; sz++;
} } }
}

17
Deletion and Find
● Deletion
○ Find the node to be deleted
○ Point the previous element’s next to the next element (or to NULL)
○ Point this node’s next to NULL
○ Update head and tail accordingly
● Find
○ Reach the element through traversal (curr = curr->next)
○ Return the element

18
List Data Structure
● List data structure is implemented using linked lists (doubly linked lists actually)
● This is one of the three basic data structures. The other two are stacks and queues.
● Look up the STL for lists

19
Fun operations on LL
Solution:
● Reversing a linked list:
Node *curr, *prev, *next;
○ Input: Head pointer to a LL
prev = NULL;
○ Output: Head pointer to reversed LL.
curr = head;
while(curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}

return prev;

20
References
1. Introduction to algorithms, CLRS
2. GeeksForGeeks

21
Thank you!

22

You might also like