Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Data Structures

Data structures are efficient ways of storing and organizing data in computer memory, divided into linear and non-linear types. Linear data structures include arrays, stacks, queues, and linked lists, each with specific operations for data management. Stacks follow a Last In First Out (LIFO) approach, while queues use a First In First Out (FIFO) method, and linked lists allow dynamic memory allocation without contiguous storage.

Uploaded by

rogitha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Data Structures

Data structures are efficient ways of storing and organizing data in computer memory, divided into linear and non-linear types. Linear data structures include arrays, stacks, queues, and linked lists, each with specific operations for data management. Stacks follow a Last In First Out (LIFO) approach, while queues use a First In First Out (FIFO) method, and linked lists allow dynamic memory allocation without contiguous storage.

Uploaded by

rogitha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Data Structures

The collection of data elements that are stored in memory is known as data structure. It is considered as an
efficient way of storing and organizing data inside the computer memory.

Types of Data Structure:

Data structures are divided into 2 types. They are

1. Linear Data Structure.

2. Non-Linear Data Structure.

1. Linear Data Structure:

A data structure in which all the data elements are stored in sequential manner is called a Linear Data Structure.
There are different types of Linear Data structure available in C++. They are

1. Array

2. Stack

3. Queue

4. Linked List

1. Array: An Array is a collection of data elements with same data types. It is a variable that is capable of
holding fixed values in adjacent memory locations.

A0 A1 A2 -- --- --- --- --- --- An

Data
values
2 30 40 50 60 70 80 90 100
10 0

Stack in C++

A stack is a linear data structure that stores a set of elements in a sequential manner. In stack, new elements can
be inserted and the existing elements are deleted to/ form only single end called TOP.

Stack is an ordered collection of homogeneous elements that follow LIFO (Last In First Out) procedure in which,
the element that is inserted last is deleted first. The various operations performed on stack are as follow.

1. Push

2. Pop
1. Push: the method of inserting an element onto a stack is called “PUSH” operation. It adds a new element
onto top of the stack.

2. POP: The method of deleting an element from a stack is called “POP” operation. It delete the

top most element present on the stack.

The position where the above two operations are performed is called “TOP”. The numbers of elements which are
present in stack are called “ITEMS”. The representation of stack as follows below

 Size of stack: 3

Refer to the following image for more information about the operations performed in the code.
Queue Data Structure In C++

The queue is a basic data structure just like a stack. In contrast to stack that uses the LIFO approach, queue uses
the FIFO (first in, first out) approach. With this approach, the first item that is added to the queue is the first item
to be removed from the queue. Just like Stack, the queue is also a linear data structure.

The queue can be viewed as a set or collection of elements as shown below. The elements are arranged linearly.

We have two ends i.e. “front” and “rear” of the queue. When the queue is empty, then both the pointers are set
to -1.

The “rear” end pointer is the place from where the elements are inserted in the queue. The operation of
adding /inserting elements in the queue is called “enqueue”.

The “front” end pointer is the place from where the elements are removed from the queue. The

operation to remove/delete elements from the queue is called “dequeue”.

When the rear pointer value is size-1, then we say that the queue is full. When the front is null, then the queue is
empty.

Basic Operations

The queue data structure includes the following operations:

 EnQueue: Adds an item to the queue. Addition of an item to the queue is always done at the rear of
the queue.

 DeQueue: Removes an item from the queue. An item is removed or de-queued always from the
front of the queue.

 isEmpty: Checks if the queue is empty.

 isFull: Checks if the queue is full.

 peek: Gets an element at the front of the queue without removing it.

Enqueue

In this process, the following steps are performed:

 Check if the queue is full.

 If full, produce overflow error and exit.

 Else, increment „rear‟.

 Add an element to the location pointed by „rear‟.

 Return success.

Dequeue
Dequeue operation consists of the following steps:

 Check if the queue is empty.

 If empty, display an underflow error and exit.

 Else, the access element is pointed out by „front‟.

 Increment the „front‟ to point to the next accessible data.

 Return success.

