CS301-Lec06 handout - CS301 Data Structures Lecture No 06...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS301 – Data Structures Lecture No. 06 ___________________________________________________________________ Data Structures Lecture No. 06 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 3 3.3.2, 3.3.3 (Postfix expressions) Summary Stack From the Previous Lecture Stack Using Linked List Stack Implementation: Array or Linked List Use of Stack Precedence of Operators Examples of Infix to Postfix Stack From the Previous Lecture We started discussing Stack data structure and its implementation in the previous lecture. We also implemented stack structure using an array and wrote code for its push(), pop() and top() operations. We realized that we have to specify the size of the array before using it whether we declare it statically or dynamically. Arrays are of fixed size and when they become full, no more elements can be added to them. In order to get to know that the array has gone full, we wrote the isFull() method. It became the responsibility of the user of the stack structure to call isFull() method before trying to insert an element using the push() method otherwise the whole program could crash. isEmpty() method is implemented as a stack can be empty like a list or set structures. It is important to understand that isFull() method is there in stack implementation because of limitation of array but isEmpty() method is part of the stack characteristics or functionality. As previously in the implementation of list structure, we used linked list while allocating nodes dynamicallyin order to avoid the fixed sized limitation of array. Now in this case also, again to overcome the limitation of array, we are going to make use of linked list in place of array to implement the stack data structure. Let’s see, how we can implement a stack structure using linked list and how the implementation code will look like internally. Page 1 of 7
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS301 – Data Structures Lecture No. 06 ___________________________________________________________________ Stack Using Linked List We can avoid the size limitation of a stack implemented with an array, with the help of a linked list to hold the stack elements. As needed in case of array, we have to decide where to insert elements in the list and where to delete them so that push and pop will run at the fastest. Primarily, there are two operations of a stack; push() and pop() . A stack carries lifo behavior i.e. last in, first out. You know that while implementing stack with an array and to achieve lifo behavior, we used push and pop elements at the end of the array. Instead of pushing and popping elements at the beginning of the array that contains overhead of shifting elements towards right to push an element at the start and shifting elements towards left to pop an element from the start. To avoid this overhead of shifting left and right, we decided to push and pop elements at the end of the array. Now, if we use linked list to implement the stack, where will we
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/21/2010 for the course CS CS301 taught by Professor Dr.naveed malik during the Spring '10 term at Virtual University of Pakistan.

Page1 / 7

CS301-Lec06 handout - CS301 Data Structures Lecture No 06...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online