Chapter3 Stacks and Queues

Size: px
Start display at page:

Download "Chapter3 Stacks and Queues"

Transcription

1 Chapter Stacks and Queues Stacks Stacks Using Dynamic Arrays Queues Circular Queues Using Dynamic Arrays A Maze Problem Evaluation of Expressions Multiple Stacks and Queues C-C Tsai P. Stacks A stack is an ordered list in which insertions and deletions are made at one end called the top. A stack is also known as a First-In-Last-Out (FILO) or Last-In-First-Out (LIFO) list. A top B A top C B A top D C B A top E D C B A top D C B A top push(b) push(c) push(d) push(e) pop C-C Tsai P.

2 Stack of Cups A stack is a linear list, one end is called top and other end is called bottom. Additions to and removals from the top end only. Example: Add a cup to the stack. top E top F E D D C C bottom B A bottom B A C-C Tsai P. Application: Stack frame of function call A program places an activation record or a stack frame on top of the system stack when it invokes a function. fp: a pointer to current stack frame system stack before a is invoked system stack after a is invoked C-C Tsai P.4

3 Example of function call #include <stdio.h> main() { int x; x = fact(5); int fact(int n) { if (n>) return n*fact(n-); else return ; X =? invoke fact(5) invoke fact(4) invoke fact() invoke fact() invoke fact() return from fact() = return from fact() = return from fact() = 6 return from fact(4) = 4 return from fact(5) = X = C-C Tsai P.5 Abstract Data Type for Stack ADT Stack is objects: a finite ordered list with zero or more elements. functions: for all stack Stack, item element, max_stack_size positive integer Stack CreateS(max_stack_size) ::= create an empty stack whose maximum size is max_stack_size Boolean IsFull(stack, max_stack_size) ::= if (number of elements in stack == max_stack_size) return TRUE else return FALSE Stack Push(stack, item) ::= if (IsFull(stack)) stackfull else insert item into top of stack and return Boolean IsEmpty(stack) ::= if(stack == CreateS(max_stack_size)) return TRUE else return FALSE Element Pop(stack) ::= if(isempty(stack)) return else remove and return the item on the top of the stack. C-C Tsai P.6

4 Stacks Using Dynamic Arrays Stack using static array Stack CreateS() ::= #define MAX_STACK_SIZE typedef struct { int key; /* other fields */ element; element stack[max_stack_size]; int top = -; Boolean IsEmpty(Stack) ::= top < ; Boolean IsFull(Stack) ::= top >= MAX_STACK_SIZE -; C-C Tsai P.7 Implementation of Push and Pop void Push(element item) { /* add an item to the global stack */ if (top >= MAX_STACK_SIZE-) stackfull( ); stack[++top] = item; a b c d e element Pop() 4 { /* delete and return the top element from the stack */ if (top == -) return stack_empty( ); /* returns an error key */ return stack[top--]; void StackFull() /* for Static Array */ { fprintf(stderr, Stack is full, cannot add element. ); exit(exit_failure); top C-C Tsai P.8 4

