Document Sample

Data Structures and C++ I year ECM D2 II sem G. sumalatha Syllabus for B. Tech. I Year II semester Computer Science and Engineering Code:101CS01 DATA STRUCTURES AND C++ UNIT – I Introduction to data structures: Abstract data type(ADT), Stacks and Queues circular queues and their implementation with arrays.Stack applications: infix to post fix conversion, postfix expression evaluation. Applications of queues. UNIT – II Singly linked lists, doubly linked lists, circular list and their operations, representing stacks and queues with linked lists. UNIT – III Trees- Binary tress, terminology, representation, traversals, Minimal Spanning trees. Graphs- terminology, representation, graph traversals (dfs & bfs). UNIT – IV Searching - Linear and binary search methods. Sorting - Bubble sort, selection sort, Insertion sort, Quick sort, merge sort. UNIT – V Introduction to c++ programming-object oriented programming concepts, Structured Vs OOP. Classes and objects-class definition, Objects, class scope and accessing members, access functions and utility functions. UNIT – VI Constructors-default constructor, parameterized constructor, constructor initialization list, copy constructor. Destructors, Static class members this pointer, friend functions and classes, Dynamic memory management with operators new and delete. Overloading-function overloading, Operator overloading, restrictions on operator overloading, 2 10/1/2012 sumalatha overloading unary and binary operators, type conversion, templates, inheritance. 1.Data Structure Through C by Yashavant Kanetkar. 2.The complete reference C++ By Herb Schildt. 3. Data Structures, A pseudocode Approach with C by Richard F. Gilberg & Behrouz A. Forouzan. 10/1/2012 sumalatha 3 Data Structures UNIT - 1 Data Structures Definition: Data structures is a study of different methods of organizing the data and possible operations on these structures. Or Data structure is a representation of the logical relationship existing between individual elements of data. In other words , a data structure is a way of organizing all data items that consider not only the elements stored but also their relationship each other. 10/1/2012 sumalatha 5 data single or collection of instances integer = {0, +1, -1, +2, -2, +3, -3, …} daysOfWeek = {S,M,T,W,Th,F,Sa} instances may or may not be related myData = {apple, chair, red, green, Jack} Data structure: Data + relationships that exist among instances 10/1/2012 sumalatha 6 Classification of Data structure Data structures are normally divided into two categories: Linear data structures Non linear data structures. Linear Data structures: Linear data structures organize the data in a sequence manner. Ex: arrays , stacks , queues, Linked list Non-Linear Data Structures: The data is organized in non linear fashion. Ex: Trees , Graphs 10/1/2012 sumalatha 7 Representation of data structure • Static representation( using arrays) • Dynamic representation (using linked list) 10/1/2012 sumalatha 8 Data structure operations • Traversing: Accessing each element exactly once so that certain items in the record may be processed • Searching: Finding the location of a element with a given key value, or finding the locations of all elements which satisfy one or more conditions. • Inserting: Adding a new element to the structure. • Deleting: Removing a element from the structure. • Sorting: arranging the elements in some logical order. 10/1/2012 sumalatha 9 ABSTRACT DATA TYPE Data type: Collection of values +operations Ex: int: numbers Abstract Data Type : Collection of data and operations Abstraction No implementation details. Data structure: physical implementation of an ADT 10/1/2012 sumalatha 10 Introduction to Stacks • Definition • What is a Stack? • Basic operations of stack – Pushing, popping etc. • Stack ADT • Stack implementation using array. • Stack implementation using linked list. • Applications of Stacks. 10/1/2012 sumalatha 11 stack Definition: • A stack is an ordered collection of data in which data is inserted and deleted at one end (i.e same end) known as top of the stack. • As all the insertions and deletions in a stack is done from top of the stack , the last added element will be the first to be removed from the stack. • Due to this property it is also called Last In First Out (LIFO)data structure. 10/1/2012 sumalatha 12 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 inserted is the first one to be removed • Example 10/1/2012 sumalatha 13 Stacks (LIFO) Top: Examines the most recently inserted element E top D top D D top C top C C C B top B B B B A top A A A A A top 10/1/2012 sumalatha 14 Fundamental operations on stack: Push: Inserting an element to the stack Pop: Deletes the most recently inserted element empty stack push an element push another pop top B top top A A A top10/1/2012 sumalatha 15 An Example of a Stack top top 2 8 8 top 1 Push(8) 1 Push(2) 1 7 7 7 2 2 2 pop() top top 8 top 1 pop() 7 pop() 1 7 2 7 2 10/1/2012 sumalatha 2 16 Implementing Stack using Arrays Stack overflow and stack underflow Empty stack Full stack Consider stack[6] top 5 5 4 4 4 6 3 3 8 2 2 1 1 1 7 0 0 2 Top=-1 Now we can’t add new elt to this stack Because stack is full. Here we can’t perform pop When top=MAXSIZE-1 the stack is said operation because stack is empty to 10/1/2012 be overflow When top=-1 the stack is said to be sumalatha 18 underflow STACK ADT Data: Finite ordered list with zero or more elements. Operations: push(elt): Inserts an element into top of the stack elt pop() : Removes an element from the stack and return. traverse(): Displays all elements of the stack 10/1/2012 sumalatha 19 Implementation of stack using an array • Procedure: Step1: Declare a stack with fixed size and Initialize a variable top int stack[max_size] , top=-1; Step 2: implement following functions on this stack. void push( int elt); int pop(); 10/1/2012 sumalatha 20 Pseudo code to insert an element to stack (PUSH) push(int elt) 1. /* Check for stack overflow? */ if top=max_size-1 then print “overflow” and exit 2. else set top=top+1; /* increase top by one*/ 3. set stack[top] elt /* inserts elt in new top position*/ 4. exit 10/1/2012 sumalatha 21 Pseudo code to delete an element from the stack (POP) int pop() 1. /* Check for the stack underflow? */ if top<0 then print “stack underflow” and exit 2.else /*remove the top element */ elt stack[top] 3. /* decrement top by one*/ set top=top-1 4. /*Return deleted element from the stack */ return elt 5. exit 10/1/2012 sumalatha 22 Queues Queue Overview • Definition • Basic operations of queue – Enqueuing, dequeuing etc. • Queue ADT • Implementation of queue – Array – Linked list 10/1/2012 sumalatha 24 Queue Definition: • A queue is an ordered collection of data such that the data is inserted at one end and deleted from other end. Queue 10/1/2012 sumalatha 25 Queue (FIFO) • Accessing the elements of queues follows a First In, First Out (FIFO) order. • Example: • The first one in line is the first one to be served 10/1/2012 sumalatha 26 • In queue all insertions performed at one end called rear and deletions are performed at other end called front. Remove Insert (Dequeue) 10/1/2012 front sumalatha rear (Enqueue)27 Queue operations • Enqueue(insertion):Inserts an element at the rear of the queue • Dequeue(deletion):Removes an element from the front of the queue 10/1/2012 sumalatha 28 The following figures of queue show graphically during insertion operation (Queue[5] ) Front=-1 0 1 2 3 4 Rear=-1 Empty queue front rear 10/1/2012 sumalatha 29 The following figures of queue show graphically during insertion operation (Queue[5] ) Front=-1 0 1 2 3 4 Rear=-1 Empty queue front rear 0 1 2 3 4 After inserting one element 20 front=0 rear=0 Front rear 0 1 2 3 4 10/1/2012 sumalatha 30 The following figures of queue show graphically during insertion operation (Queue[5] ) Front=-1 0 1 2 3 4 Rear=-1 Empty queue front rear 0 1 2 3 4 After inserting one element 20 front=0 rear=0 Front rear 0 1 2 3 4 After inserting second elt 20 30 front=0 rear=1 10/1/2012 sumalatha 31 front rear Queue Overflow? 0 1 2 3 4 After inserting 5 elements 20 30 40 50 60 front=1 rear=4 Front rear No new element is inserted , because the queue is already full. When rear points to last position of the queue i.e (rear=MAXSIZE-1) the queue is said to be full 10/1/2012 sumalatha 32 The following figures of queue show graphically during deletion operation (Queue[5] ) Front=0 0 1 2 3 4 Rear=3 34 22 78 11 queue with 4 elts front rear 10/1/2012 sumalatha 33 The following figures of queue show graphically during deletion operation Queue[5] Front=0 0 1 2 3 4 Rear=3 34 22 78 11 queue with 4 elts front rear 0 1 2 3 4 After deleting one element front=1 22 78 11 rear=3 Front rear 10/1/2012 sumalatha 34 The following figures of queue show graphically during deletion operation Queue[5] Front=0 0 1 2 3 4 Rear=3 34 22 78 11 queue with 4 elts front rear 0 1 2 3 4 After deleting one element front=1 22 78 11 rear=3 Front rear 0 1 2 3 4 After deleting second elt 78 11 front=2 rear=3 10/1/2012 sumalatha 35 front rear Queue underflow? 0 1 2 3 4 After deleting all elements from queue front=4 rear=3 rear front Now no elements to be deleted from the queue, because the queue is empty When (front>rear) or( front=-1) queue is said to be empty 10/1/2012 sumalatha 36 Queue ADT Operations: Enqueue(): Inserting new element to queue Dequeue(): Deleting an element from queue Traverse(): Displays all the elements of queue. 10/1/2012 sumalatha 37 Implementation of queue using an array • Procedure: Step1: Declare an queue with fixed size and Initialize rear and front variables int queue[max_size] , rear=-1 , front=-1; Step 2: implement following functions on this queue. void enqueue( int elt); int dequeue(); 10/1/2012 sumalatha 38 A pseudo code to insert a new element to queue enqueue(int elt) 1. If rear=MAXSIZE-1 then print “Queue is full” 2. Else set rear=rear+1 3. /* insert an elt to queue */ Queue[rear]=elt 4. If front=-1 then set front =0 /* when first elt is inserted*/ 5. exit 10/1/2012 sumalatha 39 Pseudo code to delete an element from queue dequeue() (first method) 1.If front<0 then print “ Queue is empty” and exit 2. Else elt = queue[front] set front=front+1 4. if front>rear then print “queue is empty” set front=-1, rear=-1 /* Reinitialize front and rear values*/ 3. Return elt 5. Exit 10/1/2012 sumalatha 40 Pseudo code to delete an element from queue dequeue() (second method) 1.If front<0 then print “ Queue is empty” and exit 2. Else elt = queue[front] 4. if front=rear set front=-1, rear=-1 /* Reinitialize front and rear values*/ 3. Else set front=front+1 4. Return elt 5. Exit 10/1/2012 sumalatha 41 Limitations of Linear queue Memory wastage 10/1/2012 sumalatha 42 Limitations of Queue Front=0 0 1 2 3 4 Rear=4 34 22 78 11 13 queue with 5 elts front rear 10/1/2012 sumalatha 43 Limitations of Queue Front=0 0 1 2 3 4 Rear=4 34 22 78 11 13 queue with 5 elts front rear 0 1 2 3 4 After deleting three element front=3 11 13 rear=4 Front rear New element can’t be added to this queue even the space is available To avoid this problem will introduce circular queue. 10/1/2012 sumalatha 44 Circular Queue In circular queue ,last location (q[Maxsize-1]) of the queue followed by first location (q[0]) 10/1/2012 sumalatha 45 QUEUE WITH 3 ELEMENTS EMPTY QUEUE [2] [3] [1] [2] 22 53 [1] [4] [0] 23 [3] [0] [5] [5] [4] front = 0 front = 0 rear = 0 rear = 2 10/1/2012 Can be seen as a circular queue sumalatha 46 FULL QUEUE 1 2 22 33 How to test when queue is empty? How to test when queue is full? 0 11 3 44 How to increment front and rear values? 66 55 5 4 Front =0 Rear =5 10/1/2012 sumalatha 47 Incrementing rear and front values during insertion and deletion • Rear=(rear+1)% MAXSIZE • Front=(front+1)%MAXSIZE 10/1/2012 sumalatha 48 Pseudo code to insert an element to circular queue 1.if (front =(rear+1)%MAXSIZE) then print ‘‘Queue is full’’ and exit 2.Else if front=-1 then set front=0 3. /* Increment rear value by one*/ set rear=(rear+1)%MAXSIZE 4. /*Insert an elt to queue*/ queue[rear]=elt 4.exit 10/1/2012 sumalatha 49 Pseudo code to delete an element from circular queue 1. if front=-1 print ‘‘Queue is empty’’ and exit 2. else elt=queue[front] 3.if (front=rear) set front=-1 rear=-1 Else /* Increment front value by one */ front=(front+1)%MAXSIZE 4. exit 10/1/2012 sumalatha 50 Applications of Stack 1. Reversing a string 2. Recursion 3.Conversion of infix expressions to postfix expressions 4.Evaluation of postfix expressions 10/1/2012 sumalatha 51 1. Reversing a string Reversing using Stack operations push in : CHAD C pop out : 10/1/2012 sumalatha 53 Reversing using Stack operations push in : CHAD CH pop out : 10/1/2012 sumalatha 54 Reversing using Stack operations push in : CHAD CHA pop out : 10/1/2012 sumalatha 55 Reversing using Stack operations push in : CHAD CHAD pop out : 10/1/2012 sumalatha 56 Reversing using Stack operations push in : CHAD CHA pop out :D 10/1/2012 sumalatha 57 Reversing using Stack operations push in : CHAD CH pop out :DA 10/1/2012 sumalatha 58 Reversing using Stack operations push in : CHAD C pop out : DAH 10/1/2012 sumalatha 59 Reversing using Stack operations push in : CHAD pop out : DAHC 10/1/2012 sumalatha 60 2. Recursion Factorial of a number using recursion int fact(int n) { if (n==1) { return 1; } else { return n * fact(n-1); } } int main() { printf(“%d” , fact(4)); } Rewrite PUSH int fact(4) { if (4 = =1) { return 1; } else { return 4 * fact(4-1); } } int main() { printf(“%d” , fact(4)); } Fact(4) Rewrite PUSH int fact(4) { if (4 = =1) { return 1; } else { return 4 * fact(4-1); } } Fact(3) Fact(4) Rewrite PUSH int fact(3) { if (3 = =1) { return 1; } else { return 3 * fact(3-1); } } Fact(2) Fact(3) Fact(4) Rewrite PUSH int fact(2) { if (2 = =1) { return 1; } else { return 2 * fact(2-1); } } Fact(1) fact(2) Fact(3) Fact(4) Rewrite POP int fact(1) { if (1 = =1) { return 1; } } Fact(1) fact(2) Fact(3) fact(1)=1 Fact(4) Rewrite POP fact(1)=1 fact(2)=2*fact(1) =>2*1 fact(2) Fact(3) Fact(4) Rewrite POP fact(1)=1 fact(2)=2*fact(1) =>2*1 Fact(3)=3*fact(2)3*2 Fact(3) Fact(4) Rewrite POP fact(1)=1 fact(2)=2*fact(1) =>2*1 Fact(3)=3*fact(2)=>3*2 Fact(4)=4*fact(3)=>4* 6=>24 Fact(4) 3. Conversion of Infix expression to postfix expression REPRESENTATION OF EXPRESIONS • An Expression Consists of operators and operands. • 3 Ways to represent an expression: Infix notation : A + B (Operator placed in between operands) Postfix notation : AB+ (Operator placed after (post )operands) Prefix notation : +AB (Operator placed before (pre) operands) 10/1/2012 sumalatha 72 EVALATION OF EXPRESSIONS A/B * C +D (Infix) • These expressions are evaluated based on precedence and direction of operation. AB/C*D+ (Postfix) 1. No parenthesis, No precedence and No need to consider Direction of operation 2. Evaluation process is much simpler than attempting a direct evaluation from infix notation. Compiler uses postfix notation for evaluating expressions. 10/1/2012 sumalatha 73 Infix to Postfix conversion (manual) • An Infix to Postfix manual conversion algorithm is: 1. Completely parenthesize the infix expression according to order of priority you want. 2. Move each operator to its corresponding right parenthesis. 3. Remove all parentheses. • Examples: 3+4*5 (3 + (4 * 5) ) 345*+ a/b^c– d*e –a*c^3^4 abc^/d e*a c34 ^^* -- ((a / (b ^ c)) – ((d * e) – (a * (c ^ (3 ^ 4) ) ) ) ) 10/1/2012 sumalatha 74 Algorithm for converting Infix to postfix using stack Suppose IE is an arithmetic expression written in infix. This algorithm finds equivalent postfix expression PE. 1. Scan IE from left to right and repeat steps 2 to 5 for each element of IE until the STACK is empty. 2. If an operand is encountered, add it to PE. 3. If a left parenthesis is encountered, PUSH it onto STACK. 4. If an operator is encountered , then a) add Operator to STACK b) Repeatedly POP from STACK and add to PE if each operator has the same or higher precedence than scanned operator. 5. If a right parenthesis encountered, then repeatedly POP from STACK and add it to PE until a left parenthesis encountered. 10/1/2012 sumalatha 75 6. Exit. Infix to postfix conversion Using Stack infix (a+b-c)*d–(e+f) postfix 10/1/2012 Stack sumalatha 76 Infix to postfix conversion stack infix a+b-c)*d–(e+f) postfix ( 10/1/2012 sumalatha 77 Infix to postfix conversion stack infix +b-c)*d–(e+f) postfix a ( 10/1/2012 sumalatha 78 Infix to postfix conversion stack infix b-c)*d–(e+f) postfix a + ( 10/1/2012 sumalatha 79 Infix to postfix conversion stack infix -c)*d–(e+f) postfix ab + ( 10/1/2012 sumalatha 80 Infix to postfix conversion stack infix c)*d–(e+f) postfix ab+ - ( 10/1/2012 sumalatha 81 Infix to postfix conversion stack infix )*d–(e+f) postfix ab+c - ( 10/1/2012 sumalatha 82 Infix to postfix conversion stack infix *d–(e+f) postfix ab+c- 10/1/2012 sumalatha 83 Infix to postfix conversion stack infix d–(e+f) postfix ab+c- * 10/1/2012 sumalatha 84 Infix to postfix conversion stack infix –(e+f) postfix ab+c-d * 10/1/2012 sumalatha 85 Infix to postfix conversion stack infix (e+f) postfix ab+c–d* - 10/1/2012 sumalatha 86 Infix to postfix conversion stack infix e+f) postfix ab+c–d* ( - 10/1/2012 sumalatha 87 Infix to postfix conversion stack infix +f) postfix ab+c–d*e ( - 10/1/2012 sumalatha 88 Infix to postfix conversion stack infix f) postfix + ab+c–d*e ( - 10/1/2012 sumalatha 89 Infix to postfix conversion stack infix ) postfix + ab+c–d*ef ( - 10/1/2012 sumalatha 90 Infix to postfix conversion stack infix postfix ab+c–d*ef+ - 10/1/2012 sumalatha 91 Infix to postfix conversion stack infix postfix ab+c–d*ef+- 10/1/2012 sumalatha 92 Algorithm for Evaluating postfix Expression using stack Suppose P is an arithmetic expression written in postfix. This algorithm evaluates this expression and returns the resultant value. 1. Scan P from left to right and repeat steps 2 to 3 for each element of P . 2. If an operand is encountered, PUSH it onto stack. 3. If an operator © is encountered , then a) POP two operands from STACK ,say op1 and op2 b) Calculate op2 © op1 and PUSH this value to STACK. 4. POP the result from STACK and return the value. 5. Exit. 10/1/2012 sumalatha 93 Postfix String : 123*+4- Initially the Stack is empty. Now, the first three characters scanned are 1,2 and 3, which are operands. Thus they will be pushed into the stack in that order. Postfix String : *+4- Expression Stack Next character scanned is "*", which is an operator. Thus, we pop the top two elements from the stack and perform the "*" operation with the two operands. The second operand will be the first element that is popped. Postfix String : +4- 10/1/2012 Expression sumalatha 94 Stack The value of the expression(2*3) that has been evaluated(6) is pushed into the stack. Postfix string : +4- Expression Stack Next character scanned is "+", which is an operator. Thus, we pop the top two elements from the stack and perform the "+" operation with the two operands. The second operand will be the first element that is popped. Postfix string : 4- Expression Stack The value of the expression(1+6) that has been evaluated(7) is pushed into the stack. Postfix string : 4- Expression Stack 10/1/2012 sumalatha 95 Next character scanned is "4", which is added to the stack. Postfix string - Expression Stack Next character scanned is "-", which is an operator. Thus, we pop the top two elements from the stack and perform the "-" operation with the two operands. The second operand will be the first element that is popped. Postfix string Expression Stack The value of the expression(7-4) that has been evaluated(3) is pushed into the stack. Now, since all the characters are scanned, the remaining element in the stack (there will be only one element in the stack) will be returned. Expression Stack 10/1/2012 sumalatha 96 Result : 3 Applications of Queue • Job scheduling in OS • Printing 10/1/2012 sumalatha 97

DOCUMENT INFO

Shared By:

Tags:

Stats:

views: | 36 |

posted: | 10/2/2012 |

language: | English |

pages: | 97 |

OTHER DOCS BY vijay.ravipati

Docstoc is the premier online destination to start and grow small businesses. It hosts the best quality and widest selection of professional documents (over 20 million) and resources including expert videos, articles and productivity tools to make every small business better.

Search or Browse for any specific document or resource you need for your business. Or explore our curated resources for Starting a Business, Growing a Business or for Professional Development.

Feel free to Contact Us with any questions you might have.