Docsity

Description of Data Structures and Algorithms, Summaries of Data Structures and Algorithms

Partial preview of the text

Download Description of Data Structures and Algorithms and more Summaries Data Structures and Algorithms in PDF only on Docsity!

III. Specify an abstract data type for the software stack

I. Create a design specification for the data structure and explain what valid operations can be performed on the structure

II. What is the stack data type? Operations on the stack data type

Abstract Data Types

Abstract Data type (ADT) is a type (or class) for objects whose behaviour is defined by a set of value and a set of operations.

Before a program can be written, the programmer must know what tasks the program does. The inputs and outputs of each task must be clearly defined from the outset. Also decide which data structure to use to be most efficient in terms of time and space. An items that specifies operations is called an abstract data type. In Java, ADT can be part of a program as an interface.

Example of ADT

ADT example: LIST ADT

  • A list contains elements of same type arranged in sequential order and following operations can be performed on the list.
  • get() โ€“ Return an element from the list at any given position.
  • insert() โ€“ Insert an element at any position of the list.
  • remove() โ€“ Remove the first occurrence of any element from a non-empty list.
  • removeAt() โ€“ Remove the element at a specified location from a non-empty list.
  • replace() โ€“ Replace an element at any position by another element.
  • size() โ€“ Return the number of elements in the list.
  • isEmpty() โ€“ Return true if the list is empty, otherwise return false.
  • isFull() โ€“ Return true if the list is full, otherwise return false.

II. What is the stack data type?

Operations on the stack data type

Stack is an abstract data structure that operates on the Last In First Out (LIFO) principle. That is, the last element inserted on the stack will be the first element removed from the stack.

Stack memory

Operations on the memory stack

The stack data structure is limited in the same way as above. Thus, our stack manipulation includes only the following actions:

Push: Add an element to the top of the stack, the number of elements of the stack increases by 1. Pop: Removes the first element at the top of the stack, the number of elements on the stack is reduced by 1. Top: Get the value of the first element at the top of the stack, the number of elements of the stack does not change. IsEmpty: Check if the stack is empty or not. An empty stack is a stack with no elements. IsFull: Checks whether the stack is full or not. This action is not always available. Size: Gets the number of stack elements available.

Code demo Push

Pop Operation Accessing the content while removing it from the stack, is known as a Pop Operation. In an array implementation of pop() operation, the data element is not actually removed, instead top is decremented to a lower position in the stack to point to the next value. But in linked-list implementation, pop() actually removes data element and deallocates memory space. A Pop operation may involve the following steps โˆ’

  • Step 1 โˆ’ Checks if the stack is empty.
  • Step 2 โˆ’ If the stack is empty, produces an error and exit.
  • Step 3 โˆ’ If the stack is not empty, accesses the data element at which top is pointing.
  • Step 4 โˆ’ Decreases the value of top by 1.
  • Step 5 โˆ’ Returns success.