5 Stack Using Dynamic Array Stack CreateS() ::= typedef struct { int key; /* other fields */ element; element *stack; MALLOC(stack, sizeof(*stack)); int capacity = ; int top = -; Boolean IsEmpty(Stack) ::= top< ; Boolean IsFull(Stack) ::= top >= capacity-; C-C Tsai P.9 Array Doubling for StackFull() Dynamic Array Doubling : When stack is full, double the capacity using REALLOC void StackFull() { REALLOC(stack, *capacity*sizeof(*stack); capacity *= ; Let final value of capacity be k, k > Number of pushes is at least k- + Total time spent on array doubling is Σ <=i=k i, This is O( k ) So, although the time for an individual push is O(capacity), the time for all n pushes remains O(n). C-C Tsai P. 5

6 Queues A queue is an ordered list in which all insertions take place at one end called rear and all deletions take place at the opposite end called front. A queue is also known as a First-In-First-Out (FIFO) or Last-In- Last-Out (LILO) list. Example: Bus stop queue: Bus Stop front rear rear C-C Tsai P. Queues A queue is an ordered list in which all insertions take place at one end called rear and all deletions take place at the opposite end called front. A queue is also known as a First-In-First-Out (FIFO) or Last-In- Last-Out (LILO) list. Example: Bus stop queue: Bus Stop rear front rear C-C Tsai P. 6

7 Queues A queue is an ordered list in which all insertions take place at one end called rear and all deletions take place at the opposite end called front. A queue is also known as a First-In-First-Out (FIFO) or Last-In- Last-Out (LILO) list. Example: Bus stop queue: Bus Stop front rear C-C Tsai P. Queues A queue is an ordered list in which all insertions take place at one end called rear and all deletions take place at the opposite end called front. A queue is also known as a First-In-First-Out (FIFO) or Last-In- Last-Out (LILO) list. Bus stop queue: Bus Stop front rear C-C Tsai P.4 7

8 Representation of a Queue A queue may be implemented with a linear array and two pointers: front and rear. Initially, front = rear = -. Example: A rear front B A Add(B) rear front C B A Add(C) rear front D C B A Add(D) rear front D C B Delete rear front C-C Tsai P.5 Application: Job Scheduling front rear Q[] Q[] Q[] Q[] Comments J J J J J J J J J queue is empty Job is added Job is added Job is added Job is deleted Job is deleted C-C Tsai P.6 8

9 Abstract Data Type of Queue ADT Queue is objects: a finite ordered list with zero or more elements. functions: for all queue Queue, item element, max_ queue_ size positive integer Queue CreateQ(maxQueueSize) ::= create an empty queue whose maximum size is max_queue_size Boolean IsFullQ(queue, maxqueuesize) ::= if(number of elements in queue == max_queue_size) return TRUE else return FALSE Queue AddQ(queue, item) ::= if (IsFullQ(queue)) queuefull else insert item at rear of queue and return queue Boolean IsEmptyQ(queue) ::= if (queue ==CreateQ(maxQueueSize)) return TRUE else return FALSE Element DeleteQ(queue) ::= if (IsEmptyQ(queue)) return C-C Tsai else remove and return the item at front of queue. P.7 Queues Using Dynamic Arrays Queue using static array Queue CreateQ(maxQueueSize) ::= # define MAX_QUEUE_SIZE typedef struct { int key; /* other fields */ element; element queue[max_queue_size]; int rear = -; int front = -; Boolean IsEmptyQ(queue) ::= front == rear Boolean IsFullQ(queue) ::= rear == MAX_QUEUE_SIZE- C-C Tsai P.8 9

10 Implementation of AddQ and DeleteQ void AddQ(element item) { /* add an item to the queue */ if (rear == MAX_QUEUE_SIZE-) queuefull(); queue [++rear] = item; element DeleteQ() { /* remove element at the front of the queue */ if (front == rear) return queueempty( ); /* return an error key */ return queue [++front]; C-C Tsai P.9 Circular Array for Queues Problem: Two pointers only increments, never decrements. We eventually fall off the right end of the array. This problem can be solved by periodically moving the elements to the left, to make room on the right end. We could use a circular array plus two pointers to implement a queue. The front index always points one position counterclockwise from the first element in the queue. The rear index points to the current end of the queue. [] [] [] [] J J [] [4] [] J [4] [] [5] [] [5] front = front = rear = rear = C-C Tsai P.

11 Circular Array for Queues (Cont d) Though there are MAX_Queue_SIZE slots in the circular array, we can store at most MAX_Queue_SIZE - elements in the circular array at any instant. Examples of full queues: [] [] [] [] J J J8 J9 [] J J4 [4][] J7 [4] J5 J6 J5 [] [5] [] [5] front = rear = 5 front =4 rear = C-C Tsai P. Implementation of Circular Array void AddQ(element item) { /* add an item to the queue */ rear = (rear +) % MAX_QUEUE_SIZE; if (front == rear) queuefull(); /* print error and exit */ queue[rear] = item; element DeleteQ() { /* remove front element from the queue and put it in item */ element item; if (front == rear) return queueempty( ); /* return an error key */ front = (front+) % MAX_QUEUE_SIZE; return queue[front]; C-C Tsai P.

12 Circular Queues Using Dynamic Allocated Arrays Problem: Add an element to a full queue, we must first increase the size of this array using a function such as REALLOC. As with dynamically allocated stacks, we use array doubling. Example: Full queue: front rear Doubling queue: front rear C-C Tsai P. Implementation of Doubling Circular Array void AddQ(element item) { /* add an item to the queue */ rear = (rear +) % capacity; if (front == rear) queuefull(); /* double capacity */ queue[rear] = item; C-C Tsai P.4

13 A Mazing Problem How to solve the maze problem? In the real world, touch the wall. In the computer, try all paths systematically. entrance : blocked path : through path exit C-C Tsai P.5 Allowable Moves in Maze There are eight possible moves for the current position. NW [row-][col-] N [row-][col] NE [row-][col+] W [row][col-] SW [row+][col-] [row][col] S [row+][col] E [row][col+] SE [row+][col+] Name Dir move[dir].vert move[dir].horiz N NE E SE S SW W NW C-C Tsai P.6

14 Representation of a Maze A possible implementation typedef struct { next_row = row + move[dir].vert; short int vert; next_col = col + move[dir].horiz; short int horiz; offsets; /*array of moves for each direction */ offsets move[8]; Alternative implementation using stack to keep pass history #define MAX_STACK_SIZE /*maximum stack size*/ typedef struct { short int row; short int col; short int dir; element; element stack[max_stack_size]; C-C Tsai P.7 Implementation: Use Stack Initialize a stack to the maze s entrance coordinates and direction to north; while (stack is not empty) { /* move to position at top of stack */ <row, col, dir> = delete from top of stack; while (there are more moves from current position) { <next_row, next_col > = coordinates of next move; dir = direction of move; if ((next_row == EXIT_ROW)&& (next_col == EXIT_COL)) success; if (maze[next_row][next_col] == && mark[next_row][next_col] == ) { /* legal move and haven t been there */ mark[next_row][next_col] = ; /* save current position and direction */ add <row, col, dir> to the top of the stack; row = next_row; col = next_col; dir = north; O(mp) where m and p are the number printf( No path found\n ); of rows and columns C-C Tsai P.8 4

15 Initial State A Mazing Example Mark (,) 4 (,,) Second State Mark (,) (,) 4 (,,) (,,) C-C Tsai P.9 A Mazing Example (Cont d) Third State 4 (,,) (,,) (,,) Mark (,) (,) (,) Forth State 4 (,,5) (,,) (,,) Mark (,) (,) (,) (,) C-C Tsai P. 5

16 A Mazing Example (Cont d) Fifth State 4 (4,,) (,,5) (,,) (,,) Mark (,) (,) (,) (,) (4,) Sixth State 4 (4,,) (4,,) (,,5) (,,) (,,) Mark (,) (,) (,) (,) (4,) (4,) C-C Tsai P. Analysis of path There are 8 directions of movements. We assume there is an outer border. So an m * n maze is represented as an (m + ) * (n + ) binary array. m*p The size of the maze determines the computing time of path. Since each position within the maze is visited no more than once, the worst case complexity of the algorithm is O(mp). C-C Tsai P. 6

17 Maze Search Function void path (void) /* output a path through the maze if such a path exists */ { int i, row, col, next_row, next_col, dir, found = FALSE; element position; mark[][] = ; top =; stack[].row = ; stack[].col = ; stack[].dir = ; while (top > - &&!found) { position = delete(&top); row = position.row; col = position.col; dir = position.dir; while (dir < 8 &&!found) { /*move in direction dir */ next_row = row + move[dir].vert; next_col = col + move[dir].horiz; if (next_row==exit_row && next_col==exit_col) found = TRUE; else if (!maze[next_row][next_col] &&!mark[next_row][next_col] { mark[next_row][next_col] = ; position.row = row; position.col = col; position.dir = ++dir; add(&top, position); row = next_row; col = next_col; dir = ; else ++dir; (,) (m,p) (m+)*(p+) 7 N 6 W E 5 S 4 C-C Tsai P. Maze Search Function (Cont d) if (found) { printf( The path is :\n ); printf( row col\n ); for (i = ; i <= top; i++) printf( %d%5d, stack[i].row, stack[i].col); printf( %d%5d\n, row, col); printf( %d%5d\n, EXIT_ROW, EXIT_COL); else printf( The maze does not have a path\n ); C-C Tsai P.4 7

18 Lee s Routing start pin end pin Label all reachable squares unit from start. C-C Tsai P.5 Lee s Wire Router start pin end pin Label all reachable unlabeled squares units from start. C-C Tsai P.6 8

19 Lee s Wire Router start pin end pin Label all reachable unlabeled squares units from start. C-C Tsai P.7 Lee s Wire Router start pin end pin Label all reachable unlabeled squares 4 units from start. C-C Tsai P.8 9

20 Lee s Wire Router start pin end pin Label all reachable unlabeled squares 5 units from start. C-C Tsai P.9 Lee s Wire Router start pin end pin Label all reachable unlabeled squares 6 units from start. C-C Tsai P.4

21 Lee s Wire Router start pin end pin End pin reached. Traceback. C-C Tsai P.4 Lee s Wire Router start pin end pin End pin reached. Traceback. C-C Tsai P.4

22 Evaluation of Expressions X = a / b - c + d * e - a * c Assume that a = 4, b = c =, d = e = Interpretation : ((4/)-)+(*)-(4*)= + 9-8= Interpretation : (4/(-+))*(-4)*=(4/)*(-)*= How to generate the machine instructions corresponding to a given expression? precedence rule + associative rule C-C Tsai P.4 Precedence Hierarchy In any programming language, a precedence hierarchy determines the order in which we evaluate operators. Operators with highest precedence are evaluated first. With right associative operators of the same precedence, we evaluate the operator furthest to the right first. Expressions are always evaluated from the innermost parenthesized expression first. C-C Tsai P.44

23 Precedence Hierarchy for C Token Operator Precedence Associativity ( ) [ ] ->. function call array element struct or union member 7 left-to-right increment, decrement 6 left-to-right -- ++! & * sizeof decrement, increment logical not one s complement unary minus or plus address or indirection size (in bytes) 5 right-to-left (type) type cast 4 right-to-left * / % mutiplicative Left-to-right.The precedence column is taken from Harbison and Steele..Postfix form C-C Tsai.Prefix form P binary add or subtract left-to-right << >> shift left-to-right > >= relational left-to-right < <= ==!= equality 9 left-to-right & bitwise and 8 left-to-right ^ bitwise exclusive or 7 left-to-right bitwise or 6 left-to-right && logical and 5 left-to-right logical or 4 left-to-right?: conditional right-to-left = += -= assignment right-to-left /= *= %= <<= >>= &= ^= =, comma left-to-right C-C Tsai P.46

24 Infix and Postfix Notation Infix Postfix +*4 4*+ a*b+5 ab*5+ (+)*7 +7* a*b/c ab*c/ (a/(b- c+d))*(e-a)*c abc-d+/ ea- *c* a/b- c+d*e- a*c ab/c- de*+ac*- Postfix: no parentheses, no precedence Two questions:. How to evaluate an expression in postfix notation?. How to change infix notation into postfix notation? C-C Tsai P.47 Evaluating Postfix Expressions Evaluation process Make a single left-to-right scan of the expression. Place the operands on a stack until an operator is found. Remove, from the stack, the correct numbers of operands for the operator, perform the operation, and place the result back on the stack. Example: 6 / 4 * + Token Stack Top [] [] [] / 6/ 6/ - 4 6/- 6/- 4 6/- 4 * 6/- 4* + 6/-+4* C-C Tsai P.48 4

25 Function to Evaluate a Postfix Expression Assumptions: operators: +, -, *, /, % operands: single digit integer #define MAX_STACK_SIZE /* maximum stack size */ #define MAX_EXPR_SIZE /* max size of expression */ typedef enum{lparen, rparen, plus, minus, times, divide, mod, eos, operand precedence; int stack[max_stack_size]; /* global stack */ char expr[max_expr_size]; /* input string */ int eval(void) { /* evaluate a postfix expression, expr, maintained as a global variable, \ is the the end of the expression. The stack and top of the stack are global variables. get_token is used to return the token type and the character symbol. Operands are assumed to be single character digits */ precedence token; char symbol; int op, op; int n = ; /* counter for the expression string */ int top = -; token = get_token(&symbol, &n); C-C Tsai P.49 Function to Evaluate a Postfix Expression (Cont d) while (token!= eos) { if (token == operand) add(&top, symbol- ); /* stack insert */ else { /* remove two operands, perform operation, and return result to the stack */ op = delete(&top); /* stack delete */ op = delete(&top); switch(token) { case plus: add(&top, op+op); break; case minus: add(&top, op-op); break; case times: add(&top, op*op); break; case divide: add(&top, op/op); break; case mod: add(&top, op%op); token = get_token (&symbol, &n); return delete(&top); /* return result */ C-C Tsai P.5 5

26 Getting Token precedence get_token(char *symbol, int *n) { /* get the next token, symbol is the character representation, which is returned, the token is represented by its enumerated value, which is returned in the function name */ *symbol =expr[(*n)++]; switch (*symbol) { case ( : return lparen; case ) : return rparen; case + : return plus; case - : return minus; case / : return divide; case * : return times; case % : return mod; case \ : return eos; default : return operand; /* no error checking, default is operand */ C-C Tsai P.5 Infix to Postfix Two-pass algorithm: Fully parenthesize expression a / b -c + d * e -a* c --> ((((a / b) - c) + (d * e)) - a * c)) All operators replace their corresponding right parentheses. ((((a / b) - c) + (d * e)) - a * c)) Delete all parentheses. ab/c-de*+ac*- C-C Tsai P.5 6

27 Example: Infix to Postfix The orders of operands in infix and postfix are the same. a + b * c, * > + Token a + b * c eos Stack [] [] [] * + * Top Output C-C Tsai P a a ab ab abc abc*+ Example: Infix to Postfix Token a * ( b + c ) * d eos a * (b +c) * d Stack [] [] [] * * ( * ( * ( + * ( + * * match ) * = * * * Top - Output a a a ab ab abc abc+ abc+* abc+* d abc+* d* C-C Tsai P.54 7

28 Rules () Operators are taken out of the stack as long as their in-stack precedence is higher than or equal to the incoming precedence of the new operator. () ( has low in-stack precedence (isp), and high incoming precedence (icp). ( ) + - * / % eos isp 9 icp 9 C-C Tsai P.55 Precedence-based postfix() The left parenthesis is placed in the stack whenever it is found in the expression, but it is unstacked only when its matching right parenthesis is found. We remove an operator from the stack only if its isp is greater than or equal to the icp of the new operator. precedence stack[max_stack_size]; /* isp and icp arrays -- index is value of precedence lparen, rparen, plus, minus, times, divide, mod, eos */ static int isp [ ] = {, 9,,,,,, ; static int icp [ ] = {, 9,,,,,, ; void postfix(void) { /* output the postfix of the expression. The expression string, the stack, and top are global */ char symbol; precedence token; int n = ; int top = ; /* place eos on stack */ stack[] = eos; C-C Tsai P.56 8

29 Precedence-based postfix() for (token = get _token(&symbol, &n); token!= eos; token = get_token(&symbol, &n)) { if (token == operand) printf ( %c, symbol); else if (token == rparen ){ /*unstack tokens until left parenthesis */ while (stack[top]!= lparen) print_token(delete(&top)); delete(&top); /*discard the left parenthesis */ else{ /* remove and print symbols whose isp is greater than or equal to the current token s icp */ while(isp[stack[top]] >= icp[token] ) print_token(delete(&top)); add(&top, token); while ((token = delete(&top))!= eos) print_token(token); print( \n ); f(n)=θ(g(n)) iff there exist positive constants c, c, and n such that c g(n) f(n) c g(n) for all n, n n. f(n)=θ(g(n)) iff g(n) is both an upper and lower bound on f(n). C-C Tsai P.57 Analysis of postfix() Let n be the number of tokens in the expression. θ(n) time is spent extracting tokens and outputting them. Time is spent in the two while loops. The total time spent here is θ(n) as the number of tokens that get stacked and unstacked is linear in n. So, the complexity of function postfix() is θ(n). C-C Tsai P.58 9

30 Infix and Prefix Notation () evaluation () transformation Infix a*b/c a/b-c+d*e-a*c a*(b+c)/d-g Prefix /*abc -+-/abc*de*ac -/*a+bcdg C-C Tsai P.59 Multiple Stacks and Queues When we only need two stacks, we can put the two stacks on the two ends of an array. When we need more than two stacks, we can: Use a fixed partition for each stack. Use a variable partition for each stack. When stack is full, then Find a larger, free space. Move the related stacks around. C-C Tsai P.6

31 Implementation of Multiple Stacks Initially, boundary[i]=top[i]. [ m/n ] [ m/n ] m- boundary[ ] boundary[] boundary[ ] boundary[n] top[ ] top[ ] top[ ] All stacks are empty and divided into roughly equal segments. C-C Tsai P.6 Implementation of Multiple Stacks #define MEMORY_SIZE /* size of memory */ #define MAX_STACKS /* max number of stacks plus */ element memory[memory_size]; int top[max_stacks]; int boundary[max_stacks]; int n; /* number of stacks entered by the user */ top[] = boundary[] = -; for (i = ; i < n; i++) void push(int i, element item) top[i] =boundary[i] =(MEMORY_SIZE/n)*i; { /* add an item to the ith stack */ boundary[n] = MEMORY_SIZE-; if (top[i] == boundary [i+]) stackfull(i); memory[++top[i]] = item; element pop(int i) { /* remove top element from the ith stack */ if (top[i] == boundary[i]) return stackempty(i); return memory[top[i]--]; C-C Tsai P.6

32 When Stack is Full If there is space available (in memory), it should shift the stacks so that space is allocated to the full stack. Determine the least, j, stack i < j < n, such that there is free space between stacks j and j+. If there is such a j, then move stacks stack i +, stack i +,, j one position to the right. This creates a space between stack i and i+. If there is no such j, then look to the left of stack stack i. Find the largest j such that j < stack i and there is space between stacks j and j+. If there is such a j, then move stacks j+, j+,, stack i one position to the left. In the worst case, the function, stackfull, has a time complexity of O(MEMORY_SIZE). C-C Tsai P.6 When Stack is Full (Cont d) Find j, stack_no < j < n such that top[j] < boundary[j+] or, j < stack_no b[] t[] b[] t[] b[i] t[i] t[i+] t[j] b[j+] b[n] b[i+] b[i+] b=boundary, t=top C-C Tsai P.64

STACKS AND QUEUES CHAPTER 3

STACKS AND QUEUES CHAPTER 3 CHAPTER 3 STACKS AND QUEUES All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed Fundamentals of Data Structures in C, CHAPTER 3 1 Stack: a Last-In-First-Out

More information

CHAPTER 3 STACKS AND QUEUES

CHAPTER 3 STACKS AND QUEUES CHAPTER 3 STACKS AND QUEUES All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed Fundamentals of Data Structures in C /2nd Edition, Silicon Press, 2008.

More information

Fall, 2015 Prof. Jungkeun Park

Fall, 2015 Prof. Jungkeun Park Data Structures t and Algorithms Stacks Application Infix to Postfix Conversion Fall, 2015 Prof. Jungkeun Park Copyright Notice: This material is modified version of the lecture slides by Prof. Rada Mihalcea

More information

Data Structure. Chapter 3 Stacks and Queues. Department of Communication Engineering National Central University Jhongli, Taiwan.

Data Structure. Chapter 3 Stacks and Queues. Department of Communication Engineering National Central University Jhongli, Taiwan. Data Structure Chapter 3 Stacks and Queues Instructor: Angela Chih-Wei Tang Department of Communication Engineering National Central University Jhongli, Taiwan 29 Spring Outline Stack Queue A Mazing Problem

More information

NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 3. Stacks And Queues

NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 3. Stacks And Queues CHAPTER 3 Stacks And Queues 1 Stack Stack: a Last-In-First-Out (LIFO/FILO) list Push Pop C A B C B A Top 2 An application of stack: stack frame of function call Old frame pointer fp Return address al Old

More information

Two abstract data types. performed on them) Stacks Last In First Out (LIFO) Queues First In First Out (FIFO)

Two abstract data types. performed on them) Stacks Last In First Out (LIFO) Queues First In First Out (FIFO) Two abstract data types o Can be implemented using arrays or linked lists o Restricted lists (only a small number of operations are performed on them) Stacks Last In First Out (LIFO) Queues First In First

More information

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi Data Structures & Algorithm Analysis Lec(3) Stacks Lecturer: Souad Alonazi What is a stack? Stores a set of elements in a particular order Stack principle: LAST IN FIRST OUT = LIFO It means: the last element

More information

Infix to Postfix Conversion

Infix to Postfix Conversion Infix to Postfix Conversion Infix to Postfix Conversion Stacks are widely used in the design and implementation of compilers. For example, they are used to convert arithmetic expressions from infix notation

More information

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES BBM 201 DATA STRUCTURES Lecture 6: EVALUATION of EXPRESSIONS 2018-2019 Fall Evaluation of Expressions Compilers use stacks for the arithmetic and logical expressions. Example: x=a/b-c+d*e-a*c If a=4, b=c=2,

More information

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES BBM 201 DATA STRUCTURES Lecture 6: EVALUATION of EXPRESSIONS 2017 Fall Evaluation of Expressions Compilers use stacks for the arithmetic and logical expressions. Example: x=a/b-c+d*e-a*c If a=4, b=c=2,

More information

Data Structure - Stack and Queue-

Data Structure - Stack and Queue- Data Structure - Stack and Queue- Hanyang University Jong-Il Park STACK Stack ADT List that insertions and deletions can be performed at the end of the list Operations Push(X, S): insert X in the list

More information

Chapter 3. Stack & Queue 1

Chapter 3. Stack & Queue 1 Chapter 3 Stacks & Queues Templates in C++ The Stack ADT The Queue ADT SubTypeing & Inheritance in C++ A Mazing Problem Evaluation of Expressions Multiple Stacks & Queues March 30, 2015 Stack & Queue 1

More information

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES BBM 201 DATA STRUCTURES Lecture 6: Stacks and Queues 2015-2016 Fall Stacks A list on which insertion and deletion can be performed. Based on Last-in-First-out (LIFO) Stacks are used for a number of applications:

More information

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms Introduction Problem Solving on Computer Data Structures (collection of data and relationships) Algorithms 1 Objective of Data Structures Two Goals: 1) Identify and develop useful high-level data types

More information

Stack. A list whose end points are pointed by top and bottom

Stack. A list whose end points are pointed by top and bottom 4. Stck Stck A list whose end points re pointed by top nd bottom Insertion nd deletion tke plce t the top (cf: Wht is the difference between Stck nd Arry?) Bottom is constnt, but top grows nd shrinks!

More information

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES BBM 201 DATA STRUCTURES Lecture 5: Stacks and Queues 2017 Fall Stacks A list on which insertion and deletion can be performed. Based on Last-in-First-out (LIFO) Stacks are used for a number of applications:

More information

Lecture Data Structure Stack

Lecture Data Structure Stack Lecture Data Structure Stack 1.A stack :-is an abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example a deck of cards

More information

Stacks and Queues. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

Stacks and Queues. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Stacks and Queues Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Two New ADTs Define two new abstract data types Both are restricted lists Can be implemented using arrays

More information

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1 Data Structure using C++ Lecture 04 Reading Material Data Structures and algorithm analysis in C++ Chapter. 3 3.1, 3.2, 3.2.1 Summary Stack Operations on a stack Representing stacks Converting an expression

More information

CHAPTER 3 STACKS AND QUEUES. Iris Hui-Ru Jiang Fall 2008

CHAPTER 3 STACKS AND QUEUES. Iris Hui-Ru Jiang Fall 2008 HAPTER 3 STAKS AND QUEUES Iris Hui-Ru Jiang Fall 2008 2 ontents Templates in ++ Stack (LIFO) Queue (FIFO) Subtyping and Inheritance in ++ A Mazing Problem Evaluation of Expressions Readings hapter 3 ++

More information

DEEPIKA KAMBOJ UNIT 2. What is Stack?

DEEPIKA KAMBOJ UNIT 2. What is Stack? What is Stack? UNIT 2 Stack is an important data structure which stores its elements in an ordered manner. You must have seen a pile of plates where one plate is placed on top of another. Now, when you

More information

Review of the C Programming Language for Principles of Operating Systems

Review of the C Programming Language for Principles of Operating Systems Review of the C Programming Language for Principles of Operating Systems Prof. James L. Frankel Harvard University Version of 7:26 PM 4-Sep-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights

More information

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9 Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Uppercase Alphabets Lowercase Alphabets Character Set A, B, C, Y, Z a, b, c, y, z Digits

More information

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5 What is Stack? Stack 1. Stack is LIFO Structure [ Last in First Out ] 2. Stack is Ordered List of Elements of Same Type. 3. Stack is Linear List 4. In Stack all Operations such as Insertion and Deletion

More information

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g. 1.3a Expressions Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a syntactical token that requires an action be taken An operand is an object

More information

Outline. Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications. ADT for stacks

Outline. Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications. ADT for stacks Stack Chapter 4 Outline Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications Recursive Programming Evaluation of Expressions ADT for stacks Introduction

More information

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques Stacks Manolis Koubarakis 1 Stacks and Queues Linear data structures are collections of components arranged in a straight line. If we restrict the growth of a linear data structure so that new components

More information

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows: STACKS A stack is a linear data structure for collection of items, with the restriction that items can be added one at a time and can only be removed in the reverse order in which they were added. The

More information

Operators. Java operators are classified into three categories:

Operators. Java operators are classified into three categories: Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

More information

Expressions and Precedence. Last updated 12/10/18

Expressions and Precedence. Last updated 12/10/18 Expressions and Precedence Last updated 12/10/18 Expression: Sequence of Operators and Operands that reduce to a single value Simple and Complex Expressions Subject to Precedence and Associativity Six

More information

March 13/2003 Jayakanth Srinivasan,

March 13/2003 Jayakanth Srinivasan, Statement Effort MergeSort(A, lower_bound, upper_bound) begin T(n) if (lower_bound < upper_bound) Θ(1) mid = (lower_bound + upper_bound)/ 2 Θ(1) MergeSort(A, lower_bound, mid) T(n/2) MergeSort(A, mid+1,

More information

Computer Science BS Degree - Data Structures (I2206) Abstract Data Types (ADT)

Computer Science BS Degree - Data Structures (I2206) Abstract Data Types (ADT) Abstract Data Types (ADT) 70 Hierarchy of types Each type is implemented in terms if the types lower in the hierarchy Level 0 Bit Byte Word Level 1 Integer Real Char Boolean Pointer Level 2 Array Record

More information

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305 Q.1 If h is any hashing function and is used to hash n keys in to a table of size m, where n

More information

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering USN 1 P E PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering SOLUTION FOR INTERNAL ASSESSMENT TEST 2 Date : 6/10/2017 Marks:

More information

Review of the C Programming Language

Review of the C Programming Language Review of the C Programming Language Prof. James L. Frankel Harvard University Version of 11:55 AM 22-Apr-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Reference Manual for the

More information

Introduction to Computer and Program Design 2. Lesson 6. Stacks. James C.C. Cheng Department of Computer Science National Chiao Tung University

Introduction to Computer and Program Design 2. Lesson 6. Stacks. James C.C. Cheng Department of Computer Science National Chiao Tung University Introduction to Computer and Program Design 2 Lesson 6 Stacks James C.C. Cheng Department of Computer Science National Chiao Tung University Introduction Stack A data collection, a grouping of data items

More information

Foundations of Data Structures

Foundations of Data Structures Foundations of Data Structures Lecture 4 Elementary Abstract Data Types (ADT): Stack Queue 1 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies:

More information

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

UNIT-II. Part-2: CENTRAL PROCESSING UNIT Page1 UNIT-II Part-2: CENTRAL PROCESSING UNIT Stack Organization Instruction Formats Addressing Modes Data Transfer And Manipulation Program Control Reduced Instruction Set Computer (RISC) Introduction:

More information

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Op. Use Description + x + y adds x and y x y

More information

The Arithmetic Operators

The Arithmetic Operators The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Examples: Op. Use Description + x + y adds x

More information

Department of Computer Science

Department of Computer Science Department of Computer Science Definition An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations and is usually used to manipulate data and

More information

STACKS AND QUEUES. Problem Solving with Computers-II

STACKS AND QUEUES. Problem Solving with Computers-II STACKS AND QUEUES Problem Solving with Computers-II 2 Stacks container class available in the C++ STL Container class that uses the Last In First Out (LIFO) principle Methods i. push() ii. iii. iv. pop()

More information

Linear Data Structure

Linear Data Structure Linear Data Structure Definition A data structure is said to be linear if its elements form a sequence or a linear list. Examples: Array Linked List Stacks Queues Operations on linear Data Structures Traversal

More information

Stacks. Ordered list with property: Insertions and deletions always occur at the same end. INSERT DELETE A3 A3 TOP TOP TOP

Stacks. Ordered list with property: Insertions and deletions always occur at the same end. INSERT DELETE A3 A3 TOP TOP TOP Stacks Ordered list with property: Insertions and deletions always occur at the same end. INSERT A3 A3 TOP DELETE A2 TOP A2 A2 TOP A1 A1 A1 A0 A0 A0 Stacks Implementation Implementation with arrays: Declare

More information

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues The Bucharest University of Economic Studies Data Structures ADTs-Abstract Data Types Stacks and Queues Agenda Definition Graphical representation Internal interpretation Characteristics Operations Implementations

More information

Informatics Ingeniería en Electrónica y Automática Industrial

Informatics Ingeniería en Electrónica y Automática Industrial Informatics Ingeniería en Electrónica y Automática Industrial Operators and expressions in C Operators and expressions in C Numerical expressions and operators Arithmetical operators Relational and logical

More information

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved Stacks Chapter 5 Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions A Problem Solved: Checking for Balanced Delimiters in an Infix Algebraic Expression A Problem Solved:

More information

Lecture No.04. Data Structures

Lecture No.04. Data Structures Lecture No.04 Data Structures Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i

More information

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Unit-2 (Operators) ANAND KR.SRIVASTAVA Unit-2 (Operators) ANAND KR.SRIVASTAVA 1 Operators in C ( use of operators in C ) Operators are the symbol, to perform some operation ( calculation, manipulation). Set of Operations are used in completion

More information

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1 Some Applications of Stack Spring Semester 2007 Programming and Data Structure 1 Arithmetic Expressions Polish Notation Spring Semester 2007 Programming and Data Structure 2 What is Polish Notation? Conventionally,

More information

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one. http://www.tutorialspoint.com/go/go_operators.htm GO - OPERATORS Copyright tutorialspoint.com An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.

More information

A flow chart is a graphical or symbolic representation of a process.

A flow chart is a graphical or symbolic representation of a process. Q1. Define Algorithm with example? Answer:- A sequential solution of any program that written in human language, called algorithm. Algorithm is first step of the solution process, after the analysis of

More information

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are: LESSON 1 FUNDAMENTALS OF C The purpose of this lesson is to explain the fundamental elements of the C programming language. C like other languages has all alphabet and rules for putting together words

More information

Top of the Stack. Stack ADT

Top of the Stack. Stack ADT Module 3: Stack ADT Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu Stack ADT Features (Logical View) A List that

More information

Arithmetic Operators. Portability: Printing Numbers

Arithmetic Operators. Portability: Printing Numbers Arithmetic Operators Normal binary arithmetic operators: + - * / Modulus or remainder operator: % x%y is the remainder when x is divided by y well defined only when x > 0 and y > 0 Unary operators: - +

More information

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0) CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0) Class Teacher: Pralay Mitra Department of Computer Science and Engineering Indian Institute of Technology Kharagpur Conceptual Idea

More information

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands Performing Computations C provides operators that can be applied to calculate expressions: tax is 8.5% of the total sale expression: tax = 0.085 * totalsale Need to specify what operations are legal, how

More information

Linked List. April 2, 2007 Programming and Data Structure 1

Linked List. April 2, 2007 Programming and Data Structure 1 Linked List April 2, 2007 Programming and Data Structure 1 Introduction head A linked list is a data structure which can change during execution. Successive elements are connected by pointers. Last element

More information

STACKS 3.1 INTRODUCTION 3.2 DEFINITION

STACKS 3.1 INTRODUCTION 3.2 DEFINITION STACKS 3 3.1 INTRODUCTION A stack is a linear data structure. It is very useful in many applications of computer science. It is a list in which all insertions and deletions are made at one end, called

More information

JAVA OPERATORS GENERAL

JAVA OPERATORS GENERAL JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

Data Types and Variables in C language

Data Types and Variables in C language Data Types and Variables in C language Disclaimer The slides are prepared from various sources. The purpose of the slides is for academic use only Operators in C C supports a rich set of operators. Operators

More information

Operators & Expressions

Operators & Expressions Operators & Expressions Operator An operator is a symbol used to indicate a specific operation on variables in a program. Example : symbol + is an add operator that adds two data items called operands.

More information

Formal Languages and Automata Theory, SS Project (due Week 14)

Formal Languages and Automata Theory, SS Project (due Week 14) Formal Languages and Automata Theory, SS 2018. Project (due Week 14) 1 Preliminaries The objective is to implement an algorithm for the evaluation of an arithmetic expression. As input, we have a string

More information

Discussion of project # 1 permutations

Discussion of project # 1 permutations Discussion of project # 1 permutations Algorithm and examples. The main function for generating permutations is buildp. We don t need a separate class for permutations since the permutations objects are

More information

Constants and Variables

Constants and Variables DATA STORAGE Constants and Variables In many introductory courses you will come across characteristics or elements such as rates, outputs, income, etc., measured by numerical values. Some of these will

More information

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3 Linear and Non-Linear Data Structures Linear data structure: Linear data structures are those data structure in which data items are arranged in a linear sequence by physically or logically or both the

More information

Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU Data Structures Chapter 06 1 Stacks A stack is a list of elements in which an element may be inserted or deleted only at one end, called the top of the stack. This means that elements are removed from

More information

Data Structures Week #3. Stacks

Data Structures Week #3. Stacks Data Structures Week #3 Stacks Outline Stacks Operations on Stacks Array Implementation of Stacks Linked List Implementation of Stacks Stack Applications October 5, 2015 Borahan Tümer, Ph.D. 2 Stacks (Yığınlar)

More information

Postfix (and prefix) notation

Postfix (and prefix) notation Postfix (and prefix) notation Also called reverse Polish reversed form of notation devised by mathematician named Jan Łukasiewicz (so really lü-kä-sha-vech notation) Infix notation is: operand operator

More information

12 Abstract Data Types

12 Abstract Data Types 12 Abstract Data Types 12.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define the concept of an abstract data type (ADT). Define

More information

Stacks and Queues. Introduction to Data Structures Kyuseok Shim SoEECS, SNU.

Stacks and Queues. Introduction to Data Structures Kyuseok Shim SoEECS, SNU. Stacks and Queues Introduction to Data Structures Kyuseok Shim SoEECS, SNU. 1 3.1 Templates in C++ Make classes and functions more reusable Without using templates (example) program 1.6 : Selection sort

More information

Information Science 1

Information Science 1 Information Science 1 Simple Calcula,ons Week 09 College of Information Science and Engineering Ritsumeikan University Topics covered l Terms and concepts from Week 8 l Simple calculations Documenting

More information

Types of Data Structures

Types of Data Structures DATA STRUCTURES material prepared by: MUKESH BOHRA Follow me on FB : http://www.facebook.com/mukesh.sirji4u The logical or mathematical model of data is called a data structure. In other words, a data

More information

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

CprE 288 Introduction to Embedded Systems Exam 1 Review.  1 CprE 288 Introduction to Embedded Systems Exam 1 Review http://class.ece.iastate.edu/cpre288 1 Overview of Today s Lecture Announcements Exam 1 Review http://class.ece.iastate.edu/cpre288 2 Announcements

More information

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the elements may locate at far positions

More information

Lecture 02 C FUNDAMENTALS

Lecture 02 C FUNDAMENTALS Lecture 02 C FUNDAMENTALS 1 Keywords C Fundamentals auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void

More information

MODULE 3: LINKED LIST

MODULE 3: LINKED LIST MODULE 3: LINKED LIST DEFINITION A linked list, or one-way list, is a linear collection of data elements, called nodes, where the linear order is given by means of pointers. That is, each node is divided

More information

CS6202 - PROGRAMMING & DATA STRUCTURES I Unit IV Part - A 1. Define Stack. A stack is an ordered list in which all insertions and deletions are made at one end, called the top. It is an abstract data type

More information

Stacks and Queues. CSE Data Structures April 12, 2002

Stacks and Queues. CSE Data Structures April 12, 2002 Stacks and Queues CSE 373 - Data Structures April 12, 2002 Readings and References Reading Section 3.3 and 3.4, Data Structures and Algorithm Analysis in C, Weiss Other References 12-Apr-02 CSE 373 - Data

More information

Top of the Stack. Stack ADT

Top of the Stack. Stack ADT Module 3: Stack ADT Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu Stack ADT Features (Logical View) A List that

More information

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression Expressions and Statementst t Expression C Programming Lecture 6 : Operators Combination of constants,variables,operators, operators and function calls a+b 3.0*x 9.66553 tan(angle) Statement An expression

More information

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK Contents: Definition and Examples Representing stacks in C Example: infix, prefix, and postfix Exercises THE STACK Definition and Examples A stack is an ordered collection of items into which new items

More information

Stacks. CONTENTS 3.1 Introduction 1. Stack as an abstract data type 2. Representation of a Stack as an array 3.2Applications of Stack.

Stacks. CONTENTS 3.1 Introduction 1. Stack as an abstract data type 2. Representation of a Stack as an array 3.2Applications of Stack. Stacks CONTENTS 3.1 Introduction 1. Stack as an abstract data type 2. Representation of a Stack as an array 3.2Applications of Stack Hours: 12 Marks: 18 3.1 Introduction to Stacks 1. Stack as Abstract

More information

Programming and Data Structure Solved. MCQs- Part 2

Programming and Data Structure Solved. MCQs- Part 2 Programming and Data Structure Solved MCQs- Part 2 Programming and Data Structure Solved MCQs- Part 2 Unsigned integers occupies Two bytes Four bytes One byte Eight byte A weather forecasting computation

More information

UNIT 3 OPERATORS. [Marks- 12]

UNIT 3 OPERATORS. [Marks- 12] 1 UNIT 3 OPERATORS [Marks- 12] SYLLABUS 2 INTRODUCTION C supports a rich set of operators such as +, -, *,,

More information

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out Stack and Queue 1 Stack Data structure with Last-In First-Out (LIFO) behavior In Out C B A B C 2 Typical Operations Pop on Stack Push isempty: determines if the stack has no elements isfull: determines

More information

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators JAVA Standard Edition Java - Basic Operators Java provides a rich set of operators to manipulate variables.

More information

Operators and Expressions:

Operators and Expressions: Operators and Expressions: Operators and expression using numeric and relational operators, mixed operands, type conversion, logical operators, bit operations, assignment operator, operator precedence

More information

Operators in C. Staff Incharge: S.Sasirekha

Operators in C. Staff Incharge: S.Sasirekha Operators in C Staff Incharge: S.Sasirekha Operators An operator is a symbol which helps the user to command the computer to do a certain mathematical or logical manipulations. Operators are used in C

More information

Computers Programming Course 6. Iulian Năstac

Computers Programming Course 6. Iulian Năstac Computers Programming Course 6 Iulian Năstac Recap from previous course Data types four basic arithmetic type specifiers: char int float double void optional specifiers: signed, unsigned short long 2 Recap

More information

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1 Data Structure using C++ Lecture 04 Reading Material Data Structures and algorithm analysis in C++ Chapter. 3 3.1, 3.2, 3.2.1 Summary Infix to Postfix Example 1: Infix to Postfix Example 2: Postfix Evaluation

More information

Chapter 9 STACK, QUEUE

Chapter 9 STACK, QUEUE Chapter 9 STACK, QUEUE 1 LIFO: Last In, First Out. Stacks Restricted form of list: Insert and remove only at front of list. Notation: Insert: PUSH Remove: POP The accessible element is called TOP. Stack

More information

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE TED (10)-3071 Reg. No.. (REVISION-2010) Signature. FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours (Maximum marks: 100)

More information

Principles of Programming Languages COMP251: Syntax and Grammars

Principles of Programming Languages COMP251: Syntax and Grammars Principles of Programming Languages COMP251: Syntax and Grammars Prof. Dekai Wu Department of Computer Science and Engineering The Hong Kong University of Science and Technology Hong Kong, China Fall 2007

More information

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently. DATA STRUCUTRES A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently. An algorithm, which is a finite sequence of instructions, each of which

More information

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours TED (10)-3071 Reg. No.. (REVISION-2010) (Maximum marks: 100) Signature. FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours PART

More information

Stacks, Queues (cont d)

Stacks, Queues (cont d) Stacks, Queues (cont d) CSE 2011 Winter 2007 February 1, 2007 1 The Adapter Pattern Using methods of one class to implement methods of another class Example: using List to implement Stack and Queue 2 1

More information

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017 Stack Applications Lecture 27 Sections 18.7-18.8 Robb T. Koether Hampden-Sydney College Wed, Mar 29, 2017 Robb T. Koether Hampden-Sydney College) Stack Applications Wed, Mar 29, 2017 1 / 27 1 Function

More information

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Stacks, Queues and Hierarchical Collections. 2501ICT Logan Stacks, Queues and Hierarchical Collections 2501ICT Logan Contents Linked Data Structures Revisited Stacks Queues Trees Binary Trees Generic Trees Implementations 2 Queues and Stacks Queues and Stacks

More information

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS 1. Define global declaration? The variables that are used in more

More information