Answer to Question #182585 in C++ for Mwansa Kunda

Question #182585

Elaborate at least five (5) real life applications with examples for each in form of source codes, the importance and usage of data structures


1
Expert's answer
2021-04-17T18:51:04-0400

Data structures are used to store different types of data types in a single data type as of data structure. For example, a book has different attributes as follows:

 

Attribute                                                        Data Type

Titile                                                               string

Author                                                            string

Edition                                                            int                   

No. of pages                                                   int

ISDN No                                                         string

Library ID                                                       string

 

Now, all of the above attributes can be combined intoa single data type called as structure and can be declared as below:

 

Struct Book

{

           String Title;

           String Authors;

           Int Edition;

Int No_of_Pages;

           String ISDN_No;;

           String ID;

}

 

If there are no. of books (100, say), then, array of Book can be declared as:

Struct Book B[100];

In this way, we have a data type as Book that has no. of data types as declared above. Each attribute canbe accessed using the dot operator.

                       

Data structures facilitate computer programming to a great extent as new types of data type can be formed to suit the application requirement. Array of new data types can be formed. Access to array of data structures is linear and can be accesed using loops. Further arithmatic operations can be applied on indexing while ijsearch algorithjmsor traversing thoughout the data base.

 

There are different data structures available in C/C++ or in any computer programming language as listed below:

1.      Arrays

2.      Structure

3.      Linked List

4.      Stack

5.      Unions

6.      Queue

7.      Graph

8.      Tree

9.      Hash Table

10.  Heap



Example: Arrays

main(void)

{

               Int Score[10];

}

Example: Structures

Struct Book

{

               String Author;

String Title;

Int No_of_pages;

Float Price;

}

Example Linked List

struct node

{

   int data;

   struct node *next;

};

 

Example Stack:

// Head pointer always points to first element of the linked list

struct node *head = NULL;


class Stack {

    int top;

 

public:

    int a[MAX]; // Maximum size of Stack

 

    Stack() { top = -1; }

    bool push(int x);

    int pop();

    int peek();

    bool isEmpty();

};

 

bool Stack::push(int x)

{

    if (top >= (MAX - 1)) {

        cout << "Stack Overflow";

        return false;

    }

    else {

        a[++top] = x;

        cout << x << " pushed into stack\n";

        return true;

    }

}

Example Union:

union car
{
  char name[50];
  int price;
};
 
int main()
{
  union car car1, car2, *car3;
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS