0% found this document useful (0 votes)
101 views8 pages

Introduction To Data Structures

The document provides an introduction to data structures, including linear and non-linear structures. Linear structures like linked lists store elements sequentially, while non-linear structures like trees have elements attached through relationships rather than sequentially. Common operations for stacks and queues are described, including how queues can be implemented using two stacks. Applications of stacks like expression evaluation and backtracking are discussed. Memory is managed using a stack model, with activation records created on method calls.

Uploaded by

kiran
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
101 views8 pages

Introduction To Data Structures

The document provides an introduction to data structures, including linear and non-linear structures. Linear structures like linked lists store elements sequentially, while non-linear structures like trees have elements attached through relationships rather than sequentially. Common operations for stacks and queues are described, including how queues can be implemented using two stacks. Applications of stacks like expression evaluation and backtracking are discussed. Memory is managed using a stack model, with activation records created on method calls.

Uploaded by

kiran
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 8

Introduction to Data Structures

Define a linear and non linear data structure.

Linear data fashion- Linked list is an example of linear data storage or structure. Linked list
stores data in an organized a linear fashion. They store data in the form of a list.

Non Linear data structure- Tree data structure is an example of a non linear data structure. A
tree has one node called as root node that is the starting point that holds data and links to other
nodes.

Define a linear and non linear data structure.

Linear data structure: A linear data structure traverses the data elements sequentially, in which
only one data element can directly be reached. Ex: Arrays, Linked Lists

Non-Linear data structure: Every data item is attached to several other data items in a way that
is specific for reflecting relationships. The data items are not arranged in a sequential
structure. Ex: Trees, Graphs

Describe stack operation.

Stack is a data structure that follows Last in First out strategy.


StackOperations:-
Push – Pushes (inserts) the element in the stack. The location is specified by the pointer.
Pop – Pulls (removes) the element out of the stack. The location is specified by the pointer
Swap: - the two top most elements of the stack can be swapped
Peek: - Returns the top element on the stack but does not remove it from the stack
Rotate:- the topmost (n) items can be moved on the stack in a rotating fashion
A stack has a fixed location in the memory. When a data element is pushed in the stack, the
pointer points to the current element

Describe queue operation.

Queue is a data structure that follows First in First out strategy.


QueueOperations:
Push – Inserts the element in the queue at the end.
Pop – removes the element out of the queue from the front
Size – Returns the size of the queue
Front – Returns the first element of the queue. 
Empty – to find if the queue is empty.

Discuss how to implement queue using stack.

A queue can be implemented by using 2 stacks:-


1. An element is inserted in the queue by pushing it into stack 1
2. An element is extracted from the queue by popping it from the stack 2
3. If the stack 2 is empty then all elements currently in stack 1 are transferred to stack 2 but in the
reverse order
4. If the stack 2 is not empty just pop the value from stack 2.

Explain stacks and queues in detail.

A stack is a data structure based on the principle Last In First Out. Stack is container to hold
nodes and has two operations - push and pop. Push operation is to add nodes into the stack and
pop operation is to delete nodes from the stack and returns the top most node.

A queue is a data structure based on the principle First in First Out. The nodes are kept in an
order. A node is inserted from the rear of the queue and a node is deleted from the front. The first
element inserted in the first element first to delete.

Question - What are priority queues?

A priority queue is essentially a list of items in which each item has associated with it a priority
Items are inserted into a priority queue in any, arbitrary order. However, items are withdrawn
from a priority queue in order of their priorities starting with the highest priority item first.

Priority queues are often used in the implementation of algorithms

Question - What is a circular singly linked list?

In a circular singly linked list, the last node of the list is made to point to the first node. This
eases the traveling through the list

Stack applications:

Three applications of stacks are presented here.  These examples are central to many activities
that a computer must do and deserve time spent with them.
1. Expression evaluation
2. Backtracking (game playing, finding paths, exhaustive searching)
3. Memory management, run-time environment for nested language features.

Expression evaluation

In particular we will consider arithmetic expressions.  Understand that there are boolean and
logical expressions that can be evaluated in the same way.  Control structures can also be treated
similarly in a compiler.

This study of arithmetic expression evaluation is an example of problem solving where you solve
a simpler problem and then transform the actual problem to the simpler one.

Aside: The NP-Complete problem. There are a set of apparently intractable problems: finding the
shortest route in a graph (Traveling Salesman Problem), bin packing, linear programming, etc.
that are similar enough that if a polynomial solution is ever found (exponential solutions abound)
for one of these problems, then the solution can be applied to all problems.

Infix, Prefix and Postfix Notation

