Recent In Internet

Data Structures 

In C++, a data structure refers to a way of organizing and storing data in a computer program. C++ provides several built-in data structures such as arrays, linked lists, stacks, queues, and trees. Additionally, programmers can create their own data structures using C++ classes.


1. Arrays

Arrays are a fundamental data structure in C++. An array is a collection of elements of the same data type stored in contiguous memory locations. Elements in an array can be accessed using an index. The first element has an index of 0, and the last element has an index of n-1, where n is the size of the array.


2. Linked Lists

A linked list is a dynamic data structure that consists of a sequence of nodes, each containing an element and a pointer to the next node. The first node is called the head, and the last node is called the tail. Linked lists can be singly linked, where each node points to the next node, or doubly linked, where each node points to both the next and previous nodes.


3. Stacks

A stack is a data structure that follows the Last In First Out (LIFO) principle. Elements are added and removed from the top of the stack. C++ provides a built-in stack container that supports push, pop, and top operations.


4. Queues

A queue is a data structure that follows the First In First Out (FIFO) principle. Elements are added at the rear end of the queue and removed from the front end. C++ provides a built-in queue container that supports push, pop, and front operations.


5. Trees

A tree is a hierarchical data structure that consists of nodes connected by edges. The topmost node is called the root, and nodes with no children are called leaves. C++ provides several tree data structures, including binary trees, AVL trees, and Red-Black trees.


6. Classes

In C++, programmers can create their own data structures using classes. A class is a user-defined data type that can contain data members and member functions. Data members can be any of the built-in data types or other user-defined data types. Member functions can be used to manipulate the data members and provide access to them.


In conclusion, C++ provides a wide range of built-in data structures that can be used to store and manipulate data efficiently. Additionally, programmers can create their own data structures using classes to suit their specific needs. 

Here are some example programs for each of the data structures mentioned:


1. Arrays:

Code

#include <iostream>

using namespace std;


int main() {

  // Declare an array of 5 integers

  int arr[5] = {1, 2, 3, 4, 5};


  // Access the second element of the array

  cout << "The second element of the array is: " << arr[1] << endl;


  // Modify the third element of the array

  arr[2] = 6;

  cout << "The third element of the array is now: " << arr[2] << endl;


  return 0;

}

----------------------------------------------------------------------------------------------------------------------------------


2. Linked Lists:

Code

#include <iostream>

using namespace std;


// Define the structure of a linked list node

struct Node {

  int data;

  Node* next;

};


int main() {

  // Create three linked list nodes

  Node* head = new Node;

  Node* second = new Node;

  Node* third = new Node;


  // Assign data values and link the nodes

  head->data = 1;

  head->next = second;


  second->data = 2;

  second->next = third;


  third->data = 3;

  third->next = NULL;


  // Traverse the linked list and print the data

  Node* current = head;

  while (current != NULL) {

    cout << current->data << " ";

    current = current->next;

  }


  return 0;

}

-----------------------------------------------------------------------------------------------------------------------------------------


3. Stacks:

Code

#include <iostream>

#include <stack>

using namespace std;


int main() {

  // Create a stack of integers

  stack<int> s;


  // Push three integers onto the stack

  s.push(1);

  s.push(2);

  s.push(3);


  // Print the top element of the stack

  cout << "The top element of the stack is: " << s.top() << endl;


  // Pop the top element off the stack

  s.pop();

  cout << "The top element of the stack is now: " << s.top() << endl;


  return 0;

}

-----------------------------------------------------------------------------------------------------------------------------------------

4. Queues:

Code

#include <iostream>

#include <queue>

using namespace std;


int main() {

  // Create a queue of integers

  queue<int> q;


  // Push three integers onto the queue

  q.push(1);

  q.push(2);

  q.push(3);


  // Print the front element of the queue

  cout << "The front element of the queue is: " << q.front() << endl;


  // Pop the front element off the queue

  q.pop();

  cout << "The front element of the queue is now: " << q.front() << endl;


  return 0;

}

--------------------------------------------------------------------------------------------------------------------------------------

5. Trees:

Code

#include <iostream>

using namespace std;


// Define the structure of a binary tree node

struct Node {

  int data;

  Node* left;

  Node* right;

};


int main() {

  // Create a binary tree with three nodes

  Node* root = new Node;

  root->data = 1;


  root->left = new Node;

  root->left->data = 2;


  root->right = new Node;

  root->right->data = 3;


  // Traverse the binary tree in pre-order and print the data

  cout << "Pre-order traversal: ";

  cout << root->data << " ";

  cout << root->left->data << " ";

  cout << root->right->data << endl;


  return 0;


Post a Comment

أحدث أقدم