Docsity

LAB REPORT FOR DATA STRUCTURE AND ALGORITHMS ALL CONCEPTS COVERED, Lab Reports of Data Structures and Algorithms

Toggle sidebar
bg1
LIST OF EXPERIMENTS
EXPERIMENT 1 – REVISION OF C++ POINTERS AND ARRAYS.................................................................................................2
EXPERIMENT 2 – STACKS – DYNAMIC AND STATIC IMPLEMENTATION................................................................................. 8
EXPERIMENT 3 – APPLICATIONS OF STACK..........................................................................................................................14
EXPERIMENT 4 – QUEUES – DYNAMIC IMPLEMENTATION................................................................................................. 18
EXPERIMENT 5 – SINGLY LINKED LISTS................................................................................................................................23
EXPERIMENT 6 – DOUBLY LINKED LISTS.............................................................................................................................. 28
EXPERIMENT 7a – RECURSION............................................................................................................................................ 33
EXPERIMENT 7b – Tower of Hanoi...................................................................................................................................... 38
EXPERIMENT 8 – BINARY TREES..........................................................................................................................................40
EXPERIMENT 9 – BINARY SEARCH TREE...............................................................................................................................43
EXPERIMENT 10 – SORTING.................................................................................................................................................45
EXPERIMENT 11 – SEARCHING-LINEAR AND BINARY...........................................................................................................52
EXPERIMENT 12 – GRAPH....................................................................................................................................................55
EXPERIMENT 13 – GRAPH- MST & DIJKSTRA ALGO.............................................................................................................59
EXPERIMENT 14 – HASHING................................................................................................................................................63
1Data Structures and Algorithms
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41

Partial preview of the text

Download LAB REPORT FOR DATA STRUCTURE AND ALGORITHMS ALL CONCEPTS COVERED and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

LIST OF EXPERIMENTS

  • EXPERIMENT 1 – REVISION OF C++ POINTERS AND ARRAYS.................................................................................................
  • EXPERIMENT 2 – STACKS – DYNAMIC AND STATIC IMPLEMENTATION.................................................................................
  • EXPERIMENT 3 – APPLICATIONS OF STACK..........................................................................................................................
  • EXPERIMENT 4 – QUEUES – DYNAMIC IMPLEMENTATION.................................................................................................
  • EXPERIMENT 5 – SINGLY LINKED LISTS................................................................................................................................
  • EXPERIMENT 6 – DOUBLY LINKED LISTS..............................................................................................................................
  • EXPERIMENT 7a – RECURSION............................................................................................................................................
  • EXPERIMENT 7b – Tower of Hanoi......................................................................................................................................
  • EXPERIMENT 8 – BINARY TREES..........................................................................................................................................
  • EXPERIMENT 9 – BINARY SEARCH TREE...............................................................................................................................
  • EXPERIMENT 10 – SORTING.................................................................................................................................................
  • EXPERIMENT 11 – SEARCHING-LINEAR AND BINARY...........................................................................................................
  • EXPERIMENT 12 – GRAPH....................................................................................................................................................
  • EXPERIMENT 13 – GRAPH- MST & DIJKSTRA ALGO.............................................................................................................
  • EXPERIMENT 14 – HASHING................................................................................................................................................

EXPERIMENT 1 – REVISION OF C++ POINTERS AND ARRAYS

  1. Objectives: (a). Revise arrays and use of arrays with pointers (b). To learn basic terminologies and syntax to use pointers in C++
  2. Time Required: 3 hrs
  3. Software Required: (a). Windows OS (b). Bloodshed Dev C++ (c). Microsoft Visual Studio 2012
  4. Constructors: It is convenient if an object can initialize itself when it is first created, without need to make a separate call to its member functions. C++ provides a non-static member function called as constructor that is automatically called when object is created. After objects are created, their members can be initialized by using constructors.  Important points to be noted while using constructors  Constructor name must be the same as that of its class name in which it belongs.  Constructors are invoked automatically when objects are created.  Constructors cannot specify return types nor return values.  Constructors cannot be declared static or const.  Constructors should have public access within the class.Whe
  5. Destructors: A destructor is a member function that is called automatically when an object is destroyed. ~class name (); Where the class name is the name of the destructor and is preceded by a tilde (~) symbol.

Activity 1.2 : Swap 1st & last element of 1-dimensional Array

