lecture 6: linked list

61
Placement Preparation Linked List Shobhit Chaurasia B.Tech, CSE

Upload: vivek-bhargav

Post on 05-Jul-2015

67 views

Category:

Engineering


0 download

DESCRIPTION

This slide is part of course designed for placement preparation for students of IIT Guwahati.

TRANSCRIPT

Page 1: Lecture 6:  linked list

Placement PreparationLinked List

Shobhit ChaurasiaB.Tech, CSE

Page 2: Lecture 6:  linked list

Target Audience

People who have coding experience limited to the basic

syntax of C++, pointers and structures, but didn’t take

CS101 seriously and want to learn some basics of coding

quickly.

If you already know how to implement a linked list, then

please spare me the pain of taking a lecture on linked

lists, and volunteer to deliver this lecture.

Page 3: Lecture 6:  linked list

Motivation

Suppose I have an array: 1,4,10,19,6

I want to insert a 7 between the 4 and the 10

What do we need to do?

Page 4: Lecture 6:  linked list

Linked List

head

Page 5: Lecture 6:  linked list

Node

Page 6: Lecture 6:  linked list

How to represent a Node in C++ ?

How to represnt integers in C++?

int x;

But Node is a combination of data + pointer

(int) + pointer

(char) + pointer

(string) + pointer

Page 7: Lecture 6:  linked list

Structures Revisited

Structure variable

stud s;

Access members of struct

s.rollNo = 11010179

s.cpi = 8.1

s.gender = ‘M’

;

Page 8: Lecture 6:  linked list

How to represent Node in C++

;

Page 9: Lecture 6:  linked list

Head

Pointer to the first node.Holds the address of the first node in the list

head

Page 10: Lecture 6:  linked list

Head

Pointer to the first node.

node* head

;

Page 11: Lecture 6:  linked list

Struct Pointer revisited

Struct Pointer

stud *s;

Access members of struct

s->rollNo = 11010179

s->cpi = 8.1

s->gender = ‘M’rollNo cpi gender

s

;

Page 12: Lecture 6:  linked list

Accessing data/next node

node* head

Data stored at first node

head->data = ‘a’;

Pointer to second node

head->next

Data stored at second node

(head->next)->data = ‘b’;

head

;

Page 13: Lecture 6:  linked list

Demo #0(a)

node* temp;

temp = head;

cout<<temp->data;

head

Page 14: Lecture 6:  linked list

Demo #0(a)

How to go to the second node?

temp = temp->next;

cout<<temp->data;

head

Page 15: Lecture 6:  linked list

Demo #0(a)

How to go to the third node?

temp = temp->next;

cout<<temp->data;

head

Page 16: Lecture 6:  linked list

Where’s the list’s end?

headptr

Page 17: Lecture 6:  linked list

Demo #0(b)

Printing the linked list

Page 18: Lecture 6:  linked list

Demo #0(b)

Printing the linked list (correct version)

Page 19: Lecture 6:  linked list

Demo #0(b)

An observation

Page 20: Lecture 6:  linked list

Demo #0(b) - MORAL

Never move the HEAD while traversing a

linked list

Page 21: Lecture 6:  linked list

Demo #1

Function to print the 3rd element in linked list

Page 22: Lecture 6:  linked list

Insert a new node at the end

node* temp;

temp = start;

temp->next == NULL ??

Page 23: Lecture 6:  linked list

Insert a new node at the end

temp = temp->next;

temp->next == NULL ??

Page 24: Lecture 6:  linked list

Insert a new node at the end

temp = temp->next;

temp->next == NULL ??

Page 25: Lecture 6:  linked list

Insert a new node at the end

temp = temp->next;

temp->next == NULL ??

Page 26: Lecture 6:  linked list

Insert a new node at the end

temp = temp->next;

temp->next == NULL ?? (YES!! Finally reached the end)

Page 27: Lecture 6:  linked list

Insert a new node at the end

ptr

node* ptr = new node

Page 28: Lecture 6:  linked list

Insert a new node at the end

ptr

ptr->data = ‘f’;

ptr->next = NULL;

f

Page 29: Lecture 6:  linked list

Insert a new node at the end

ptr

temp->next = ptr;

Page 30: Lecture 6:  linked list

Demo #2

Inserting a new node at the end.Discuss 2.cpp

Page 31: Lecture 6:  linked list

Insert a new node at the start

node* ptr = new node;

ptr

Page 32: Lecture 6:  linked list

Insert a new node at the start

node* ptr = new node;

ptr->data = ‘z’;

ptrz

Page 33: Lecture 6:  linked list

Insert a new node at the start

ptr->next = start;

ptr

Page 34: Lecture 6:  linked list

Insert a new node at the start

start = ptr;

ptr

Page 35: Lecture 6:  linked list

Demo #3

Insert a new node at the headDiscuss 3.cpp (two methods)

Page 36: Lecture 6:  linked list

Demo #4

Create a linked list and print its contents.

Page 37: Lecture 6:  linked list

Demo #5

Same as last problem. Use functions.

Page 38: Lecture 6:  linked list

Insert after a particular node

Page 39: Lecture 6:  linked list

Demo #6

Write a function which takes as parameter a

node pointer and inserts a new node after it.

insertInBetween (node* temp)

Page 40: Lecture 6:  linked list

Deleting first node

Page 41: Lecture 6:  linked list

Deleting first node

Page 42: Lecture 6:  linked list

ACTUALLY Deleting first node

Page 43: Lecture 6:  linked list

ACTUALLY Deleting first node

Page 44: Lecture 6:  linked list

ACTUALLY deleting first node

Page 45: Lecture 6:  linked list

ACTUALLY deleting first node

delete frees the memory allocated by new

Page 46: Lecture 6:  linked list

Demo #7

Write a function to delete the first node of a

linked list.Add this function to 5.cpp

Page 47: Lecture 6:  linked list

Demo #8

Write a function to delete the last node of

linked listAdd this function to 7.cpp

Page 48: Lecture 6:  linked list

Delete a node inside the linked list

Page 49: Lecture 6:  linked list

Delete a node inside the linked list

Page 50: Lecture 6:  linked list

Locating the node “y”

Page 51: Lecture 6:  linked list

Can we delete the node “y” now?

Having just the pointer to the node containing

“y” sufficient to delete it?

Page 52: Lecture 6:  linked list

Break this link, and make it point to the

node “d”

What do we want to do?

Page 53: Lecture 6:  linked list

Expected Output

Page 54: Lecture 6:  linked list

What do we need?

Pointer to the previous node

Page 55: Lecture 6:  linked list

Step 1

prev->next = temp->next;

Page 56: Lecture 6:  linked list

Step 2

delete temp;

Page 57: Lecture 6:  linked list

Final output

Page 58: Lecture 6:  linked list

Demo #9

Delete the node with data = 3Add this function to 5.cpp

Page 59: Lecture 6:  linked list

Homework

Reverse a linked list.

Page 60: Lecture 6:  linked list

Linked list vs Arrays

Operation Linked List Array

Insert at start O(1) O(n)

Insert anywhere O(1) O(n)

Delete anywhere O(1) O(n)

Access ith element O(n) O(1)

Page 61: Lecture 6:  linked list

THANK YOU