UNIT - I LISTS, STACKS AND QUEUES

Size: px
Start display at page:

Download "UNIT - I LISTS, STACKS AND QUEUES"

Transcription

1 UNIT - I LISTS, STACKS AND QUEUES Abstract data types- List ADT-Stack ADT-recursion-Queue ADT A data structure is an arrangement of data in a computer's memory or even disk storage. An example of several common data structures are arrays, linked lists, queues, stacks, binary trees, and hash tables.

2 1.1 Abstract data types (ADTs) One of the basic rule concerning with programming is that no routine should ever exceed a page.this is accomplished by breaking down into modules. Each module is a logical unit and does a specific job. Its size is kept small by calling other modules. Modularity has several advantages. It is much easier to debug small routines than large routines. It is easier for several people to work on a modular program simultaneously. A well written modular program places certain dependencies in only one routine, making changes easier. An abstract data type (ADT) is a set of operations. Abstract data types are mathematical operations. Abstract data types can be viewed as an extension of modular design. Objects such as lists, sets, graphs along with their operations, can be viewed as abstract data types, just as integers,reals, and Boolean data types. Just as integers, reals, and Boolean data types have operations associated with them. The basic idea is Implementation of these operations is written once in the program and any other part of the program that needs to perform operations on the ADT can do so by calling the appropriate function. Implementation details need to be changed. It should be easy to do do so by changing the routines the t perform the ADT operations. The change would be completely transparent to the rest of the program. 1.2 List ADT Let us consider the list of the form A 1, A 2, A 3...A n Size of the list is N. Size of the empty list is 0. For any nonempty List, A i+1 follows A i (i<n) and A i _ 1 precedes A i (i>1). First element is A 1.The last element is A N. We will not define the predecessor of A and successor of A. The position of the element Ai in a list is i.

3 Some popular operations are PrintList Print the elements in the list. MakeEmpty Make the list Empty Find returns the position of the first occurrence of a key. Insert Delete inserts and deletes some key from some position in the list FindKth returns the element in some position. Next & Previous take a position as argument and return the position of the successor and predecessor. Example: If list is 34, 12,52,16,12 Find (52) returns 3 Insert(X, 4) makes the list into 34, 12, 52, X, 16, 12. Delete (52) might turn that list into 34, 12, X, 16, A Simple Array implementation of Lists All of these operations can be implemented by using an array. Even if the array is dynamically allocated, an estimate of the maximum size is required. An array implementation allows PrintList and Find to be carried out in linear time FindKth takes constant time. Insertion and deletion are expensive. For example, Inserting at position 0 requires first pushing the entire array down one spot to make room Deleting the first element requires shifting all elements in the list up one, So the worst case of these operations is O (N). On average, half of the list needs to be moved for either operation, so linear time is still required. Building a list by N successive inserts would require quadratic time. Because the running time for insertions and deletions is so slow and list size must be known in advance.

4 1.2.2 Linked Lists Definition: A list is implemented by each item having a link to the next item. Node: In order to avoid the linear cost of insertion and deletion, we need to move to Linked list. The linked list consists of a series of structures. Each structure contains the element and a pointer to a structure containing its successor which is Next pointer. Next pointer of the last cells is NULL. A pointer variable is variable that contains the address where the data are stored. If P is declared as pointer, contains value stored in P is location in memory, where a structure can be found. A field of the structure can be accessed by P Fieldname, Fieldname is the name of the field. EXAMPLE: A linked list INSERTION INTO A LINKED LIST

5 DELETION FROM LINKED LIST After deleting a node EXECUTION OF PrintList(L) and Find(L,Key)

6 We First pass pointer to the first element in the list Traverse th list by following the Next pointers. would take linear time Programming details Problems related with programs where we may go wrong There is no obvious way to insert at the front of the list Deleting from the front of the list may change the start of the list or loss of the list. Deletion algorithm require us to keep track of the cell before the one that we want to delete One simple change solves all 3 problems We will keep a sentinel node or header node or Dummy node The header is in position 0. To avoid the problems related with deletions we need to write routine. FindPrevious return the position of the predecessor of the cell we wish to delete. If we use a header, FindPrevious return the position of the header. Type declarations for linked list #ifndef _List_H Struct Node; Typedef Struct Node *ptrtonode; Typedef PtrToNode List; Typedef PtrToNode position; List MakeEmpty(List L); Int IsEmpty(List L); Int IsLast(Position P,List L); Position Find (ElementType X,List L); Void Delete (ElementType X,List L); Position Find Previous (ElementType X,List L); Void Insert (ElementType X,List L); Void deletelist(list L);

7 Position Header (List L); Position First (List L); Position Advance (Position P); ElementType Retrieve (Position P); #endif /* Place in the implementation file*/ Struct Node ElementType Element; Position Next; ; Function to test whether a linked list is empty Int IsEmpty(List L) Return L Next==Null; Function to test whether current position is last in a linked list Int IsLast(Position p,list L) return P Next==Null; Find Routine Position Find (Element Type X, List L) Position P; P=L Next; While (P! =Null && P Element! =X) P=P Next; Return P; Delete routine for linked list Void Delete (ElementType X,List L) Position P,Tmpcell; P=FindPrevious(X,L); If(!IsLast (P,L) )

