linked lists - github pages · create a singe node linked list ml node nti m1 n1 data 10s data nest...

13
LINKED LISTS Problem Solving with Computers-I

Upload: others

Post on 26-Mar-2020

27 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: LINKED LISTS - GitHub Pages · Create a singe node linked list Ml Node nti M1 n1 data 10s data nest mi next 2 O's Node 25 ni m2 dates w n2 next 0 Address ofthe nett node data next

LINKED LISTSProblem Solving with Computers-I

Page 2: LINKED LISTS - GitHub Pages · Create a singe node linked list Ml Node nti M1 n1 data 10s data nest mi next 2 O's Node 25 ni m2 dates w n2 next 0 Address ofthe nett node data next

Store a sequence of numbers or

names or snacks or anydata

1 Array Fixed size

int arr 100J 11 Only storea 100 numbers

2 Linked list Variable size

Main difference comparedto aways

Elements are not next to each

other in memory

arr T B Nggde

0 8000 0 8004 0 8008

edqtaAddress of the

next boogie

did FEDhead Node

RepresentinganodestructNode

int dataNodes next

o

3

Page 3: LINKED LISTS - GitHub Pages · Create a singe node linked list Ml Node nti M1 n1 data 10s data nest mi next 2 O's Node 25 ni m2 dates w n2 next 0 Address ofthe nett node data next

Create a singe node linked listMl

Node nti M 1n1 data 10 s data nestmi next 2 O's

Node25 nim2 dates w

n2 next 0Addressofthe nett

nodedata next

t.TOhead next 0 7,000400

ml next h2

n1 data 50

Node head

Print the data of both nodes using

thevariableheadcouta head data

Page 4: LINKED LISTS - GitHub Pages · Create a singe node linked list Ml Node nti M1 n1 data 10s data nest mi next 2 O's Node 25 ni m2 dates w n2 next 0 Address ofthe nett node data next

head 2 next r

T.gscout Ls head data

cont Cs headnext data

cont CC head next next

outschead next next da

Page 5: LINKED LISTS - GitHub Pages · Create a singe node linked list Ml Node nti M1 n1 data 10s data nest mi next 2 O's Node 25 ni m2 dates w n2 next 0 Address ofthe nett node data next

Accessing elements of a linked list

Assume the linked list has already been created, what do the following expressions evaluate to?1. head->data2. head->next->data3. head->next->next->data4. head->next->next->next->data

A. 1B. 2C. 3D. NULLE. Run time error

head

struct Node {int data;Node *next;

};

is weeDaerneffgg

Seg fare

Page 6: LINKED LISTS - GitHub Pages · Create a singe node linked list Ml Node nti M1 n1 data 10s data nest mi next 2 O's Node 25 ni m2 dates w n2 next 0 Address ofthe nett node data next

Creating a small list3

• Define an empty list• Add a node to the list with data = 10

struct Node {int data;Node* next;

};

Page 7: LINKED LISTS - GitHub Pages · Create a singe node linked list Ml Node nti M1 n1 data 10s data nest mi next 2 O's Node 25 ni m2 dates w n2 next 0 Address ofthe nett node data next

Heap vs. stack

Node* createSmallLinkedList(int x, int y){Node* head = NULL;Node n1 ={x, NULL};Node n2 ={y, NULL};head = &n1;n1->next = &n2;return head;

}

Does the above function correctly return a two-node linked list?A. YesB. No

n Steed nzstad

Node pM 12211

pecreat Io 20 P

The nodes are removed fromthe stack

after the functionreturns

Page 8: LINKED LISTS - GitHub Pages · Create a singe node linked list Ml Node nti M1 n1 data 10s data nest mi next 2 O's Node 25 ni m2 dates w n2 next 0 Address ofthe nett node data next

Stackhead Do empty

Heap value

Case I data next

n to li ID

case 2 Before calling insertNode

Heap head Heap

notE g

After calling insert Node

p

Page 9: LINKED LISTS - GitHub Pages · Create a singe node linked list Ml Node nti M1 n1 data 10s data nest mi next 2 O's Node 25 ni m2 dates w n2 next 0 Address ofthe nett node data next

Creating a small list5

• Define an empty list

• Add a node to the list with data = 10

struct Node {int data;Node* next;

};

struct LinkedList {Node* head;Node* tail;

};

Page 10: LINKED LISTS - GitHub Pages · Create a singe node linked list Ml Node nti M1 n1 data 10s data nest mi next 2 O's Node 25 ni m2 dates w n2 next 0 Address ofthe nett node data next

Inserting a node in a linked listvoid insert(LinkedList* h, int value) ;

Page 11: LINKED LISTS - GitHub Pages · Create a singe node linked list Ml Node nti M1 n1 data 10s data nest mi next 2 O's Node 25 ni m2 dates w n2 next 0 Address ofthe nett node data next

Iterating through the listint count(LinkedList* list) {

/* Find the number of elements in the list */

}

head tail

list

Page 12: LINKED LISTS - GitHub Pages · Create a singe node linked list Ml Node nti M1 n1 data 10s data nest mi next 2 O's Node 25 ni m2 dates w n2 next 0 Address ofthe nett node data next

Deleting the listint freeList(LinkedList * list) {

/* Free all the memory that was created on the heap*/

}

head tail

list

Page 13: LINKED LISTS - GitHub Pages · Create a singe node linked list Ml Node nti M1 n1 data 10s data nest mi next 2 O's Node 25 ni m2 dates w n2 next 0 Address ofthe nett node data next

Next time• Memory-related errors• Double-linked lists