0% found this document useful (0 votes)
9 views

Data Structures and Algorithms

About data structure

Uploaded by

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

Data Structures and Algorithms

About data structure

Uploaded by

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

Data Structures and

Algorithms C++
Data Structures and Algorithms C++ – means arranging or

organizing the elements in a particular way. When we say we have

to arrange elements, those elements can be organized in different

forms. For example, socks can be arranged in various different

ways. You can just keep it in your cupboard all messed up. Or you

can keep it neatly folded. The best way can be folding and arranging

them color-wise. So for searching a particular pair of socks, the third

arrangement is perfect.

In a similar way of organization of socks, Data can be also organized

in different ways or forms. These different ways of organizing data

are called as Data structures. Let’s see a formal definition of a data

structure and the data structures and algorithms basics.

Data Structures And Algorithms C++


The logical or mathematical model of a particular

organization of data.

OR
It is a particular way of organizing data in a computer so

that it can be used.

Similarly to socks; different organization of list data structures and

algorithms C++ available is –

1. Array

2. Linked List

3. Stack

4. Queue

5. Tree

6. Graph

7. Hash Table

8. Heap

9. Records

10. Tables

These data structures and algorithms C++ are very important while

programming. A good programmer always gives emphasis on data

structure rather than code. Each programming language works on

various data structures and algorithms in C++. Data structures that

are available in C++ are as follows.


Popular Course in this category

C++ Training (4 Courses, 5 Projects, 4 Quizzes) 4 Online Courses | 5


Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4
Quizzes with Solutions
4.5 (6,984 ratings)
Course Price
$49 $499
View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)C Programming Training (3 Courses, 5 Project)

1. Array

2. Linked List

3. Stack

4. Queue

5. Tree

6. Graph

7. Hash Table

8. Heap

Let’s discuss this one by one:


#1 Array

Array is a simplest type of data structure and algorithms C++. The

array is defined as a Fix-size sequential collection of data elements

of the same data type. E.g. a0=12, a1=21,a2=14,a3=15….We can

represent one-dimensional array as shown in figure:

Where

0,1,2,3…..n is called subscript or index

a[1],a[2],…a[n] is called subscript variable

It can be 1-Dimensional, 2-Dimensional, 3-Dimensional and so on

multi-Dimensional.

In memory array stores into contiguous memory locations.

The lowest address corresponds to the first element


The highest address corresponds to the last element

We can declare 1-D (1-Dimensional) array in C++ as follows

dataType arrayName[arraySize];

E.g. int num[5];

Initializing Array in C++

num={23, 10, 12, 3, 6};

We can combine declaration and initialization into a single

statement as follows.

int num={23, 10, 12, 3, 6};

When we want to dynamically allocate the size of an array then we

should new operator as follows

int * a= new int[size];

The disadvantage of the array are insertion and deletion of

elements is slow as in ordered array and its fixed size storage.

#2 Linked List
List refers to a linear collection of items. A linked list is a series of

connected nodes (data element) as shown in figure 3. Header node

points to the first node of the list and the last node points to NULL

indicated byÆ. As each node contains at least.

1. A piece of data (any type)

2. Pointer to the next node in the list


Linked List is represented in memory using two arrays. One array

stores Information called info that is data to be stored and other

stores the next-pointer field called LINK that is an address of the

next node.

An advantage of a linked list over an array:

Both an array and a linked list are representations of a list of items

in memory. The important difference is the way in which the items

are linked together. The main limitation of the array is element

insertion into array and element deletion from the ordered array are

difficult as rest elements have to be move. Insertion and deletion of

elements from a linked list are very simple.


Note: Become a C++ Developer
Learn to design and customize programs for various platforms. Code, test,
debug, and implement software applications. Develop skills to ensure
applications run smoothly.
Types of Linked List are:
1. Singly linked list: contains only one linked field which holds the

address of next node in the list and info filed which holds

information to be stored.

2. Single Circular Linked List is a single list only but the last node

of the list contains the address of the first node instead of null. That

is content of head and next field of the last node are same.
3. The doubly linked list contains two linked field previous and

next. A previously linked field which holds an address of the

previous node in the list and next linked field holds the address of

the next node in the list and info filed holds the information to be a

store.

4. Double Circular Linked List is doubly linked list but next field

of the last node contains the address of the first node instead of

null.
Implementation of linked list in C++ involves the creation of

node, deletion of a node from the list, insertion of a newly created

node into the list and searching a node with a particular key.

Code for creation of the node is given as follows:

Inserting a node into the list involves three cases


1. Inserting a node at the beginning means inserting the newly

created node as starting node. For inserting a node at the beginning

first have created a new node and make new node point to old start,

and then update start to point to new node as shown in below

figure:

Code for inserting a node at the beginning:


2. Inserting a node at the tail means inserting the newly created

node as the last node. For inserting the node as a tail node have to

create a new node and make old last node point to the new node

and then update tail to point to new node.

3. Inserting a node at a given position involves the creation of

new temp node, Then have to find the position of insertion of the

newly created node.


Code for insertion of the node at a given a position:
Deleting a node from list involves removing a node from existing

list. Deletion of the node from link list is simple than inserting a

node into the list. In C++ code for deletion of the node is given as

follows:
Traversing a node with a particular key (value) from a list will

search a node from the list whose info will match with the key of a

given node. The following C++ code will traverse a list. data

structures and algorithms C++

You might also like