8 Tmpcell==P Next; P =TmpCell Next; Free (TmpCell); Find Previous-The find routine for use with delete Find Previous ((ElementType X,List L) Position P; P=FindPrevious(X,L); If(!IsLast (P,L) ) Tmpcell==P Next; P =TmpCell Next; Free (TmpCell); Insertion routine for linked list Void insert ((ElementType X,List L,Position p) Position Tmpcell; Tmpcell=malloc(sizeof(struct node)); If(Tmpcell==NULL) FatalError( Out of space!!! ); Tmpcell Element=X; TmpCell Next=p Next; P Next= TmpCell; Common Errors The program will crash with nasty messages from the system Memory access violation Segmentation violation It is due to that the pointer variable contains a bogus address. One common reason is failure to initialize the variable.

9 If the line 1 is omitted, P is undefined and is not likely to be pointing at a valid part of memory. If P is NULL, then the indirection is illegal.this fuction knows that p Is not NULL, so the routine is OK. Whenever we do indirection, we must make that the pointer is not NULL. When and when not to use malloc to get a new cell, We must declare a pointer to a structure but only gives enough space to hold the address. The only way to create record that is not already declared is to use the malloc library routine. Malloc(HowManyBytes) has the system create a new structure and return a pointer to it. If we want use a new pointer variable to run down a list, thereis no need to create a new structure.in that malloc is inappropriate. Type cast is needed on very old compilers. The C library provides other variations of malloc and calloc.which require the inclusion of stdlib.h Incorrect way to delete a list Void DeleteList (List L) Position p; /*1 */ P=L Next; L Next=NULL; While (P! =NULL)) Free (P); P=P Next; Correct way to delete a list When things are no longer needed, we can issue a free command to inform the system that it may reclaim the space. The free (P) command is that the address that P is pointing to is unchanged, but the data that reside at that address are now undefined. If we never delete from a linked list, the number of calls to malloc should equal the size of the list, plus 1 if a header is used. If our program uses a lot of space, the system may be unable to satisfy our request for a new cell. So NULL pointer is returned. After a deletion in a linked list, It is usually a good idea to free the cell, especially if there are lots of insertions and deletions intermingled and memory become a problem. malloc(sizeof(ptrtonode) is legal, but it does not allocate enough space for a structure. It allocates space only for a pointer.

10 Void DeleteList (List L) Position p.tmp; P=L Next; L Next=NULL; While (P! =NULL)) Tmp= P Next; Free (P); P=Tmp; Disposal is not necessarily a fast thing; we want check to see if the disposal routine is causing any slow performance. The above program was made 25 times faster by commenting out the disposal (of10, 000 nodes).to dispose N cells, linear program have to spend O(Nlog N) time. malloc(sizeof(ptrtonode) is legal, but it does not allocate enough space for a structure. It allocates space only for a pointer Doubly Linked Lists A linked list in which each node has 2 pointers: 1. forward pointer (a pointer to the next node in the list) 2. Backward pointer (a pointer to the node preceding the current node in the list) is called a doubly linked list Circularly Linked Lists

11 A linked list have a beginning node and an ending node: the beginning node was a node pointed to by a special pointer called head and the end of the list was denoted by a special node which had a NULL pointer in its next pointer field. If a problem requires that operations need to be performed on nodes in a linked list and it is not important that the list have special nodes indicating the front and rear of the list, then this problem should be solved using a circular linked list. A singly linked circular list is a linked list where the last node in the list points to the first node in the list. A circular list does not contain NULL pointers Ex s We can define an abstract data type for single variable polynomials by using a list. Let f(x) =A X Polynomial ADT Type declarations for array implementation of the polynomial ADT Typedef struct int CoeffArray[MaxDegree +!]; int HighPower; *polynomial; Procedure to initialize a polynomial to zero void ZeroPolynomial (Polynomial Poly) int i; for(i=0;i<=maxdegree;i++) poly CoeffArray[i]=0; Poly HighPower=0; Procedure to add two polynomials Void AddPolynomial(const Polynomial poly1,const polynomial poly2,polynomial Polysum) int i; Zeropolynomial ( Polysum);

12 Polysum HighPower=Max(Poly1 HighPower,poly2 HighPower); For(i=Polysum Highpower;i>=0;i--) PolySum CoeffArray[i]=Poly1 coeffarray[i]=0 + Poly2 coeffarray[i]; Procedure to multiply two polynomials Void MultPolynomial(const Polynomial poly1,const Polynomial poly2,polynomial Polyprod) int i,j; ZeroPolynomial(PolyProd); PolyProd HighPower=Poly1 Highpower+Poly2 HighPower; If(PolyProd HighPower>MaxDegree) Error( Exceeded array size ); Else For(i=0;i<=Poly1 HighPower; i++) PolyProd CoeffArray[i+j += Poly1 coeffarray[i] * Poly2 coeffarray[j]; Type declarations for Linked list implementation of the polynomial ADT Typedef struct Node *PtrToNode; Struct Node int Coefficient; int Exponent; PtrToNode Next; ; typedef ptrtonode Polynomial; Radix sort (card sort) If we have N integers in the range 1 to M (or 0 to M-1), we can use this information to obtain a fast sort known as bucket sort. We keep an array called count of size M, which is initialized to 0.so count has M cells (buckets) which are initially empty. When A is read, increment (by 1) counts [A.After all the input is read, scan the Count array, printing out a representation of the sorted list. Thus algorithm takes O (M+N); Suppose we have 10 numbers, in the range 0 to 999, that we would like to sort. This is N numbers in the range 0 to N-1 for some constant P. We cannot use bucket sort. There would be too many buckets. The trick is to use several passes of bucket sort. The natural algorithm. Ex. Radix sort is on 10 numbers. The input is 64,8,216,512,27,729,0,1,343,125(the first 10 cubes arranged randomly)

13 The first step bucket-sorts by the least significant digit. The math is in base 10. The buckets are Buckets after first step of radix sort Buckets after the second pass of radix sort Buckets after the last pass of radix sort As an example, we could sort all integers that are represented on a computer (32 bits) by radix sort, if we did 3 passes over a bucket size of This algorithm would be O (N). Multilists A university with 40,000 students and 2,500 courses needs to be able to generate two types of reports. 1. Lists the registration for each class. 2. Lists by student, the classes that each student is registered for. We can t use the two dimensional array. Such an array would have 100 million entries. The average student registers for about 3 courses, so only of these entries, or roughly 0.1 percent, would actually have meaningful data.

14 All lists use a header and are circular. To list all of students in class c3, we start at c3 and traverse its information to this effect; this can be determined by following the students linked list until the header is reached. Once this is done, we return to c3 s list the position in the course list and find another cell which can be determined to belong to s3. We can continue and find that s4 and s5 are also in the class. And we can determine for any student, all of classes in which the student is Registered. Using circular lists saves space but does so at the expense of time. In the worst case if the student was registered for every course, then entry would need to be examined to determine all the course names for that student. In this application there few courses per student and few students per course, is not happen. If it were suspected that this would cause problem, then each of the cells could have pointers directly back to the student and class header. This would double the space requirement but would simplify and speed the implementation. Graph Representations (12/13) Example for Adjacency Multlists When a list node is included in several lists, the resulting structure is sometimes called a multilist.

15 Consider, for example, the tasks of providing reports on all books in a library on an arbitrary subject, and reports on all books by an arbitrary author. We could put the subject classification and author s name in the node for each book, then sort on the appropriate key, and print the relevant nodes, but a separate sort would be required for each of the two report types. Further, the sorts would need to be repeated after every update to the library s holdings. Instead, we could place the node for each book on two lists (using two next pointers), one for each subject classification and one for each author. To produce either type of report, we now only need to scan (in linear time) the entries on a given list. Adding a new book to the library requires creating an appropriate node and adding it to both of the relevant lists Cursor Implementation of Linked Lists Many languages such as BASIC, FORTRAN, do not support pointers. If linked lists are required and pointers are not available, then an alternative implementation must be used.i.e is cursor implementation. The two important features present in a pointer implementation of linked list are as follows; 1. The data z is stored in a collection of structures. Each structure contains data and a pointer to the next structure. 2. The new structure can be obtained from the system s global memory by a call to malloc and released by a call to free. The logical way to satisfy condition 1 is to have a global array of structures. For any cell in the array, its array index can be used in place of an address. To satisfy condition 2 by allowing the equivalent of malloc and free for cells in the cursorspace array. To do this, we will keep a list of cells that are not in any list. The list will use a cell 0 as a header.

16 An initialized cursor space Slot Element Next The value of 0 for Next is the equivalent of a NULL pointer. The initialization of CURSOR Space is a straightforward loop. To perform a malloc, the first element is removed from free list. To perform a free, we place the cell at the front of the free list. Routines for CursorAlloc and CursotFree Static position CursorAlloc(void) Position P; P=CursorSpace [0].Next; CursorSpace[0].Next=CursorSpace[p].Next; Return p; Static void CursorFree(Position P) CursorSpace [p].next=cursorspace[0].next CursorSpace[0].Next=p; We will implement our list with a header node. If the value of L is 5 and the value of M is 3, then l represents the list a,b,e, and M represents the list c,d,f.

17 Slot Element Next b f header - header - c d e a Function to test whether a linked list is empty-cursor implementation Int IsEmpty(List L) Return CursorSpace[L].next==0; Function to test whether current position is last in a linked list- cursor implementation Int IsLast(Position p,list L) Return CursorSpace[p].next==0; Find Routine- cursor implementation Position Find (Element Type X, List L) Position P; P= CursorSpace[L].next; While(P&& CursorSpace [P].Element!=X) P= CursorSpace[P].next; Return P; Delete routine for linked list - cursor implementation

18 Void Delete (ElementType X,List L) Position P,Tmpcell; P=FindPrevious(X,L); If(!IsLast (P,L) ) Tmpcel= CursorSpace[P].next; CursorSpace[P].next= CursorSpace[Tmpcell].next; CursorFree (TmpCell); Insertion routine for linked list- cursor implementation Void insert ((ElementType X,List L,Position p) Position Tmpcell; Tmpcell=Cursorlloc(); If(Tmpcell==0) FatalError( Out of space!!! ); CursorSpace[Tmpcell].Element=X; CursorSpace[Tmpcell].Next= CursorSpace[P].Next; CursorSpace[P]. Next= TmpCell; 1.3 Stack ADT Stack model A stack is a list with the restriction that insertions and deletions can be performed In only one position namely, the end of the list called top. A stack is an abstract data type and data structure based on the principle of Last In First Out (LIFO). Stacks are used extensively at every level of a modern computer system. The stack is usually implemented with more operations than just "push" and "pop". The length of a stack can often be returned as a parameter. Another helper operation top [1] (also known as peek or peak) can return the current top element of the stack without removing it from the stack.

19 1.3.2 Implementation of Stacks Since, stack is a list, any list implementation will do. Two popular implementations are 1. Pointers 2. Arrays. Type declarations for linked list implementation of the stack ADT #ifndef _Stack_H Struct Node; Typedef Struct Node *ptrtonode; Typedef PtrToNode Stack; Typedef PtrToNode position; Int IsEmpty(Stack S); Stack CreateStack(void); Void disposestack(stacl S); Void MakeEmpty(Stack s); Void Push (ElementType X,stack S L); ElementType Top (Stack S); #endif Stack implementation is a linked list with header Struct Node ElementType Element; PtrToNodeon Next; ; Function to test whether a stack is empty- Linked List

20 Int IsEmpty(Stack S) Return S Next==Null; Routine to create an empty stack-linked list implementation Stack CreateStack(void) Stack S; S=malloc(sizeof(struct Node)); If(S==Null) FatalError( Out of space!!! ); MakeEmpty(S); Return S; void MakeEmpty(Stack S) if(s==null) Error ( Must use CreateStack first ); Else While(!IsEmpty(S)) Pop(S); Void push (ElementType X,Stack S) PtrToNode TmpCell; Tmpcell=malloc(sizeof(struct Node)); f(tmpcell ==NULL ) FatalError( Out of space!!! ); Else Tmpcell Element=X;; Tmpcell Next=S Next S Next= Tmpcell; ElementType Top(Stack S) if (!IsEmpty(S)) return S Next Element; Error( Empty stack ); Return 0;

21 Void pop (Stack S) PtrToNode FirstCell; If(!IsEmpty (S) ) Error ( Empty Stack ); Else Firstcell==S Next; S Next=S Next Next; Free (FirstCell); Array Implementation of Stacks #ifndef _Stack_h Struct StackRecord; Typedef Struct StackRecord *Stack Int IsEmpty(Stack S); Int IsFull(Stack S); Stack CreateStack( int MaxElements); Void disposestack(stack S); Void Push (ElementType X,stack S); Void Pop(Stack s); ElementType TopAndPop (Stack S); #endif Stack implementation is a dynamically allocated array #defineemptytos(-1) #define MinStack (5) Struct StackRecord int capacity; int TopOfStack; ElementType *Array; ; Routine to create an empty stack-linked list implementation Stack CreateStack(void) Stack S;

22 If(MaxElements<MinStackSize) Error( Stack size is too small ) S=malloc(sizeof(struct Node)); If(S==Null) FatalError( Out of space!!! ); S Array= malloc(sizeof(elementtype) *MaxElements); If(S Array ===Null) FatalError( Out of space!!! ); S capacity=maxelements; MakeEmpty(S); Return S; void DisposeStack(Stack S) if(s!=null) free(s Array); free(s); void MakeEmpty(Stack S) S TopOfStack=EmptyTos;; Void push (ElementType X,Stack S) if (IsFull(S)) Error ( Full stack ) Else s Array[++S TopOfStack]; Error( EmptyStack ); Retrn 0; ElementType Top(Stack S) if (!IsEmpty(S)) return S Next Element; Error( Empty stack ); Return 0; Void pop (Stack S) If(!IsEmpty (S) )

23 Error ( Empty Stack ); Else s TopOfStack--; Return 0; Elementtype TopAnd pop (Stack S) If(!IsEmpty (S) ) Return S Array[S TopOfStack]; Error ( Empty Stack ); Return 0; Applications Balancing Symbols Braces, paranthenses, brackets, begin, ends must match each other [ [ ( )] ] []() Easy check using stacks Start from beginning of the file. Push the beginning symbols you wish to match, ignore the rest. Push brace, parantheses, brackets, ignore the alphabets Whenever you encounter a right symbol, pop an element from the stack. If stack is empty, then error. Otherwise, if the popped element is the corresponding left symbol, then fine, else there is an error. Check brace, bracket parentheses matching [a+b1*2]9*1+(2-1) Push [, Push, Pop, Pop, Push (, Pop Postfix Expressions Postfix notation 1[1] is a notation for writing arithmetic expressions in which the operands appear before their operators. There are no precedence rules to learn, and parentheses are never needed. Because of this simplicity, some popular handheld calculators use postfix notation to avoid the complications of the multiple parentheses required in nontrivial infix expressions. You are to write a computer program that simulates how these postfix calculators evaluate expressions.

24 In elementary school you learned how to evaluate simple expressions that involve the basic binary operators: addition, subtraction, multiplication, and division. (These are called binary operators because they each operate on two operands.) It is easy to see how a child would solve the following problem: =? As expressions become more complicated, the pencil and paper solutions require a little more work. A number of tasks must be performed to solve the following problem: (((13-1) / 2)((3 5))) =? These expressions are written using a format known as infix notation. It is easy to make a mistake writing or interpreting an infix expression containing multiple nested sets of parentheses. Postfix notation is another format for writing arithmetic expressions. In this notation, the operator is written after the two operands. Here are some simple postfix expressions and their results. Postfix Expression Result / The rules for evaluating postfix expressions with multiple operators are much simpler than those for evaluating infix expressions; simply evaluate the operations from left to right. Now, let s look at a postfix expression containing two operators. 6 2 / 5 + We evaluate the expression by scanning from left to right. The first item, 6, is an operand, so we go on. The second item, 2, is also an operand, so again we continue. The third item is the division operator. We now apply this operator to the two previous operands. Which of the two saved operands is the divisor? The one we saw most recently. We divide 6 by 2 and substitute 3 back into the Expression replacing 6 2 /. Our expression now looks like this: We continue our scanning. The next item is an operand, 5, so we go on. The next (and last) item is the operator +. We apply this operator to the two previous operands, obtaining a result of 8. Here s another example * Scanning from left to right, the first operator we encounter is +. Applying this to the two preceding operands, we obtain the expression

25 * The next operator we encounter is -, so we subtract 2 from 7 obtaining 9 5 * Finally, we apply the last operator, *, to its two preceding operands and obtain our final answer, 45. Here are some more examples of postfix expressions containing multiple operators and the results of evaluating them. See if you get the same results when you evaluate them. Postfix Expression Result * * 7 / * * + * * + 18 Your task is to write a program that evaluates postfix expressions entered interactively from the keyboard that adheres to the following specification. Specification: Program Postfix Evaluation Function The program evaluates postfix arithmetic expressions containing real numbers and the operators +, -, *, and /. Input The input is a series of arithmetic expressions entered interactively from the keyboard in postfix notation. The user is prompted to enter an expression made up of operators (the characters '+', '-', '*', and '/') and numbers. You can assume that the numbers are one digit numbers for this lab (0-9). They must be read in as characters (because the operators are read in as characters), and converted to numbers. (EXTRA CREDIT: allow real numbers with multiple digits and decimals, such as 56.78) The end of the expression is marked by the expression terminator character, '='. Operators and operands must be terminated by at least one blank. Output The user terminates the program by entering the character "#" instead of a new expression. The user is prompted for each new expression to be evaluated with the following prompt: "Enter expression to evaluate or a # to quit." After the evaluation of each expression, the results are printed to the screen:

26 "Result = value" Where value is the result of the postfix expression (a real number). Assumptions 1. The expressions are correctly entered in postfix notation. 2. Each expression is terminated by '='. 3. The operations in expressions are valid at run time. This means that we do not try to divide by zero. Using a Stack for Evaluation If we think about the postfix expressions, and how to evaluate them, we realize that when an operator is (the characters '+', '-', '*', and '/') is encountered, we apply it to the previous two operands (which have been read in, or are results of previous computation). For example, in the postfix expression: 6 2 / 5 + we read the 6 and the 2 and then read the operand. The operand is applied to the two numbers. If we place numbers, or results of equations on a stack, then we can apply operations to the two top elements of the stack. In the expression above, we read a 6, and push it onto the stack. Then we read a 2 and push it onto the stack. We read the character '/', and apply it to the two top elements of the stack. 6/2 is 3 so we push 3 onto the stack. We read a 5 and push it onto the stack. When we read the final '+', we apply it to the two top elements of the stack (3 and 5). Deliverables A listing of your program including any classes used A listing of the expressions that you used to test your program. A listing of the output file from the test runs 1.4 Queue ADT List where an element is inserted at the end, and deleted from the beginning Insertion deletion at different ends Queue Model The basic operations on a queue are EnQueue, which inserts an element at the end of the list (rear) And Dequeue, which deletes (returns) the element at the start of the list (front)

27 Model of a Queue DeQueue(Q) Queue (Q) EnQueue(Q) For each queue data structure, we keep an array, Queue [], and the positions Front and Rear, which represents the ends of the queue. Queue Operations Enqueue which inserts a new element in the queue; the new element becomes the new tail of the queue; Dequeue which removes one element from the queue; the element removed is the current head of the queue; The new head will be the immediately preceding element in the sequence; front and rear which simply read the value which is located respectively in the head and in the tail of the queue Figure 2 shows with some examples the effect of the various operations on a queue. Type declarations for Queue array implementation #ifndef _Queue_h Struct QueueRecord; Typedef Struct QueueRecord *Queue; Int IsEmpty(Queue Q); Int IsFull(Queue Q); Queue CreateQueue ( int MaxElements); Void disposequeue(queue Q); Void MakeEmpty (Queue Q); Void EnQueue(ElementType X,Queue Q); ElementType Front(Queue Q); Void Dequeue(Queue Q)); ElementType Front And DeQueue (Queue Q); #endif

28 Queue implementation is a dynamically allocated array #define MinQueuesize (5) Struct QueueRecord int capacity; int front; int rear; int size; ElementType *Array; ; void IsEmpty(Queue Q) return Q size==0; Routine to make an empty Queue-array implementation void MakeEmpty(Queue Q) Q Size=0; Q front=1; Q Rear=0; Routines to enqueue- array implementation Static int Succ (int value, Queue Q) if(++value==q capacity) Value=0; Return Value; Void EnQueue(ElementType X, Queue Q) if(isfull(q)) error ( FullQueue ); else Q Size++; Q Rear=Succ(Q Rear,Q); Q Array [Q Rear] =X;

29 1.4.3 Applications of Queues 1. When jobs are submitted to a printer they arranged in order of arrival. Thus essentially, Jobs sent to a line printer are placed on a Queue. 2. Computer networks There are many network setups of personal computers in which the disk is attached in one machine, known as the file server. Users on other machines are given access to files on a first-come first-served basis, so the data structure is a queue. 3. Calls to large companies are generally placed on a queue when all operators are busy. 4. In large universities where resources are limited, students must sign a waiting list if all terminals are occupied. The student who has been at terminal the longest is forced off first, and the student who has been waiting the longest is the next user. 5. A whole branch of mathematics, known as queuing theory, deals with computing realistically, how long users expect to wait on a line, how long the line gets, and other such questions. Review questions PART A 1. Write down the definition of data structures? 2. Give few examples for data structures? 3. Define Algorithm? 4. What are the features of an efficient algorithm? 5. List down any four applications of data structures? 6. What is meant by an abstract data type (ADT)? 7. What are the operations of ADT? 8. What is meant by list ADT? 9. What are the various operations done under list ADT? 10. What is a Rational number? 11. What are the two parts of ADT? 12. What is a Sequence? 13. Define len(s), first(s), last(s),nilseq? 14. What are the four basic data types? 15. What are the two things specified in declaration of variables in C? 16. What is a pointer? 17. What is an array? 18. What are the two basic operations that access an array? 19. Define Structure? 20. Define Union? 21. What is a Stack?

30 22. What are the two operations of Stack? 23. Write postfix from of the expression A+B-C+D? 24. What is a Queue? 25. What is a Priority Queue? 26. What are the different ways to implement list? 27. What are the advantages in the array implementation of list? 28. What is a linked list? 29. Name the two fields of Linked list? 30. What is a doubly linked list? 31. Name the three fields of Doubly Linked list? 32. Define double circularly linked list? 33. What is the need for the header? 34. Give some examples for linear data structures? 35. Write postfix from of the expression A+B-C+D? 36. How do you test for an empty queue? 37. What are the postfix and prefix forms of the expression? 38. Explain the usage of stack in recursive algorithm implementation? 39. Write down the operations that can be done with queue data structure? 40. What is a circular queue? PART B 1. What is a Stack? Explain with example? 2. Write the algorithm for converting infix expression to postfix expression? 3. What is a Queue? Explain its operation with example? 4. What is a Priority Queue? What are its types? Explain? 5. Write an algorithm for inserting and deleting an element from doubly linked list? 6. Explain Linear linked implementation of Stack and Queue? Reference Books 1. Weiss Data Structures and Algorithm Analysis in C, Addison Wesley, Second Edition, Aaron M.Tanaenbaum, Yedidyah Langsam, Moshe J. Augenstein Data Structures using C, Printice hall of India, Seymour Lipschutz, Data Structures Schaums outline series, Tata McGraw Hill, NewDelhi, For Further references

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

DATA STRUCTURES AND ALGORITHMS UNIT I LINEAR STRUCTURES

DATA STRUCTURES AND ALGORITHMS UNIT I LINEAR STRUCTURES DATA STRUCTURES AND ALGORITHMS UNIT I LINEAR STRUCTURES Highlights of the chapter Introduce the concept of Abstract Data Types (ADTs). Show how to efficiently perform operations on lists. Introduce the

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

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50 Lists, Stacks, and Queues (Lists, Stacks, and Queues ) Data Structures and Programming Spring 2016 1 / 50 Abstract Data Types (ADT) Data type a set of objects + a set of operations Example: integer set

More information

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3 UNIT 3 LINEAR DATA STRUCTURES 1. Define Data Structures Data Structures is defined as the way of organizing all data items that consider not only the elements stored but also stores the relationship between

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

DATA STRUCTURE UNIT I

DATA STRUCTURE UNIT I DATA STRUCTURE UNIT I 1. What is Data Structure? A data structure is a mathematical or logical way of organizing data in the memory that consider not only the items stored but also the relationship to

More information

p Write a program to evaluate a postfix expression. #ifndef _Stack_H #define _Stack_H #define ElementType double

p Write a program to evaluate a postfix expression. #ifndef _Stack_H #define _Stack_H #define ElementType double p.81 3.19 Write a program to evaluate a postfix expression. #ifndef _Stack_H #define _Stack_H #define ElementType double struct Node; typedef struct Node *PtrToNode; typedef PtrToNode Stack; int IsEmpty(Stack

More information

Prepared by Mrs.D.Maladhy (AP/IT/RGCET) Page 1

Prepared by Mrs.D.Maladhy (AP/IT/RGCET) Page 1 Basics : Abstract Data Type(ADT) introduction to data structures representation - implementation Stack and list: representing stack implementation application balancing symbols conversion of infix to postfix

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

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

infix expressions (review)

infix expressions (review) Outline infix, prefix, and postfix expressions queues queue interface queue applications queue implementation: array queue queue implementation: linked queue application of queues and stacks: data structure

More information

SNS COLLEGE OF TECHNOLOGY

SNS COLLEGE OF TECHNOLOGY SNS COLLEGE OF TECHNOLOGY COIMBATORE 5 DEPARTMENT OF COMPUTER SIENCE AND ENGINEERING (UG & PG) Second Year Computer Science and Engineering, rd Semester 2 Marks Question and Answer Subject Code & Name:

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

List, Stack, and Queues

List, Stack, and Queues List, Stack, and Queues R. J. Renka Department of Computer Science & Engineering University of North Texas 02/24/2010 3.1 Abstract Data Type An Abstract Data Type (ADT) is a set of objects with a set of

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

List, Stack and Queue Implementation

List, Stack and Queue Implementation Roy Chan CSC2100B Data Structures Tutorial 2 (Version 2) January 21, 2009 1 / 39 1 CSC2100B Online Judge Score 2 Structure 3 Linked List Overview Implementation 4 Stack Overview Implementation 5 Queue

More information

Problem with Scanning an Infix Expression

Problem with Scanning an Infix Expression Operator Notation Consider the infix expression (X Y) + (W U), with parentheses added to make the evaluation order perfectly obvious. This is an arithmetic expression written in standard form, called infix

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

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

CSE 230 Intermediate Programming in C and C++

CSE 230 Intermediate Programming in C and C++ CSE 230 Intermediate Programming in C and C++ Structures and List Processing Fall 2017 Stony Brook University Instructor: Shebuti Rayana http://www3.cs.stonybrook.edu/~cse230/ Self-referential Structure

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

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

Problem with Scanning an Infix Expression

Problem with Scanning an Infix Expression Operator Notation Consider the infix expression (X Y) + (W U), with parentheses added to make the evaluation order perfectly obvious. This is an arithmetic expression written in standard form, called infix

More information

III Data Structures. Dynamic sets

III Data Structures. Dynamic sets III Data Structures Elementary Data Structures Hash Tables Binary Search Trees Red-Black Trees Dynamic sets Sets are fundamental to computer science Algorithms may require several different types of operations

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. 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

CS 216 Exam 1 Fall SOLUTION

CS 216 Exam 1 Fall SOLUTION CS 216 Exam 1 Fall 2004 - SOLUTION Name: Lab Section: Email Address: Student ID # This exam is closed note, closed book. You will have an hour and fifty minutes total to complete the exam. You may NOT

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

Stacks, Queues and Hierarchical Collections

Stacks, Queues and Hierarchical Collections Programming III Stacks, Queues and Hierarchical Collections 2501ICT Nathan Contents Linked Data Structures Revisited Stacks Queues Trees Binary Trees Generic Trees Implementations 2 Copyright 2002- by

More information

INSTITUTE OF AERONAUTICAL ENGINEERING

INSTITUTE OF AERONAUTICAL ENGINEERING INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad - 500 043 COMPUTER SCIENCE AND ENGINEERING TUTORIAL QUESTION BANK Course Name Course Code Class Branch DATA STRUCTURES ACS002 B. Tech

More information

CS301 - Data Structures Glossary By

CS301 - Data Structures Glossary By CS301 - Data Structures Glossary By Abstract Data Type : A set of data values and associated operations that are precisely specified independent of any particular implementation. Also known as ADT Algorithm

More information

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Cpt S 122 Data Structures. Course Review Midterm Exam # 1 Cpt S 122 Data Structures Course Review Midterm Exam # 1 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Midterm Exam 1 When: Friday (09/28) 12:10-1pm Where:

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

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS DATA STRUCTURES AND ALGORITHMS UNIT 1 - LINEAR DATASTRUCTURES 1. Write down the definition of data structures? A data structure is a mathematical or logical way of organizing data in the memory that consider

More information

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

CS8391-DATA STRUCTURES QUESTION BANK UNIT I CS8391-DATA STRUCTURES QUESTION BANK UNIT I 2MARKS 1.Define data structure. The data structure can be defined as the collection of elements and all the possible operations which are required for those

More information

PESIT Bangalore South Campus Department of MCA Course Information for

PESIT Bangalore South Campus Department of MCA Course Information for 1. GENERAL INFORMATION: PESIT Bangalore South Campus Department of MCA Course Information for Data Structures Using C(13MCA21) Academic Year: 2015 Semester: II Title Code Duration (hrs) Lectures 48 Hrs

More information

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures MIDTERM EXAMINATION Spring 2010 CS301- Data Structures Question No: 1 Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the

More information

An Introduction to Trees

An Introduction to Trees An Introduction to Trees Alice E. Fischer Spring 2017 Alice E. Fischer An Introduction to Trees... 1/34 Spring 2017 1 / 34 Outline 1 Trees the Abstraction Definitions 2 Expression Trees 3 Binary Search

More information

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM

More information

Programming. Lists, Stacks, Queues

Programming. Lists, Stacks, Queues Programming Lists, Stacks, Queues Summary Linked lists Create and insert elements Iterate over all elements of the list Remove elements Doubly Linked Lists Circular Linked Lists Stacks Operations and implementation

More information

Overview of today s lecture. Quick recap of previous C lectures. Introduction to C programming, lecture 2. Abstract data type - Stack example

Overview of today s lecture. Quick recap of previous C lectures. Introduction to C programming, lecture 2. Abstract data type - Stack example Overview of today s lecture Introduction to C programming, lecture 2 -Dynamic data structures in C Quick recap of previous C lectures Abstract data type - Stack example Make Refresher: pointers Pointers

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 18 EXAMINATION Subject Name: Data Structure Model wer Subject Code: 17330 Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in

More information

CS8391-DATA STRUCTURES

CS8391-DATA STRUCTURES ST.JOSEPH COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERI NG CS8391-DATA STRUCTURES QUESTION BANK UNIT I 2MARKS 1.Explain the term data structure. The data structure can be defined

More information

Summer Final Exam Review Session August 5, 2009

Summer Final Exam Review Session August 5, 2009 15-111 Summer 2 2009 Final Exam Review Session August 5, 2009 Exam Notes The exam is from 10:30 to 1:30 PM in Wean Hall 5419A. The exam will be primarily conceptual. The major emphasis is on understanding

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

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS Contents Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS 1.1. INTRODUCTION TO COMPUTERS... 1 1.2. HISTORY OF C & C++... 3 1.3. DESIGN, DEVELOPMENT AND EXECUTION OF A PROGRAM... 3 1.4 TESTING OF PROGRAMS...

More information

CS 8391 DATA STRUCTURES

CS 8391 DATA STRUCTURES DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK CS 8391 DATA STRUCTURES UNIT- I PART A 1. Define: data structure. A data structure is a way of storing and organizing data in the memory for

More information

1 P age DS & OOPS / UNIT II

1 P age DS & OOPS / UNIT II UNIT II Stacks: Definition operations - applications of stack. Queues: Definition - operations Priority queues - De que Applications of queue. Linked List: Singly Linked List, Doubly Linked List, Circular

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

DHANALAKSHMI COLLEGE OF ENGINEERING Tambaram, Chennai

DHANALAKSHMI COLLEGE OF ENGINEERING Tambaram, Chennai DHANALAKSHMI COLLEGE OF ENGINEERING Tambaram, Chennai 601 301 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING III SEMESTER - R 2017 CS8381 DATA STRUCTURES LABORATORY LABORATORY MANUAL Name Register No Section

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

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS LECTURE 8 Babeş - Bolyai University Computer Science and Mathematics Faculty 2017-2018 In Lecture 7... ADT Queue ADT Matrix ADT List ADT Stack Today ADT Queue 1 ADT Queue 2 3 4 Note ADT Queue We will not

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

SAURASHTRA UNIVERSITY

SAURASHTRA UNIVERSITY SAURASHTRA UNIVERSITY RAJKOT INDIA Accredited Grade A by NAAC (CGPA 3.05) CURRICULAM FOR B.Sc. (Computer Science) Bachelor of Science (Computer Science) (Semester - 1 Semester - 2) Effective From June

More information

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #13

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #13 Department of Computer Science & Engineering Indian Institute of Technology Kharagpur Topic: Stack and Queue Practice Sheet #13 Date: 04-04-2017 1. Consider the following sequence of push and pop operations

More information

CS PROGRAMMING & ATA STRUCTURES I. UNIT I Part - A

CS PROGRAMMING & ATA STRUCTURES I. UNIT I Part - A CS6202 - PROGRAMMING & ATA STRUCTURES I Question Bank UNIT I Part - A 1. What are Keywords? 2. What is the difference between if and while statement? 3. What is the difference between while loop and do

More information

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 The data of the problem is of 2GB and the hard disk is of 1GB capacity, to solve this problem we should Use better data structures

More information

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO) Outline stacks stack ADT method signatures array stack implementation linked stack implementation stack applications infix, prefix, and postfix expressions 1 Stacks stacks of dishes or trays in a cafeteria

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

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

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS Syllabus: Pointers and Preprocessors: Pointers and address, pointers and functions (call by reference) arguments, pointers and arrays, address arithmetic, character pointer and functions, pointers to pointer,initialization

More information

ADT: Lists, Stacks and Queues

ADT: Lists, Stacks and Queues H.O. #8 Fall 2015 Gary Chan ADT: Lists, Stacks and Queues N:6, 7, 8, 11 Outline List as an ADT Stacks An array-based implementation of lists Linked lists with pointer implementation Operations and implementations

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

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May www.jwjobs.net R10 SET - 1 II B. Tech I Semester, Supplementary Examinations, May - 2012 (Com. to CSE, IT, ECC ) Time: 3 hours Max Marks: 75 *******-****** 1. a) Which of the given options provides the

More information

// The next 4 functions return true on success, false on failure

// The next 4 functions return true on success, false on failure Stacks and Queues Queues and stacks are two special list types of particular importance. They can be implemented using any list implementation, but arrays are a more practical solution for these structures

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Queues ArrayQueue Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 10, 2014 Abstract These lecture notes are meant to be looked

More information

Lists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4

Lists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4 Lists, Stacks and Queues in C CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4 Outline Structure Linked List Overview Implementation Stack Overview Implementation Queue Overview Implementation 2

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

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 03 / 31 / 2017 Instructor: Michael Eckmann Today s Topics Questions? Comments? finish RadixSort implementation some applications of stack Priority Queues Michael

More information

Advanced Java Concepts Unit 3: Stacks and Queues

Advanced Java Concepts Unit 3: Stacks and Queues Advanced Java Concepts Unit 3: Stacks and Queues Stacks are linear collections in which access is completely restricted to just one end, called the top. Stacks adhere to a last-in, first-out protocol (LIFO).

More information

DATA STRUCTURES USING C

DATA STRUCTURES USING C Govt. of Karnataka, Department of Technical Education Diploma in Computer Science & Engineering Third Semester Subject: DATA STRUCTURES USING C Contact Hrs / week: 4 Total hrs: 64 Table of Contents Chapter

More information

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 ) ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 ) Unit - 2 By: Gurpreet Singh Dean Academics & H.O.D. (C.S.E. / I.T.) Yamuna Institute of Engineering & Technology, Gadholi What is a Stack? A stack is a

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

17CS33:Data Structures Using C QUESTION BANK

17CS33:Data Structures Using C QUESTION BANK 17CS33:Data Structures Using C QUESTION BANK REVIEW OF STRUCTURES AND POINTERS, INTRODUCTION TO SPECIAL FEATURES OF C Learn : Usage of structures, unions - a conventional tool for handling a group of logically

More information

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion. 1. What is the full form of LIFO? The full form of LIFO is Last In First Out. 2. Give some examples for stack application. The examples of stack application are reverse a string, post fix evaluation, infix

More information

V.S.B ENGINEERING COLLEGE DEPARTMENT OF INFORMATION TECHNOLOGY I IT-II Semester. Sl.No Subject Name Page No. 1 Programming & Data Structures-I 2

V.S.B ENGINEERING COLLEGE DEPARTMENT OF INFORMATION TECHNOLOGY I IT-II Semester. Sl.No Subject Name Page No. 1 Programming & Data Structures-I 2 V.S.B ENGINEERING COLLEGE DEPARTMENT OF INFORMATION TECHNOLOGY I IT-II Semester Sl.No Subject Name Page No. 1 Programming & Data Structures-I 2 CS6202 - PROGRAMMING & DATA STRUCTURES UNIT I Part - A 1.

More information

Computer Science Foundation Exam

Computer Science Foundation Exam Computer Science Foundation Exam January 12, 2019 Section I A DATA STRUCTURES NO books, notes, or calculators may be used, and you must work entirely on your own. Name: UCFID: NID: Question # Max Pts Category

More information

CS 211 Programming Practicum Fall 2018

CS 211 Programming Practicum Fall 2018 Due: Wednesday, 11/7/18 at 11:59 pm Infix Expression Evaluator Programming Project 5 For this lab, write a C++ program that will evaluate an infix expression. The algorithm REQUIRED for this program will

More information

Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers 04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University

More information

Trees. A tree is a directed graph with the property

Trees. A tree is a directed graph with the property 2: Trees Trees A tree is a directed graph with the property There is one node (the root) from which all other nodes can be reached by exactly one path. Seen lots of examples. Parse Trees Decision Trees

More information

Hashing. Hashing Procedures

Hashing. Hashing Procedures Hashing Hashing Procedures Let us denote the set of all possible key values (i.e., the universe of keys) used in a dictionary application by U. Suppose an application requires a dictionary in which elements

More information

CS DATA STRUCTURES AND ALGORITHMS

CS DATA STRUCTURES AND ALGORITHMS Computer Science and Engineering Third Semester CS1211 - DATA STRUCTURES AND ALGORITHMS UNIT-I - INTRODUCTION TO DATASTRUCTURES 1.Write down the definition of data structures? PART -A A data structure

More information

HASH TABLES. Hash Tables Page 1

HASH TABLES. Hash Tables Page 1 HASH TABLES TABLE OF CONTENTS 1. Introduction to Hashing 2. Java Implementation of Linear Probing 3. Maurer s Quadratic Probing 4. Double Hashing 5. Separate Chaining 6. Hash Functions 7. Alphanumeric

More information

CSCI 171 Chapter Outlines

CSCI 171 Chapter Outlines Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures

More information

COP 3502 Section 2 Exam #2 Version A Spring /23/2017

COP 3502 Section 2 Exam #2 Version A Spring /23/2017 COP 3502 Section 2 Exam #2 Version A Spring 2017 3/23/2017 Lecturer: Arup Guha Directions: Answer all multiple choice questions on the scantron. Each question has a single correct answer. In case of ambiguities,

More information

Computer Science Foundation Exam

Computer Science Foundation Exam Computer Science Foundation Exam January 12, 2019 Section I A DATA STRUCTURES SOLUTIONS NO books, notes, or calculators may be used, and you must work entirely on your own. Question # Max Pts Category

More information

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues By: Pramod Parajuli, Department of Computer Science, St. Xavier

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

Draw a diagram of an empty circular queue and describe it to the reader.

Draw a diagram of an empty circular queue and describe it to the reader. 1020_1030_testquestions.text Wed Sep 10 10:40:46 2014 1 1983/84 COSC1020/30 Tests >>> The following was given to students. >>> Students can have a good idea of test questions by examining and trying the

More information

15. Stacks and Queues

15. Stacks and Queues COMP1917 15s2 15. Stacks and Queues 1 COMP1917: Computing 1 15. Stacks and Queues Reading: Moffat, Section 10.1-10.2 Overview Stacks Queues Adding to the Tail of a List Efficiency Issues Queue Structure

More information

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd 19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd Will you walk a little faster? said a whiting to a snail, There s a porpoise close behind us, and he s treading

More information

Department of Computer Science and Technology

Department of Computer Science and Technology UNIT : Stack & Queue Short Questions 1 1 1 1 1 1 1 1 20) 2 What is the difference between Data and Information? Define Data, Information, and Data Structure. List the primitive data structure. List the

More information

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator EXAMINATION ( End Semester ) SEMESTER ( Autumn ) Roll Number Section Name Subject Number C S 1 1 0 0 1 Subject Name Programming

More information

CS W3134: Data Structures in Java

CS W3134: Data Structures in Java CS W3134: Data Structures in Java Lecture #10: Stacks, queues, linked lists 10/7/04 Janak J Parekh HW#2 questions? Administrivia Finish queues Stack/queue example Agenda 1 Circular queue: miscellany Having

More information

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu. 17CA 104DATA STRUCTURES Academic Year : 018-019 Programme : MCA Year / Semester : I / I Question Bank Course Coordinator: Mrs. C.Mallika Course Objectives The student should be able to 1. To understand

More information

CMSC Introduction to Algorithms Spring 2012 Lecture 7

CMSC Introduction to Algorithms Spring 2012 Lecture 7 CMSC 351 - Introduction to Algorithms Spring 2012 Lecture 7 Instructor: MohammadTaghi Hajiaghayi Scribe: Rajesh Chitnis 1 Introduction In this lecture we give an introduction to Data Structures like arrays,

More information

Stacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal.

Stacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal. (Section 2) Linked list implementation of stack Typical Application of stacks Evaluation of expressions (infix, postfix, prefix) References and further details By: Pramod Parajuli, Department of Computer

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 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