




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































BEST OF LUCK!!
By Bishwas Pokharel(068/BCT/515) Pulchowk Campus
Data structure is a particular way of organizing and storing data in a computer so that data it can be used efficiently, in term of time and space. Organization of data takes place either in main memory or in disk storage. An example of common data structures are: array, stack, queue, linked list, tree, hash map etc. Data structure Primitive D.S Non-Primitive D.S Linear D.S Non Linear D.S Primitive D.S : These data structures are defined by system with their operation like add, sub, multiply etc. Some primitive data structures used in general programming languages are char, int, float, double etc. Non Primitive D.S : These data structures are formed by the collection of primitive data structures and for implementation it must be declared with their operation. To simplify the process of solving problems, these data structures are combined with their operation and are called Abstract Data Types. An ADT consist of two parts:
b. If no, we can terminate.
Total Cost = cost_of_car + cost _of_cycle. Total cost nearly equal to cost_of_car(approx..) For the above example, we can represent the cost of car and cost of cycle in terms of function and for given function ignore the low order terms that are relatively insignificant. For Ex: if n^4 , 2n^2,110n, 500 are individual costs of some function and approximate it to n^4 is the highest rate of growth. Running time in term of function:
Ex: f(n)=3n+8 is O(n) Solution: 3n+8 <=(3n+n) <=4n f(n)<=cg(n), for c =4 and n0>= 8
2. Big Omega-Ω notation: Let f(n) and g(n) be functions mapping nonnegative integers to real numbers. We say that f(n) is Ω(g(n)) (pronounced "f(n) is big-Omega of g(n)") if there is a real constant c > 0 and an integer constant n0 >= 1 such that f(n) >= cg(n), for n >= n0. 3. Big-Theta-ʘ notation: f ( n ) is Θ( g ( n )) (pronounced " f ( n ) is big-Theta of g ( n )") if f ( n ) is O( g ( n )) and f(n) is Ω( g ( n )), that is, there are real constants c 1 > 0 and c 2 > 0, and an integer constant n 0 >= 1 such that c 1 g ( n ) <= f ( n ) <= c 2 g ( n ), for n >= n 0. By:BishwasPokharel(068/BCT/515),Pulchowkcampus
Q. How stacks are used? Consider a working day in the office. Let us assume a developer is working on a long-term project. The manager then gives the developer a new task, which is more important. The developer places the long-term project aside and begins work on the new task. The phone rings, this is the highest priority, as it must be answered immediately. The developer pushes the present task into the pending tray and answers the phone. When the call completes the task abandoned top answer the phone is retrieved from the pending tray and work progress. A stack is an ordered list in which insertion and deletion are done at one end, where the end is called as top. The last element inserted is the first one to be deleted. Hence, it is called Last IN First Out(LIFO) list. When an element is inserted in a stack, the concept is called as push and when an element is removed from the stack, the concept is called as pop. Trying to pop out an empty stack is called as underflow and trying to push an element in a full stack is called as overflow.
Implementation in C++:
2. Pop Operations: The process of putting a new data element onto stack is known as pop operation. Pop operation involves a series of steps: Step 1 - Checks if the stack is empty. Step 2 - If the stack is empty, produces an error and exit. Step 3 - If the stack is not empty, access the elements at which the top is pointing. Step 4 - Decrease the value of top by 1 Step 5- Return success. Algorithms: 1. If top<0 then write “stack underflow and stop” 2. Stack[top]<-Null 3. top<-top- 1 4. stop Implementation in C++
#include
1. Stack can be used for checking balancing of symbols. Stacks can be used to check whether the given expression has balanced symbols or not. This algorithm is very much useful in compiler. Each time parser reads one character at a time. If the character is an opening delimiter like (,{,[- then it is written to stack. When a closing delimiter is encounter like ),},]- is encountered the stack is popped. The opening and closing delimiters are then compared. If they match, the parsing of the string continues. If they do not match, the parser indicate that thers is an error on the line. Algorithm: a) Create a stack. b) While(end of input is not reached){ { a) If the character read is not a symbol to be balanced, ignore it. b) If the character is an opening symbol like (,[,{, push it onto the stack. c) If it is a closing symbol like),],} then if the stack is empty report an error. Otherwise pop the stack. d) If the symbol popped is not the corresponding opening symbol, report an error. } c) At end of input, if the stack is not empty report an error. By using algorithm, check validity of ()(()[()]).
2.Infix to postfix conversion algorithm using stack. Infix : An infix expression take, only one single letter or it has two letters with operators (+,-,,/ )in between them or complete two infix expression with operators (+, - ,,/) in between them. A - > single letter A+B - > two letters with one operator + in between them. (A+B)+(C-D) - > Two infix expression with one operator + in between them. Prefix: An prefix expression take, only one single letter or it has two letters in sequence with (+,-,,/ )before them or complete two prefix expression with operators (+, - ,,/) in between them. A - > single letter ++AB - > two letters with one operator ++ before them. ++AB-CD - > Two prefixes expression. Postfix: An postfix expression take, only one single letter or it has two letters in sequence with (+,-,,/ )after them or complete two postfix expression with operators (+, - ,,/) in between them. A - > single letter AB+ - > two letters with one operator + before them. AB+CD-+ - > Two postfixes expression. Algorithm:
**1. Scan the infix expression from left to right.
Evaluate the postfix Expression: Algorithms:
3.2 Else, Pop the operator from the stack until the precedence of the scanned operator is less-equal to the precedence of the operator residing on the top of the stack. Push the scanned operator to the stack.
**4. If the scanned character is an ‘(‘, push it to the stack.
Queue: A Queue is an ordered list in which insertions are done at one end (rear) and deletions are done at other end (front). The first element inserted is the first one to be deleted. Hence, it is called First IN First Out (FIFO) list. A good example of queue is any queue of consumers for a resource where the consumer that came first is served first.The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.
Algorithms for dequeue operations: Accessing data from the queue is a process of two tasks − access the data where front is pointing and remove the data after access. The following steps are taken to perform dequeue operation − Step 1 − Check if the queue is empty. Step 2 − If the queue is empty, produce underflow error and exit. Step 3 − If the queue is not empty, access the data where front is pointing. Step 4 − Increment front pointer to point to the next available data element. Step 5 − Return success. procedure dequeue if queue is empty return underflow end if data = queue[front] front ← front + 1 return true end procedure Implementation of queue When we remove element from Queue, we can follow two possible approaches (mentioned [A] and [B] in above diagram). In [A] approach, we remove the element at head position, and then one by one move all the other elements on position forward. In approach [B] we remove the element from head position and then move head to the next position.
In approach [A] there is an overhead of shifting the elements one position forward every time we remove the first element. In approach [B] there is no such overhead, but whenever we move head one position ahead, after removal of first element, the size on Queue is reduced by one space each time.
Circular Queue: In a normal Queue Data Structure, we can insert elements until queue becomes full. But once if queue becomes full, we cannot insert the next element until all the elements are deleted from the queue. For example consider the queue below...