cs 261: data structures linked lists list...

28
CS 261: Data Structures Linked Lists List Stack 1

Upload: others

Post on 13-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

CS 261: Data Structures

Linked ListsList Stack

1

Page 2: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

Dynamic Array -- Problems

• Data kept in a single large block of memory

• Often more memory used than necessary

– especially when repeatedly growing and

shrinking the dynamic array

2

Page 3: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

Linked List

• A good alternative

• The memory use is always proportional to the

number of elements in the collection

3

Page 4: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

Characteristics of Linked Lists

• Elements are held in objects called Links

• Links are 1-to-1 with data elements, allocated and released as necessary

data

link

data

link

…nextlink

nextlink

4

Page 5: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

Single and Double Linked Lists

Each link points to only next link à single linked listnext and previous links à double linked list

in the sequence

data

link

data

link

…nextlink

nextlink

previouslink

previouslink

5

Page 6: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

Link Structure

struct Link { TYPE value;struct Link *next;

};

val next

6

Page 7: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

Elements of Linked Lists• Sentinel -- special link for start or end

• Points to

– first or last link only (single linked list)

– first and last link (double linked list)

7

Page 8: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

List Stack

• Sentinel points to the first element

• Sentinel points to NULL if stack empty

• Add or remove elements only from front

• Allow only singly linked list

• Can access only first element8

Page 9: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

List Stack

sentinelnext:

val: 2

next:

val: 7

next:

val: 4

next: null

stk

9

Page 10: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

Implementation of List Stackstruct Link {

TYPE value;struct Link * next;

};

struct ListStack {struct Link * sentinel;

};

How to initialize List Stack?10

Page 11: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

InitStackvoid InitStack(struct ListStack * stk){

/*initialize the sentinel*/struct Link *sentinel =

(struct Link *)malloc(sizeof(struct Link));

assert(sentinel != 0);

/*linked list is empty*/sentinel->next = NULL;stk->sentinel = sentinel;

}

stk

nextsentinel 11

Page 12: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

Push List Stack: 3 Steps

sentinel =

val: 2

next:

val: 7

next:

val: 4

next: null

Step 1: allocate new link

val: 5

next:null

stk

next

12

Page 13: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

Push List Stack: 3 Steps

sentinel

val: 2

next:

val: 7

next:

val: 4

next: null

Step 2: link the new element to the next data element

val: 5

next:

stk

next

13

Page 14: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

Push List Stack: 3 Steps

sentinel

val: 2

next:

val: 7

next:

val: 4

next: null

Step 3: add the new element to the top

val: 5

next:

stk

next

14

Page 15: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

Push List Stack: 3 Steps

void pushStack (struct listStack *stk, TYPE val){

struct Link * new =

(struct Link *) malloc(sizeof(struct Link));

assert (new != 0);

new->value = val;

}

15

sentinel =

val: 2next:

val: 7next:

val: 4next: null

Step 1: allocate new link

val: 5next:null

stk

next

Page 16: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

Push List Stack: 3 Steps

void pushStack (struct listStack *stk, TYPE val){

struct Link * new =

(struct Link *) malloc(sizeof(struct Link));

assert (new != 0);

new->value = val;

new->next = stk->sentinel->next;

} 16

sentinel

val: 2next:

val: 7next:

val: 4next: null

Step 2: link the new element to the next data element

val: 5next:

stk

next

Page 17: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

Push List Stack: 3 Steps

void pushStack (struct listStack *stk, TYPE val){

struct Link * new =

(struct Link *) malloc(sizeof(struct Link));

assert (new != 0);

new->value = val;

new->next = stk->sentinel->next;

stk->sentinel->next = new;

}

17

sentinel

val: 2next:

val: 7next:

val: 4next: null

Step 3: add the new element to the top

val: 5next:

stk

next

Page 18: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

PopStack

void PopStack (struct ListStack *stk) {struct Link * lnk = stk->sentinel->next;if(lnk!=NULL){ /*the top element exists*/

}

sentinel

val: 2

next:

val: 7

next:

val: 4

next: null

val: 5

next:

stk

lnk 18

/*move the top to the next element*/

Page 19: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

PopStack

void PopStack (struct ListStack *stk) {struct Link * lnk = stk->sentinel->next;if(lnk!=NULL){ /*the top element exists*/ stk->sentinel->next = lnk->next;free(lnk);

}}

sentinel

val: 2

next:

val: 7

next:

val: 4

next: null

re-linkval: 5

next:

stk

lnk 19

/*move the top to the next element*/

Page 20: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

topStack, isEmpty...

• Should be done on your own

• Worksheet 17

20

Page 21: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

List Stack vs. Dyn. Array Stack

List Dyn. ArraypushStack O( 1 ) O( 1 )popStack O( 1 ) O( 1 )topStack O( 1 ) O( 1 )

21

Page 22: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

List Bag

• Init, Add operations are similar to List Stack

• Contains and Remove operations are tricky

• How to patch up links after removing an element?

22

Page 23: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

Removevoid removeListBag(struct ListBag *b, TYPE val) {

}

sentinel

val: 2

next:

val: 7

next:

val: 4

next: null23

Page 24: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

Removevoid removeListBag(struct ListBag *b, TYPE val) {

struct Link *previous = b->sentinel;struct Link *current = b->sentinel->next;while (current != NULL){

}}

sentinel

val: 2

next:

val: 7

next:

val: 4

next: null

previous

current

24

Page 25: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

Removevoid removeListBag(struct ListBag *b, TYPE val) {

struct Link *previous = b->sentinel;struct Link *current = b->sentinel->next;while (current != NULL){ if (EQ(current->value,val)) { previous->next = current->next;

}}

}

sentinel

val: 2

next:

val: 7

next:

val: 4

next: nullprevious current

25

Page 26: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

Remove – First Occurrencevoid removeListBag(struct ListBag *b, TYPE val) {

struct Link *previous = b->sentinel;struct Link *current = b->sentinel->next;while (current != NULL){ if (EQ(current->value,val)) { previous->next = current->next;free(current);return; /*removes only the first occurrence*/

}previous = current;current = current->next;

}}

sentinel

val: 2

next:

val: 7

next:

val: 4

next: nullprevious current

26

Page 27: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

Remove -- All Occurrencesvoid removeListBag(struct ListBag *b, TYPE val) {

struct Link *previous = b->sentinel;struct Link *current = b->sentinel->next;while (current != NULL){ if (EQ(current->value,val)) { previous->next = current->next;free(current); current = previous;

}previous = current;current = current->next;

}}

sentinel

val: 2

next:

val: 7

next:

val: 4

next: nullprevious current

27

Page 28: CS 261: Data Structures Linked Lists List Stackweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-04-20 · Linked Lists List Stack 1. Dynamic Array --Problems •Data

When you find it

• When you find the element to be deleted, what does previous point to?

• What if the element to be deleted is at the front of the list? Does this matter?

28