The easiest way to implement Stack ADT is using array. The following data members define the stack

Unit – II: LISTS, STACK AND QUEUE Singly Linked Lists - Circular Linked Lists - Doubly Linked Lists - General Lists-Stacks – Queues - Evaluation of Ex...
2 downloads 0 Views 2MB Size
Unit – II: LISTS, STACK AND QUEUE Singly Linked Lists - Circular Linked Lists - Doubly Linked Lists - General Lists-Stacks – Queues - Evaluation of Expressions-Multiple Stacks and Queues.

STACKS     

The stack is a linear data structure. All insertions and deletions are performed at the top of the stack. It uses the technique called LIFO: Last In, First Out. There are two operations namely PUSH and POP associated with stack. The push operation is used to insert an element onto the stack and the pop operation is to remove the topmost stack element.  The following Example shows the execution of the PUSH and POP operations.

Stack Abstract Data Type  The easiest way to implement Stack ADT is using array. The following data members define the stack. Private : T * stack; int top; int size;

// array for stack elements // array position of top elements // capacity of stack array

 The stack ADT can be defined with a constructor method Stack(), IsEmpty() to check if the stack is empty or not, Top() to return the top element of the stack, push() to insert the element and pop() to delete the top element of the stack. The class is represented as follows.

The implementation of Push() and Pop() functions are given below.

QUEUE  The queue is a linear data structure.  Queue is an ordered list in which insertions are performed at the rear end and deletions are performed at the front end of the queue.  There are two operations namely PUSH and POP associated with queue  The push operation is used to insert an element onto the stack and the pop operation is to remove the topmost stack element.  It uses the technique called FIFO: First In, First Out.

Rear

Front

 The following Example shows the execution of the PUSH and POP operations.

Queue Abstract Data Type  The easiest way to implement Queue ADT is using array. The following data members define the queue.

Private : T * queue; int front,rear; int capacity;

// array for stack elements // array position of front&rear elements // capacity of queue array

 The queue ADT can be defined with a constructor method Queue(), IsEmpty() to check if the stack is empty or not, Front() to return the element at the front of the queue, Rear() to return the element at the rear of the queue, push() to insert the element at the rear and pop() to delete the front element of the queue. The class is represented as follows.

Implementation for the functions in the Queue is described as follows.

Circular Queue  A circular queue is a particular implementation of a queue. It is very efficient. The insertion and deletion are totally independent.

 A circular queue consists of an array that contains the items in the queue, two array indexes and an optional length. The indexes are called the Front and Rear pointers on the diagram.

 The Front pointer points to the first element in the queue, and the Rear pointer points just beyond the last element in the queue. If the Rear pointer is before the Front pointer, the queue wraps around the end of the array.  To insert an element, write the element to the Rear index and increment the Rear, wrapping if necessary.  To delete an element, save the Front element and increment the Front wrapping if necessary. ` LINKED LIST  A linked list is a collection of objects linked together by references from one object to another object.

 By convention these objects are named as nodes. So the basic linked list is collection of nodes where each node contains one or more data fields and a reference to the next node. The last node points to a NULL reference to indicate the end of the list. The entry point into a linked list is always the first or head of the list. It should be noted that head is NOT a separate node, but a reference to the first Node in the list. If the list is empty, then the head has the value NULL.  In a singly linked list, each node has exactly one pointer field. A chain is a singly linked list that is comprised of zero or more nodes.  The following diagram represents a chain.

Linked List Abstract Data Type – Representing chains Defining a node Each node contains one or more data fields and a reference to the next node. It can be declared as, Class Node { private: int data; Node *link; }; A chain is a singly linked list that is comprised of zero or more nodes and it can be declared as, Class chain { public : // chain manipulation operation private: Class Node { private: int data; Node *link; }; Node *first; };

Chain manipulation operation Class ChainNode { friend class chain; public : // chain manipulation operation ChainNode(int element = 0, ChainNode *next=0) { data=element;link=next ;} private: int data; ChainNode *linkfirst; }; The following section gives the implementation of the related methods, such as Creating nodes, Inserting a node and Deleting a node.

CIRCULAR LIST  In a circular list, the link field of the last node points to the first node. The following diagram shows a circular list.

 Procedure for inserting a node at the front of a circular list is as below.

DOUBLY LINKED LIST  In a doubly linked list each node has data field and two link fields (Leftlink, Rightlink), one linking in the forward direction and the other in backward direction.  The following diagram represents a doubly linked list.

 The class definition of Doubly linked list can be as follows.

EVALUATION OF EXPRESSIONS EXPRESSIONS  An expression is made up of operands, operators, and delimiters. For example X=(A+B)*C/D-E is an arithmetic expression having only basic arithmetic operators (+,-,*,/) .  Expressions can also be formed with relational operators such as

Suggest Documents