Docsity

DATA STRUCTURE AND ALGORITHM, Schemes and Mind Maps of Data Structures and Algorithms

Toggle sidebar
bg1
Basic data structures
Arrays:
Arrays are a collection of elements of the same type stored
in contiguous memory locations. Each element in an array
can be accessed by its index value, which starts from 0.
Arrays are commonly used to store and manipulate a fixed-
size sequence of elements.
For example, consider an array of integers that stores the
scores of students in a class:
int scores[5] = { 90, 85, 80, 95, 87 };
Here, we have an array named scores that contains 5
integer values. The first element in the array is scores[0],
which has a value of 90, and the last element is scores[4],
which has a value of 87.
pf3
pf4
pf5

Partial preview of the text

Download DATA STRUCTURE AND ALGORITHM and more Schemes and Mind Maps Data Structures and Algorithms in PDF only on Docsity!

Basic data structures

Arrays:

Arrays are a collection of elements of the same type stored in contiguous memory locations. Each element in an array can be accessed by its index value, which starts from 0. Arrays are commonly used to store and manipulate a fixed- size sequence of elements. For example, consider an array of integers that stores the scores of students in a class: int scores[5] = { 90, 85, 80, 95, 87 }; Here, we have an array named scores that contains 5 integer values. The first element in the array is scores[0], which has a value of 90, and the last element is scores[4], which has a value of 87.

Linked Lists:

Linked lists are a dynamic data structure that consists of a sequence of nodes, where each node stores a value and a pointer to the next node in the list. Linked lists are useful when the size of the data to be stored is not known in advance, and when insertions and deletions of elements are frequent. For example, consider a linked list of integers: struct Node { int data; struct Node* next; }; struct Node* head = NULL; struct Node* second = NULL; struct Node* third = NULL; head = (struct Node)malloc(sizeof(struct Node)); second = (struct Node)malloc(sizeof(struct Node)); third = (struct Node*)malloc(sizeof(struct Node)); head->data = 1;

Stacks:

A stack is a linear data structure that follows the Last-In- First-Out (LIFO) principle. In a stack, elements are added and removed from the same end, known as the top of the stack. Stacks are useful in applications that require a temporary storage of data, such as function calls in a computer program. For example, consider a stack of integers: #include std::stack myStack; myStack.push(10); myStack.push(20); myStack.push(30); while(!myStack.empty()){ std::cout << myStack.top() << " "; myStack.pop(); }

Here, we have a stack of integers that contains the values 1 0, 20, and 30, in that order. We can use the push() function to add elements to the top of the stack, and the pop() function to remove elements from the top of the stack. In this example, the values in the stack are printed in reverse order, because the last value pushed (30) is the first value popped.