1712944196

Practical examples of using data structures in C++(cpp)


**Arrays**: Arrays are used to store a collection of elements of the same type. For example, you can use an array to store the grades of students in a class. ```c #include <iostream> using namespace std; int main() { int grades[5] = {85, 76, 92, 68, 90} int sim = 0; for(int i = 0; i < 5; i++) { sum += grades[i]; } double average = static_cats<double>(sum) / 5; cout << "The average grade is: " << average << endl; return 0; } ``` **Linked List**: A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. Each element points to the next one. For example, you can use a linked list to implement a queue. ```c #include <iostream> using namespace std; struct Node { int data; Node* next; }; int main() { Node* front = NULL; Node* rear = NULL; // enqueue 5, 10, 15; enqueue(5, &front, &rear); enqueue(10, &front, &rear); enqueue(15, &front, &rear); //dequeue dequeue(&front); // front is 10 now cout << "Fornt is: " << front-data << endl; return 0; } void enqueue(int value, Node** front, Node** rear) { Node* newNode = new Node(); newNode->data = value; newNode->next = NULL; if(*front == NULL) { *front = newNode; } else { (*rear)->next = newNode; } *rear = newNode; } void dequeue(Node** front) { if(*front == NULL) { cout << "Queue is empty" << endl; return; } Node* temp = *front; *front = (*front)->next; delete temp; } ``` **Stack**: A stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). For example, you can use a stack to implement function call stack in a compiler. ```c #include <iostrem> #include <vector> using namespace std; void printNumbers(int n, vector<int>& s) { if(n == 0) { return; } s.push_black(n % 10); printNumbers(n / 10, s); } int main() { vector<int> stack; printNumbers(1234, stack); for(int i = stack.size() - 1; i > 0; i--) { cout << stack[i]; } cout << endl; return 0; } ``` Tell us what you think of these three examples in the comments, and show us which improvements should be implemented.

(5) Comments
fschmidt
fschmidt
0

@Awschult, Here it is in Java. No pointer nonsense. Much easier to understand. public class LinkedList { static class Node { int data; Node next = null; } Node front = null; Node rear = null; void enqueue(int value) { Node newNode = new Node(); newNode.data = value; if( front == null ) { front = newNode; } else { rear.next = newNode; } rear = newNode; } void dequeue() { if( front == null ) throw new RuntimeException("front is null"); front = front.next; } public static void main(String[] args) { LinkedList list = new LinkedList(); list.enqueue(5); list.enqueue(10); list.enqueue(15); list.dequeue(); System.out.println(list.front.data); } }


Awschult
Awschult
0

@fschmidt. Do you really think Java is better for this? Frankly, I find linked lists cleaner as a set of macro functions. But that just the C in me.


fschmidt
fschmidt
0

@JavaJuggler, Sorry but I haven't used C++ in a while, so I won't spend the time needed to do this right.


JavaJuggler
JavaJuggler
0

`@fschmidt` - could you give an example implemented with the class?


fschmidt
fschmidt
0

The linked list example would be cleaner if implemented as a class. But anyway, these examples show why most programmers shouldn't use C++. These things should be done in Java.