Next, we will see a detailed illustration of insertion and deletion operations in queue.

Illustration

This is an empty queue and thus we have rear and empty set to -1.

Next, we add 1 to the queue and as a result, the rear pointer moves ahead by one location.

In the next figure, we add element 2 to the queue by moving the rear pointer ahead by another increment.

In the following figure, we add element 3 and move the rear pointer by 1.
At this point, the rear pointer has value 2 while the front pointer is at the 0 th location.

Next, we delete the element pointed by the front pointer. As the front pointer is at 0, the element that is deleted
is 1.

Thus the first element entered in the queue i.e. 1 happens to be the first element removed from the queue. As a
result, after the first dequeue, the front pointer now will be moved ahead t0 the next location which is 1.

Linked List Data Structure In C++

A linked list is a linear dynamic data structure to store data items. We have already seen arrays in our previous
topics on basic C++. We also know that arrays are a linear data structure that store data items in contiguous
locations. Unlike arrays, the linked list does not store data items in contiguous memory locations. A linked list
consists of items called “Nodes” which contain two parts. The first part stores the actual data and the second
part has a pointer that points to the next node. This structure is usually called “Singly linked list”.

The following diagram shows the structure of a singly linked list.

As shown above, the first node of the linked list is called “head” while the last node is called “Tail”. As we see, the
last node of the linked list will have its next pointer as null since it will not have any memory address pointed to.

Since each node has a pointer to the next node, data items in the linked list need not be stored at contiguous
locations. The nodes can be scattered in the memory. We can access the nodes anytime as each node will have
an address of the next node.
We can add data items to the linked list as well as delete items from the list easily. Thus it is possible to grow or
shrink the linked list dynamically. There is no upper limit on how many data items can be there in the linked list.
So as long as memory is available, we can have as many data items added to the linked list.

Apart from easy insertion and deletion, the linked list also doesn‟t waste memory space as we need not specify
beforehand how many items we need in the linked list. The only space taken by linked list is for storing the
pointer to the next node that adds a little overhead.

Operations

Just like the other data structures, we can perform various operations for the linked list as well. But unlike arrays,
in which we can access the element using subscript directly even if it is somewhere in between, we cannot do the
same random access with a linked list.

In order to access any node, we need to traverse the linked list from the start and only then we can access the
desired node. Hence accessing the data randomly from the linked list proves to be expensive.

We can perform various operations on a linked list as given below:

#1) Insertion

Insertion operation of linked list adds an item to the linked list. Though it may sound simple, given the structure
of the linked list, we know that whenever a data item is added to the linked list, we need to change the next
pointers of the previous and next nodes of the new item that we have inserted. The second thing that we have to
consider is the place where the new data item is to be added.

There are three positions in the linked list where a data item can be added. #1) At the beginning of the linked
list

A linked list is shown below 2->4->6->8->10. If we want to add a new node 1, as the first node of the list, then the
head pointing to node 2 will now point to 1 and the next pointer of node 1 will have a memory address of node 2
as shown in the below figure.

Thus the new linked list becomes 1->2->4->6->8->10.

#2) After the given Node

Here, a node is given and we have to add a new node after the given node. In the below-linked list a->b->c->d -
>e, if we want to add a node f after node c then the linked list will look as follows:
Thus in the above diagram, we check if the given node is present. If it‟s present, we create a new
node f. Then we point the next pointer of node c to point to the new node f. The next pointer of
the node f now points to node d.

#3) At the end of the Linked List

In the third case, we add a new node at the end of the linked list. Consider we have the same
linked list a->b->c->d->e and we need to add a node f to the end of the list. The linked list will
look as shown below after adding the node.

Thus we create a new node f. Then the tail pointer pointing to null is pointed to f and the next
pointer of node f is pointed to null. We have implemented all three types of insert functions in
the below C++ program.

In C++, we can declare a linked list as a structure or as a class. Declaring linked list as a structure
is a traditional C-style declaration. A linked list as a class is used in modern C++, mostly while
using standard template library.

You might also like