#include<iostream.h> #include<conio.h> int main() { intArr[ 100 ],n,temp; cout<<"Enter number of elements you want to insert "; cin>>n; for(inti= 0 ;i<n;i++) { cout<<"Enter element "<<i+ 1 <<":"; cin>>Arr[i]; } temp=Arr[ 0 ]; Arr[ 0 ]=Arr[n- 1 ]; Arr[n- 1 ]=temp; cout<<"\nArray after swapping"<<endl; for(i= 0 ;i<n;i++) cout<<Arr[i]<<" "; getch(); return 0 ; }

  1. Pointers: A pointer is a memory variable that stores a memory address of another variable and can also be defined as it is a variable that holds the address of some other memory location. It is always denoted by ‘*’. Following example describes that how to declare and assign memory address to an integer pointer.

Activity 1.3 : How to use pointer

#include int main () { intfirstvalue = 5, secondvalue = 15; int * p1, * p2; p1 = &firstvalue; // p1 = address of first value p2 = &secondvalue; // p2 = address of second value *p1 = 10; // value pointed by p1 = 10 *p2 = *p1; // value pointed by p2 = value pointed by p p1 = p2; // p1 = p2 (value of pointer is copied) *p1 = 20; // value pointed by p1 = 20 cout<< "firstvalue is " <<firstvalue<<endl; cout<< "secondvalue is " <<secondvalue<<endl; return 0; }

  1. Exercises:

Exercise 1.3: (4)

Introduce int variables x, y, z and int* pointer variables p, q, r. Set x, y, z to three distinct values. Set p, q, r to the addresses of x, y, z respectively.

  • Print with labels the values of x, y, z, p, q, r, *p, *q, *r.
  • Print the message: Swapping values.
  • Execute the swap code: z = x; x = y; y = z;
  • Print with labels the values of x, y, z, p, q, r, *p, *q, *r.
  1. Web Resources : http://www.tutorialspoint.com/cplusplus/cpp_constructor_destructor.htm http://www.cplusplus.com/doc/tutorial/arrays/ http://www.tutorialspoint.com/cplusplus/cpp_pointers.htm
  2. Video Resources: http://www.infiniteskills.com/training/programming-c++-training-video/arrays-in-functions.html http://www.learnerstv.com/video/Free-video-Lecture-5620-Computer-Science.htm http://www.learnerstv.com/video/Free-video-Lecture-13737-Computer-Science.htm
  3. Summary: This lab session introduces the concepts of constructors and destructors, arrays and pointers in C+ +. This lab is designed to revise the concepts of arrays and pointers which will be used in implementing the concepts of data structures.

Data4 Top Data Data Data1 Bottom

EXPERIMENT 2 – STACKS – DYNAMIC AND STATIC IMPLEMENTATION

  1. Objectives: (a). Understand the concept of stacks and their uses (b). Implement stacks using both static and dynamic implementation
  2. Time Required: 3 hrs
  3. Software Required: (a). Windows OS (b). Microsoft Visual Studio
  4. Stack: Stack is a memory portion, which is used for storing the elements. The elements are stored based on the principle of LIFO (Last In, First Out). In stack insertion and deletion of elements take place at the same end (Top). The last element inserted will be the first to be retrieved.
    • This end is called Top
    • The other end is called Bottom
  5. Top: The stack top operation gets the data from the top-most position and returns it to the user without deleting it. The underflow state can also occur in stack top operation if stack is empty.
  6. Push: The push operation adds a new element to the top of the stack, or initializes the stack if it is empty. If the stack is full and does not contain enough space to accept the given item, the stack is then considered to be in an overflow state.
  7. Pop: The pop operation removes an item from the top of the stack. A pop either returns previously inserted items, or NULL if stack is empty.
  8. Underflow: When stack contains equal number of elements as per its capacity and no more elements can be added, the status of stack is known as overflow.

c) Pop ( ) function: This function deletes an element from top of stack. *void ListStack::pop() { node temp; temp=top; if(top==NULL) cout<<"Stack UnderFlow"<<endl; else { cout<<"deleted Number from the stack ="; cout<<top->num; top=top->next; delete temp; } } d) Main( ) function: void main() { clrscr(); ListStack LS; int choice; do{ cout<<"Menu "<<endl; cout<<" 1 .Push" <<endl; cout<<" 2 .Pop"<<endl; cout<<" 3 .Show"<<endl; cout<<" 4 .EXIT"<<endl; cin>>choice; switch(choice){ case 1 : LS.push(); break; case 2 : LS.pop(); break; case 3 : LS.display(); break; } }while(choice!= 4 ); }

  1. Exercises:

Exercise 2.3: (5)

Write a program to compare opening and closing brackets in expression. This program takes an expression in the form of string and scan it character by character. Finally the output of the program is the valid or invalid expression. Algorithm: To do this comparison a stack ADT can be used to keep track of the scope delimiters encountered while scanning the expression.

  • Whenever a scope “opener” is encountered, it can be “pushed” onto a stack
  • Whenever a scope “ender” is encountered, the stack is examined:
    • If the stack is “empty”, there is no matching scope “opener” and the expression is invalid.
    • If the stack is not empty, we pop the stack and check if the “popped” item corresponds to the scope ender
    • If match occurs, we continue scanning the expression
  • When end of the expression string is reached, the stack must be empty, otherwise one or more opened scopes have not been closed and the expression is invalid
  1. Web Resources: http://www.cs.utsa.edu/~wagner/CS2213/stack/stack.html http://scanftree.com/Data_Structure/DynamicStack https://www.cs.bu.edu/teaching/c/stack/array/
  2. Video Resources: http://www.youtube.com/watch?v=sFVxsglODoo http://www.youtube.com/watch?v=FNZ5o9S9prU
  3. Summary: This lab introduces the concepts of stacks. This lab is designed to implement the concepts of stacks using both static and dynamic implementation.

EXPERIMENT 3 – APPLICATIONS OF STACK

Objectives: (c). Understand the applications of stacks (d). Implement stacks using both static and dynamic implementation Time Required: 3 hrs Software Required: (c). Windows OS (d). Microsoft Visual Studio

  1. Infix to Postfix: An algorithm for parsing an infix notation without parentheses is given here. Algorithm:
  2. Create an empty stack called opstack for keeping operators. Create an empty string for output.
  3. Each character in string is a token.
  4. Scan the token list from left to right. o If the token is an operand, append it to the end of the output string. o If the token is a left parenthesis, push it on the opstack. o If the token is a right parenthesis, pop the opstack until the corresponding left parenthesis is removed. Append each operator to the end of the output string. o If the token is an operator, *, /, +, or -, push it on the opstack. However, first remove any operators already on the opstack that have higher or equal precedence and append them to the output list.
  5. When the input expression has been completely processed, check the opstack. Any operators still on the stack can be removed and appended to the end of the output string.

Exercise 3.2: (5)

Write a C++ code to evaluate the infix expression without parenthesis given by user. Example: **3+4-5/ Infix String Operand Stack Operator Stack Value 3 3

  • 3 + 4 3 4 +**

- 3 4 + - 5 3 4 5 + - / 3 4 5 + - / 2 3 4 5 2 + - / 3 4 + - / 2 5 -> 5/2 , Push 2 3 4 2 + - 3 + - 2 4 -> 4-2, Push 2 3 2 + 5 + 2 3 -> 3+2, Push 5

  1. Web Resources: http://jcsites.juniata.edu/faculty/kruse/cs240/stackapps.html http://scanftree.com/Data_Structure/Application-of-stack
  2. Video Resources: http://www.youtube.com/watch?v=x_B_mH5BpiE http://www.youtube.com/watch?v=iWnPbPUfhCg
  3. Summary: This lab introduces different real world applications of stacks and their implementation using static stacks in C++.

17 CS 330 Operating Systems – Process

  1. Dynamic Queue Implementation: a) Queue Class definition: class DynQueue{ private: structqueueNode { intnum; *queueNode next; }; *queueNode front; *queueNode rear; public: DynQueue(); ~DynQueue(); void enqueue(); void dequeue(); bool isEmpty(); void displayQueue(); void makeNull(); }; b) Constructor: DynQueue::DynQueue() { front = NULL; rear = NULL; } c) Enqueue() function: void DynQueue::enqueue() { *queueNode ptr; ptr = new queueNode; cout<<"Enter Data"; cin>>ptr->num; ptr->next= NULL; if (front == NULL) { front = ptr;

*rear = front; } else { rear->next=ptr; rear = ptr; } } d) Dequeue( ) Function: void DynQueue::dequeue() { queueNode temp; temp = front; if(isEmpty()) cout<<"Queue is Empty"; else { cout<<"data deleted="<<temp->num; front = front->next; delete temp; } }

  1. Exercises:

Exercise 4.1: (3)

Write a menu driven program to perform different operations with queue such aEnqueue ( ), Dequeue( ) and DisplayQueue( ).