We are accustomed to write arithmetic expressions with the operation between the two
operands: a+b or c/d.  If we write a+b*c, however, we have to apply precedence rules to avoid
the ambiguous evaluation (add first or multiply first?). 

There's no real reason to put the operation between the variables or values.  They can just as well
precede or follow the operands.  You should note the advantage of prefix and postfix: the need
for precedence rules and parentheses are eliminated.

Infix Prefix Postfix


a+b +ab ab+
a+b*c +a*bc abc*+
(a + b) * (c - d) *+ab-cd ab+cd-*
b*b-4*a*c    
40 - 3 * 5 + 1    

Postfix expressions are easily evaluated with the aid of a stack.


Postfix Evaluation Algorithm

Assume we have a string of operands and operators, an informal, by hand process is

1. Scan the expression left to right


2. Skip  values or variables (operands)
3. When an operator is found, apply the operation to the preceding two operands
4. Replace the two operands and operator with the calculated value (three symbols are
replaced with one operand)
5. Continue scanning until only a value remains--the result of the expression

The time complexity is O(n) because each operand is scanned once, and each operation is
performed once.

A more formal algorithm:

create a new stack


while(input stream is not empty){
token = getNextToken();
if(token instanceof operand){
push(token);
} else if (token instance of operator)
op2 = pop();
op1 = pop();
result = calc(token, op1, op2);
push(result);
}
}
return pop();

Demonstration with 2 3 4 + * 5 -

Infix transformation to Postfix


This process uses a stack as well.  We have to hold information that's expressed inside
parentheses while scanning to find the closing ')'. We also have to hold information on operations
that are of lower precedence on the stack.  The algorithm is:

1. Create an empty stack and an empty postfix output string/stream


2. Scan the infix input string/stream left to right
3. If the current input token is an operand, simply append it to the output string (note the
examples above that the operands remain in the same order)
4. If the current input token is an operator, pop off all operators that have equal or higher
precedence and append them to the output string; push the operator onto the stack.  The
order of popping is the order in the output.
5. If the current input token is '(', push it onto the stack
6. If the current input token is ')', pop off all operators and append them to the output string
until a '(' is popped; discard the '('.
7. If the end of the input string is found, pop all operators and append them to the output
string.

This algorithm doesn't handle errors in the input, although careful analysis of parenthesis or lack
of parenthesis could point to such error determination.

Apply the algorithm to the above expressions.

Backtracking

Backtracking is used in algorithms in which there are steps along some path (state) from some
starting point to some goal. 

 Find your way through a maze. 


 Find a path from one point in a graph (roadmap) to another point. 
 Play a game in which there are moves to be made (checkers, chess). 

In all of these cases, there are choices to be made among a number of options.  We need some
way to remember these decision points in case we want/need to come back and try the alternative

Consider the maze.  At a point where a choice is made, we may discover that the choice leads to
a dead-end.  We want to retrace back to that decision point and then try the other (next)
alternative.
Again, stacks can be used as part of the solution.  Recursion is another, typically more favored,
solution, which is actually implemented by a stack.

Memory Management

Any modern computer environment uses a stack as the primary memory management model for
a running program.  Whether it's native code (x86, Sun, VAX) or JVM, a stack is at the center of
the run-time environment for Java, C++, Ada, FORTRAN, etc.

The discussion of JVM in the text is consistent with NT, Solaris, VMS, Unix runtime
environments.

Each program that is running in a computer system has its own memory allocation containing the
typical layout as shown below.
 

Call and return process

When a method/function is called

1. An activation record is created; its size depends on the number and size of the local
variables and parameters.
2. The Base Pointer value is saved in the special location reserved for it
3. The Program Counter value is saved in the Return Address location
4. The Base Pointer is now reset to the new base (top of the call stack prior to the creation
of the AR)
5. The Program Counter is set to the location of the first bytecode of the method being
called
6. Copies the calling parameters into the Parameter region
7. Initializes local variables in the local variable region

While the method executes, the local variables and parameters are simply found by adding a
constant associated with each variable/parameter to the Base Pointer.

When a method returns

1. Get the program counter from the activation record and replace what's in the PC
2. Get the base pointer value from the AR and replace what's in the BP
3. Pop the AR entirely from the stack.

Applications of Queue Data Structure


Queue is used when things don’t have to be processed immediatly, but have to be processed
in First In First Out order like Breadth First Search. This property of Queue makes it also useful
in following kind of scenarios.
1) When a resource is shared among multiple consumers. Examples include CPU scheduling,
Disk Scheduling.
2) When data is transferred asynchronously (data not necessarily received at same rate as sent)

You might also like