data structures - lecture 9 [stack & queue using linked list]

12
Stack & Queue Using Linked List INSTRUCTOR : MUHAMMAD HAMMAD WASEEM EMAIL: [email protected]

Upload: muhammad-hammad-waseem

Post on 15-Jul-2015

332 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Data Structures - Lecture 9 [Stack & Queue using Linked List]

Stack & Queue

Using Linked ListINSTRUCTOR : MUHAMMAD HAMMAD WASEEM

EMAIL: [email protected]

Page 2: Data Structures - Lecture 9 [Stack & Queue using Linked List]

Stack Using Linked List

We can avoid the size limitation of a stack implemented with an

array by using a linked list to hold the stack elements.

As with array, however, we need to decide where to insert elements in the list and where to delete them so that push and pop will run

the fastest.

2

Page 3: Data Structures - Lecture 9 [Stack & Queue using Linked List]

Stack Using Linked List

For a singly-linked list, insert at start or end takes constant time using

the head and current pointers respectively.

Removing an element at the start is constant time but removal at the end required traversing the list to the node one before the last.

Make sense to place stack elements at the start of the list because

insert and removal are constant time.

3

Page 4: Data Structures - Lecture 9 [Stack & Queue using Linked List]

Stack Using Linked List

No need for the current pointer; head is enough.

4

top

2

5

7

11 7 5 2

head

Page 5: Data Structures - Lecture 9 [Stack & Queue using Linked List]

Stack Operation: List

int pop()

{

int x = head->info;

head = head->link;

return x;

}

5

top

2

5

71 7 5 2

head

Page 6: Data Structures - Lecture 9 [Stack & Queue using Linked List]

Stack Operation: List

void push(int x)

{

ListNode *newNode;

newNode = new ListNode;

newNode->info = x;

newNode->link=head;

head = newNode;

}

6

top

2

5

7

9

7 5 2

head

push(9)

9

newNode

Page 7: Data Structures - Lecture 9 [Stack & Queue using Linked List]

Stack Operation: List

int top(){

return head->info;} int IsEmpty(){

return ( head == NULL );}

All operations take constant time.

7

Page 8: Data Structures - Lecture 9 [Stack & Queue using Linked List]

Stack: Array or List

Since both implementations support stack operations in constant time, any reason to choose one over the other?

Allocating and de-allocating memory for list nodes does take more time than pre-allocated array.

List uses only as much memory as required by the nodes; array requires allocation ahead of time.

List pointers (head, next) require extra memory.

Array has an upper limit; List is limited by dynamic memory allocation.

8

Page 9: Data Structures - Lecture 9 [Stack & Queue using Linked List]

Queue Using Linked List

Insert an element at the start and removal at the end but it required

extra overhead of traversing the list to the node one before the last.

9

Page 10: Data Structures - Lecture 9 [Stack & Queue using Linked List]

Queue Using Linked List

No need for the current pointer; head is enough.

10

rear

2571 1 7 5 2

rear front

front

Page 11: Data Structures - Lecture 9 [Stack & Queue using Linked List]

Queue Operation: enqueue

void enqueue(int x)

{

ListNode *newNode;

newNode = new ListNode;

newNode->info = x;

newNode->link=head;

head = newNode;

}

11

Page 12: Data Structures - Lecture 9 [Stack & Queue using Linked List]

Queue Operation: dequeue

Void dequeue()

{

nodePtr = head;

while (nodePtr->link != NULL){

previousNode = nodePtr;nodePtr = nodePtr->next;

}previousNode->link = nodePtr->link;delete nodePtr;

}

12