CS255 Data Structure Lab Manual

download CS255 Data Structure Lab Manual

of 29

Transcript of CS255 Data Structure Lab Manual

  • 8/17/2019 CS255 Data Structure Lab Manual

    1/29

     D 2012 

    C D F I 1 

    Data Structure Lab 

    Yarmouk University

    Faculty of Information Technology and Computer

    Sciences

    Computer Science Department 

  • 8/17/2019 CS255 Data Structure Lab Manual

    2/29

     D 2012 

    C D F I 2 

     

    C 117. 1 C .

    Course Description:This course concentrates on the practical part of the course of Data Structure with

    OOP underC++ Environment. This course allows students to understand practically

    the Logical andphysical representation of data, algorithms, complexity and efficiency,

    data Structure operations, dense lists, and matrix representations, linked lists and their

    Different variations, string storage representation and manipulation, queues and stacks

    And their applications, tree structures and their different variations, graphs and

    Networks , sorting techniques, searching techniques 

    Course Objectives:1.  Extend programming ability using an object oriented language.

    2.  Analyze algorithms to determine time and space complexity.

    3.  Build and manipulate linear and non-linear data structures, including stacks,

    queues, linked lists, trees, and graphs.

    4.  Sort, search, and merge data.

    5.  Choose the appropriate data structure to use in solving typical computer

    science problems.

    Learning Outcomes:After Completing this course the student must demonstrate the Knowledge and

    ability to:

    1.  Demonstrate the application of software engineering principles in design, coding ,

    and testing of large programs.

    2.  To introduce students to essential data structures such as linked lists, stacks,

    queues, trees, and graphs. This introduction emphasizes the specification of each

    structure as an abstract data type before discussing implementations and application

    of the structure.

    3.  Make students aware of the importance of object – oriented methods in

    developing software.4.  Student must know the systematic approach to study algorithms , by focuses first

    on understanding the action of the algorithm then analyzing it. 

    Evaluation Plan:Students will be evaluated based on the following criteria:

    •  Quizzes after finishing each lab subject 60%

    •  Assignment, attendance, participation 10%

    •  Final Lab Exam 30%

    Course Plan

  • 8/17/2019 CS255 Data Structure Lab Manual

    3/29

     D 2012 

    C D F I 3 

    Weeks  Subjects and Contents 

    Week 1  Array (Static data structure)

    −  array creation and implementation

    − 

    Passing to functionWeek 2  Revision

    Revision for C++ statements (reading, writing, control structure and

    functions)

    Week 3  Recursion

    Design a recursive functions.

    Week 4  Dynamic Array

    −  Creation

    −  Passing to function

    −  Insertion Implementation

    −  Delete Implementation

    −  Search Implementation

    −  Sort Implementation

    −  Separation implementation

    −  Merge Implementation

    Week 5  Linked list

    − 

    Creation

    −  Passing to function

    −  Insertion Implementation

    −  Delete Implementation

    −  Search Implementation

    −  Sort Implementation

    Week 6  Double linked list

    −  Creation

    −  Passing to function

    − 

    Insertion Implementation−  Delete Implementation

    −  Search Implementation

    −  Sort Implementation

    −  Separation implementation

    −  Merge Implementation

    Week 7  Stack ADT (array implementation)

    Implementing basic operation of stack ( push, pop ) using array

    implementation

    Week 8  Stack ADT (linked list implementation)

  • 8/17/2019 CS255 Data Structure Lab Manual

    4/29

     D 2012 

    C D F I 4 

    Implementing basic operation of stack ( push, pop ) using linked list

    implementation

    Week 9  queue ADT (array implementation)

    Implementing basic operation of Queue ( Enqueue, Dequeue ) using arrayimplementation

    Week 10  queue ADT (linked list implementation)

    Implementing basic operation of Queue (Enqueue, Dequeue) using linked list

    implementation

    Week 11  Binary tree

    Implement Binary tree traversal methods : Preorder, In-order, Post-

    ordered traversal. Recursive Algorithms for above mentioned

    Traversal methods.

    Week 12  Binary Search tree

    Implementing Binary search tree operation ( search ,addition, deletion).

  • 8/17/2019 CS255 Data Structure Lab Manual

    5/29

     D 2012 

    C D F I 5 

    Array

    AIM:To write a program that implements the basic operations of the

    Array in C++.

    Array

    An array is a series of elements of the same type placed in contiguous memory

    locations that can be individually referenced by adding an index to a unique identifier.

    For example, we can store 5 values of type int in an array without having to declare 5

    different variables, each one with a different identifier. Instead of that, using an array

    we can store 5 different values of the same type, with a unique identifier.

    Lab Question:Write a program that computes the Summation of Array Values.

    1.  ()

    2. 

    3.  = 16, 2, 77, 40, 12071;

    4.  , =0;

    5.  ( =0 ;

  • 8/17/2019 CS255 Data Structure Lab Manual

    6/29

     D 2012 

    C D F I 6 

    Dynamic Array

    AIM:To write a program that implements a dynamic Array in C++.

    Dynamic Array

    Is a random access, variable-size list data structure that allows elements to be added

    or removed. It is supplied with standard libraries in many modern mainstream

    programming languages.

    Lab Question:

    Write A c++ Program Which Creates And Prints Dynamic Array Of Integer Type.

    1.  #include2.  using namespace std;

    3.  class array {4.  private:5.  int size;6.

     

    int *ptr;

    7.  public:8.  array(int s) {9.  size=s;10. ptr=new int[size];

    11. 

    for(int a=0;aptr[a];17. }18. };19.

     

    20. void main() {21. int x;22. cout

  • 8/17/2019 CS255 Data Structure Lab Manual

    7/29

  • 8/17/2019 CS255 Data Structure Lab Manual

    8/29

     D 2012 

    C D F I 8 

    AIM:To write a program that implements the basic operations of the

    linked list in C++ 

    Linked List Overview

    • 

    List is a collection of components, called nodes. Every node (except the lastnode) contains the address of the next node. Every node in a linked list has

    two components:

    1.  one to store the relevant information (that is, data)

    2.  one to store the address, called the link or next , of the next node in the

    list.

    •  The address of the first node in the list is stored in a separate location, calledthe head or first

    •  The address of the last node in the list is stored in a separate location, called

    the tail or last

    UML view of basic operation in list

    Lab Question:

    Write class called node that contain:

  • 8/17/2019 CS255 Data Structure Lab Manual

    9/29

     D 2012 

    C D F I 9 

    •  data members:

    1.  data as integer.

    2.  next as pointer of type node.

    3.  head as pointer of type node.

    4.  tail as pointer of type node.

    5. 

    current as pointer of type node.6.  count as integer.

    •  The following functions:

    1.  constructor to initialize data member

    2.  Function called initializelist to enter values to list until reach to value -1.

    3.  Function called print to print list.

    Then write main to test this class

    Answer:1.

     

    class node

    2. {

    3. 

    private:

    4. 

    int data;5. node *next;

    6. 

    node *current,*head,*tail;

    7. public:

    8. node(){

    9. 

    head=current=tail=next=NULL;

    10.  data=0;

    11. 

    }

    12.  void initializelist() { //create

    13.  head=tail=current=new node;

    14. 

    cin>>current->data;

    15.  int x;

    16. 

    cin>>x;17.  while(x!=-1){

    18.  current=new node;

    19. 

    current->data=x;

    20.  tail->next=current;

    21. 

    tail=current;

    22. 

    cin>>x; }

    23.  }

    24. 

    void print() { //Print

    25.  current=head;

    26.  while(current!=tail) {

    27. 

    cout

  • 8/17/2019 CS255 Data Structure Lab Manual

    10/29

     D 2012 

    C D F I 10 

    1.  Function called max to return maximum value in the list.

    2.  Function called length to return number of nodes in list.

    3.  Function called sort to sort elements of list.

    Exercise 2:Modify class node in Exercise 1 by adding the following:

    1.  Function called InsertAtBegin to add node at the begging of list.

    2.  Function called InsertAtEnd to add node at the last of list.

    3.  Function called Insert to inset new value after specific value in list.

    4.  Function called DeleteFromBegin to delete node from the begging of list.

    5.  Function called DeleteFromEnd to delete node from the last of list.

    6.  Function called Delete to delete maximum value in list.

  • 8/17/2019 CS255 Data Structure Lab Manual

    11/29

     D 2012 

    C D F I 11 

    Double linked list ADT 

    AIM:To write a program for double linked list implementation . 

    A doubly linked list overview:

    A doubly linked list is a linked list in which every node has a next pointer and a

    back pointer. Every node contains the address of the next node (except the last

    node), and every node contains the address of the previous node (except the first

    node). A doubly linked list can be traversed in either direction

    Lab Question:

    Write class called node that contain:

    •  data members:7.  data as integer.

    8.  next as pointer of type node.

    9.  back as pointer of type node.

    10. 

    head as pointer of type node.11. tail as pointer of type node.

    12. current as pointer of type node.

    •  The following functions:

    4.  constructor to initialize data member

    5.  Function called initializelist to enter values to list until reach to value -1.

    6.  Function called print to print list.

    Then write main to test this class

    Answer:

  • 8/17/2019 CS255 Data Structure Lab Manual

    12/29

     D 2012 

    C D F I 12 

    Exercise 1:

    Modify class node in Exercise 1 by adding the following:

    4.  Function called max to return maximum value in the list.

    5.  Function called length to return number of nodes in list.

    6.  Function called sort to sort elements of list.

    7. 

    Function called reverse to print elements of list in reverse order.

    class node

    {

    private:

    int data;

    node *next , *back;

    node *current,*head,*tail;

    public:node()

    { head=current=tail=next=NULL;

    data=0;

    }

    void initializelist()//create

    {

    head=tail=current=new node;

    cin>>current->data;

    int x;

    cin>>x;

    while(x!=-1)

    { current=new node;

    current->data=x;head->next=current;

    head=current;

    cin>>x;

    }

    }

    void print()//Print

    {

    current=head;

    while(current!=tail)

    {

    cout

  • 8/17/2019 CS255 Data Structure Lab Manual

    13/29

     D 2012 

    C D F I 13 

    Stack ADT 

    AIM:To write a program for linked implementation of stack in C++ 

    Stacks Overview

      Stack is data structure in which the elements are added and removed from one

    end only; a Last In First Out (LIFO) data structure

    UML view of stack:

    Lab Question:

    Write class called Stack that contains:

    •  data members:

    1.  size as integer.

    2.  stack as pointer of type stack.

    3.  stacktop as pointer of type stack.

    •  The following functions:

    1.  constructor to initialize data member

    2.  intializestack to pushing elements to stack.

    Then write main to test this class

    Answer:

  • 8/17/2019 CS255 Data Structure Lab Manual

    14/29

     D 2012 

    C D F I 14 

    1. class Stack

    2. 

    {

    3. private:

    4. 

    int maxsize;

    5. 

    int stacktop;

    6. int *stack;

    7. 

    public:

    8. array_stack(int s=100)

    9. {

    10. 

    maxsize=s;

    11.  stack=new int[maxsize];

    12. 

    stacktop=0;

    13. 

    }

    14.  };

    Exercise 1:Modify class node in Exercise 1 by adding the following:

    1.  Function Push to add element to stack

    2.  Function Pop to delete element from stack

    3.  Function isEmpty to true if stack is empty otherwise return false.

    4.  Function isFull to true if stack is empty otherwise return false.

    5.  Function top to retrieve elements at top of stack.

    6.  separate function called reverse to reverse elements of stack in another stack

  • 8/17/2019 CS255 Data Structure Lab Manual

    15/29

     D 2012 

    C D F I 15 

    Queue ADT 

    AIM:To write a program for array implementation of queue in C++ 

    QUEUE:

    The queue is a data structure that allows use of two ends of it. I mean from one

    end of thequeue you can insert the elements and from another end of the queue

    you can delete theelements.The Queue can be formally defined as ordered

    collection of elements that has two endsnamed as front and rear. From the front

    end one can delete the elements and from the rear endone can insert the

    elements. The queue is also called as First in First out (FIFO) data structure.

    TWO TYPES OF REPRESENTATIONS OF QUEUE

    •  Array Representations

    •  Linked list Representation

    Basic operations:

    Enqueue(x) : insert item x at the rear

    Dequeue() : delete the rear element

  • 8/17/2019 CS255 Data Structure Lab Manual

    16/29

     D 2012 

    C D F I 16 

    Queue ADT – Array implementations 

    AIM: To write a program for linked list implementation of queue 

    Queue is nothing but the collection of items. Both the ends of the queue are

    having their own functionality. All theelements in the queue are stored

    sequentially.The two queue ends are front and rear

    ENQUEUE OPERATION

    1)  Get the element to be inserted

    2)  Check whether the queue is full.

    3)  If the queue is full

    a.  we cannot insert an element into the queue.

    b.  Queue is overflow error occurs.

    4) 

    ELSEa.  We may insert the element into the queue.

    b.  Rear= (rear + 1)%size;

    c.  queue[rear]= x;

    DEQUEUE OPERATION

    1) 

    Check whether the queue is Empty.2)  If the queue is empty

    a.  we cannot delete an element from the queue.

    b.  Queue is empty error occurs.3)  ELSE

    a.  we maydelete an element from the queue.

    b.  x = queue[front];

    c.  Front = (front +1)%size;

    d.  Return x

  • 8/17/2019 CS255 Data Structure Lab Manual

    17/29

     D 2012 

    C D F I 17 

    Lab Question:

    Write a C++ code to implement enqueue and dequeue using array.

    1. 

    # include2.

     

    # include

    3. # define SIZE 20

    4. 

    class queue {

    5. int a[SIZE];

    6. int front;

    7. 

    int rear;

    8. int count

    9. 

    queue();

    10.  ~queue();

    11.  void insert(int i);

    12. 

    void remove();

    13.  bool isempty();

    14. 

    bool isfull();

    15. 

    };

    16. 

    queue::queue() {

    17.  front=0; rear=0; count =0;

    18. 

    }

    19. 

    void queue::insert(int i) {

    20.  if(isfull())

    21. 

    {

    22.  cout

  • 8/17/2019 CS255 Data Structure Lab Manual

    18/29

     D 2012 

    C D F I 18 

    Queue ADT –linked list implementations 

    AIM: To write a program for linked list implementation of queue The main advantage in linked representation is that we need not have to worry about

    sizeof the queue. As in linked organization we can create as many nodes as we want

    so there will not be a queue full condition at all. The queue using linked list will be

    very much similar to a linkedlist. The only difference between the two is in queue, the

    left most nodes is called front node andthe right most node is called rear node. We are

    only allowed to remove an item from the front node. On the other hand, we only can

    insert new items at the rear of the list only.

    ENQUEUE OPERATION1)  Get the element to be inserted

    2)  Create a new node.

    Node * p;

    P = ( node * ) mallloc (size of ( node ) );

    P -> data = x;

    P -> next = NULL

    3)  If ( rear ! = NULL)

    Rear -> next = p;

    Rear = p;

    4) 

    ElseFront = p;

    Rear = p;

    DEQUEUE OPERATION

    1)  Check the condition whether the queue is empty or not.

    2)  If ( front = = NULL && rear = = NULL)

     //we cannot delete element from the queue.

    Cout next

    Delete (p)

  • 8/17/2019 CS255 Data Structure Lab Manual

    19/29

     D 2012 

    C D F I 19 

    Lab Question:Write a C++ code to implement enqueue and dequeue using array.

    1.  #

    2.  #

    3.  ;

    4. 

    5.

     

    ;

    6.  *;

    7.  ;

    8. 

    9.  :

    10.  *;

    11.  *;

    12. :

    13. 

    ();

    14.  ();

    15. 

    ();16.  ();

    17. ;

    18. ::()

    19.  = ;

    20.  = ;

    21. 

    22.  ::()

    23.  ;

    24.  * = ;

    25. ;27. > = ;

    28. > = ;

    29.  ( == )

    30. 

    = ;

    31. 

    32. > = ;

    33. 

    34.  = ;

    35. 

    36.  ::()

    37. 

    * = ;38.

     

    ( == )

    39. 

  • 8/17/2019 CS255 Data Structure Lab Manual

    20/29

     D 2012 

    C D F I 20 

    51. 

  • 8/17/2019 CS255 Data Structure Lab Manual

    21/29

     D 2012 

    C D F I 21 

    Binary Tree

    Aim: Implement the Binary Tree Traversal

    Procedure: 

    1) Make the structure of Binary Tree Traversal for integer numbers.

    struct node {

    int data;

    struct node* left;

    struct node* right;

    }

    2) implement inorder(), preorder() and postorder() traversal for it.Preorder1)  Visit the root.

    2)  Traverse the left subtree.

    3)  Traverse the right subtree.

    Inorder1)  Traverse the left subtree.

    2)  Visit the root.

    3)  Traverse the right subtree.

    Postorder1)  Traverse the left subtree.

    2) 

    Traverse the right subtree.3)  Visit the root.

    Lab Question:Write a C++ code to implement enqueue and dequeue using array.

    1.  #

    2.  #

    3.  ;

    4. 

    B

    5. 

    6. 

    :7. 

    8. 

    9.  * ;

    10. * ;

    11.  ;

    12. 

    ;

    13. * ;

    14. :

    15. B()

    16. 

    17.  = ;

    18. 

  • 8/17/2019 CS255 Data Structure Lab Manual

    22/29

     D 2012 

    C D F I 22 

    19.  E() ==;

    20.  ();

    21.  (*);

    22.  ();

    23. 

    (*);24.  ();

    25.  (*);

    26. 

    ();

    27.  ();

    28. ;

    29.  B::()

    30. 

    31.  ();

    32. 

    33.  B::(* )

    34. 

    35.  ( != )

    36. 

    37.  (>) (>);

    38. 

  • 8/17/2019 CS255 Data Structure Lab Manual

    23/29

     D 2012 

    C D F I 23 

    64. 

    65.  (>) (>);

    66.  (>) (>);

    67. 

  • 8/17/2019 CS255 Data Structure Lab Manual

    24/29

     D 2012 

    C D F I 24 

    Binary Search Tree

    AIM: To write a program that implement the basic operations of a Binarysearch tree.

    A binary search tree is a binary tree in which the data in the nodes is ordered in the

    following way. Starting at any given node, the data in any nodes of its left subtree

    must all be less than the item in the given node, and the data in any nodes of its right

    subtree must be greater than or equal to the data in the given node. Of course, all of

    this implies that the data items can be ordered by some sort of less than relationship.

    For numbers this can obviously be done. For strings, alphabetical ordering is often

    used. For records of data, a comparison based on a particular field (the key field) is

    often used.

    A binary search tree can be traversed in same methods that are used to traverse any

    binary tree, inorder, preorder, and postorder.

    BST Basic operations: Insertion, Deletion

    Insertion:

    To insert a new node into the BST

    •  Proceed down the tree as you would with a find

    •  insert X at the last spot on the path traversed

    Deletion:

      Case 1: the node is a leaf  

      Delete it immediately

     

    Case 2: the node has one child 

      Adjust a pointer from the parent to bypass that node

      Case 3: the node has 2 children

      Replace the key of that node with the minimum element at the right

    subtree

      Delete that minimum element

      Has either no child or onlyone child because if it has a left

    child, that left child would be smaller and would have been

    chosen. So invoke case 1 or 2.

  • 8/17/2019 CS255 Data Structure Lab Manual

    25/29

     D 2012 

    C D F I 25 

    Lab Question:Write a C++ code to implement binary search tree insertion and

    deletion operations.

    1.  #

    2.  #

    3. 

    ;

    4.  B

    5. 

    6.  :

    7. 

    8. 

    9.  * ;

    10. * ;

    11.  ;

    12. 

    ;13. * ;

    14. :

    15. B()

    16. 

    17.  = ;

    18. 

    19.  E() ==;

    20.  ();

    21. 

    ();22. ;

    23.  B::( )

    24. 

    25. * = ;

    26. * ;

    27. > = ;

    28. > = ;

    29. 

    > = ;30.  = ;

    31. // ?

    32. (E()) = ;

    33. 

    34. 

    35. //: A

    36. * ;

    37.  = ;

    38. // F '

    39. 

    ()40. 

    41.  = ;

  • 8/17/2019 CS255 Data Structure Lab Manual

    26/29

     D 2012 

    C D F I 26 

    42. (> > >) = >;

    43.  = >;

    44. 

    45. (> < >)

    46. 

    > = ;47. 

    48. > = ;

    49. 

    50. 

    51.  B::( )

    52. 

    53. //

    54.  = ;

    55. 

    (E())56. 

    57. ;

    74.  = >;

    75. 

    76. 

    77. (!)

    78. 

  • 8/17/2019 CS255 Data Structure Lab Manual

    27/29

     D 2012 

    C D F I 27 

    84. //

    85. ((> == && > != ) (> !=

    86. && > == ))

    87. 

    88. 

    (> == && > != )89. 

    90. (> == )

    91. 

    92. > = >;

    93.  ;

    94. 

    95. 

    96. 

    97. > = >;

    98. 

    ;99. 

    100. 

    101.  // ,

    102. 

    103.  (> == )

    104. 

    105.  > = >;

    106.  ;

    107. 

    108. 

    109. 

    110.  > = >;

    111.  ;

    112. 

    113. 

    114.  ;

    115. 

    //'

    ( > == && > == )

    116. 

    117. 

    (> == ) > = ;

    118.  > = ;

    .  ;

    .  ;

    119. 

    120.  // 2

    121.  //

    122.  (> != && > != )

    123. 

    124.  * ;

    125.  = >;

  • 8/17/2019 CS255 Data Structure Lab Manual

    28/29

     D 2012 

    C D F I 28 

    126.  ((> == ) && (> == ))

    127. 

    128.  = ;

    129.  ;

    130.  > = ;

    131. 

    132.  //

    133. 

    134.  // '

    135.  //

    136.  ((>)> != )

    137. 

    138.  * ;

    139.  * ;

    140. 

    = >;141. 

    = (>)>;

    142.  (> != )

    143. 

    144.  = ;

    145.  = >;

    146. 

    147.  > = >;

    148.  ;

    149.  > = ;

    150. 

    151. 

    152. 

    153.  * ;

    154.  = >;

    155.  > = >;

    156.  > = >;

    157.  ;

    158. 

    159. 

    160.  ;

    161. 

    162. 

    163. 

    164.  ()

    165. 

    166.  B ;

    167.  ,,1;

    168.  (1)

    169. 

    170. 

  • 8/17/2019 CS255 Data Structure Lab Manual

    29/29

     D 2012 

    175.