Docsity

Stack ADT: Definition and Singly-linked-list-based Implementation - Prof. W. Duncan, Study notes of Computer Science

Toggle sidebar
bg1
CSc 1254: Lecture # 15
Stack ADT
October 22, 2009
•Definition of A Stack
•Singly-linked-list-based Stack ADT
1 Definition of A Stack
A stack is a LIFO (last-in-first-out) linear data structure.
Definition 1. A stack, S, of item type T is formally defined as a sequence
of items of type T on which the following operations are defined:
1. Create the stack S to be the empty stack.
2. Determine whether or not the stack, S, is empty.
3. Determine whether or not the stack, S, is full if you wish the stack to
have a maximum capacity.
4. push a new item onto the top of the stack, S.
5. If S is nonempty, pop an item from the top of stack, S.
Environments with heavy reliance on stacks may provide additional op-
erations. Some of these are:
a. debug: display the contents of the stack.
b. size: returns the number of elements on the stack.
c. top: determines the item at the top of the stack.
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Stack ADT: Definition and Singly-linked-list-based Implementation - Prof. W. Duncan and more Study notes Computer Science in PDF only on Docsity!

CSc 1254: Lecture # 15

Stack ADT

October 22, 2009

  • Definition of A Stack
  • Singly-linked-list-based Stack ADT

1 Definition of A Stack

A stack is a LIFO (last-in-first-out) linear data structure.

Definition 1. A stack, S, of item type T is formally defined as a sequence of items of type T on which the following operations are defined:

  1. Create the stack S to be the empty stack.
  2. Determine whether or not the stack, S, is empty.
  3. Determine whether or not the stack, S, is full if you wish the stack to have a maximum capacity.
  4. push a new item onto the top of the stack, S.
  5. If S is nonempty, pop an item from the top of stack, S.

Environments with heavy reliance on stacks may provide additional op- erations. Some of these are:

a. debug: display the contents of the stack.

b. size: returns the number of elements on the stack.

c. top: determines the item at the top of the stack.

d. swap: two top-most items are exchanged.

e. rotate: the three topmost items exchange places in a rotary fashion. Two variants of this operation are possible, most often called left and right rotate.

f. dup: the top item is popped and pushed again twice.

2 A Singly-linked-list-based Stack ADT

Here we consider a singly-linked-list-based implementation of the stack ADT. As is the case with any abstract data type, a variety of implementations are possible: one with iterators is possible, one with a doubly-linked list without iterators, just to name a few. The choice of which implementation is best de- pends on the particular application for which the ADT is been implemented. We leave the strategy for implementing a doubly-linked-list-based stack with an iterator class for classroom discussion. Pay close attention to the syn- tax when a templatized class grants friendship to another templatized class. Also, observe the forward declaration of the friend class before the class that grants the friendship.

template class Stack { public: /** Constructs an empty stack; */ Stack();

/** destructor - returns the stack memory to the system; */ ~Stack();

/** Determine whether the stack is empty. @return this function returns true if the stack is empty; otherwise, it returns false if the stack contains at least one node. */ bool isEmpty() const;

/** Inserts an item into the stack. @param item the value to be inserted. */ void push(T item);

/** Returns the item at the top of a non-empty stack or generates an exception if the stack is empty. @return item at the top of the stack. */ const T& top() const throw (StackException);

Delete an item from the top of the stack. Generates an exception if the stack is empty. @return item at the top. */ T pop() throw (StackException);

returns the size of the stack. @return the size of the stack / long size() const; private: template class Node; Node head; long length; };

/* nested Node class definition / template template class Stack::Node { public: /* Constructs a node with a given data value. @param s the data to store in this node / Node(T s); private: T data; Node next; friend class Stack; }; //Stack_H #endif

template bool Stack::isEmpty() const { return (length == 0); }

template void Stack::push(T item) { Node* tmp; Node* newNode = new Node(item); /* If it is the first node in the tree */ if (isEmpty()) { head = newNode; } else { newNode->next = head; head = newNode; } length++; }

template const T& Stack::top() const throw (StackException) { if (head == NULL) throw StackException("Stack Exception: top() called on empty stack."); return head->data; }

template T Stack::pop() throw (StackException) { Node* nodePtr; T item; if (head == NULL) throw StackException("Stack Exception: top() called on empty stack."); nodePtr = head; item = head->data; head = head->next; delete nodePtr; length--; return item; }

template long Stack::size() const { return length; }

while (!nums.isEmpty()) { cout<<nums.top()<<" is at the top of the stack."<<endl; x = nums.pop(); cout<<x<<" has been removed from the front."<<endl; } } catch(StackException e) { cerr<<e.what(); } cout<<endl; return 0; }

Practice Exercise (not-to-be turned in for grading):

  1. Add the following member-functions to the ADT: swap, leftRotate, rightRo- tate and dup, as described above.
  2. Add the functions above as non-member ADT functions.
  3. What is the output of the code above?