0% found this document useful (0 votes)
4 views25 pages

Dsa Module 1

Module 1 introduces data structures and algorithms, explaining the organization of data into structures like fields, records, and files, and classifying data structures into primitive and non-primitive types. It covers linear and non-linear data structures, static and dynamic structures, and operations such as traversing, searching, inserting, and deleting. The module also discusses algorithms, their complexities, and asymptotic notations used for analyzing algorithm performance.

Uploaded by

ruchithabegum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views25 pages

Dsa Module 1

Module 1 introduces data structures and algorithms, explaining the organization of data into structures like fields, records, and files, and classifying data structures into primitive and non-primitive types. It covers linear and non-linear data structures, static and dynamic structures, and operations such as traversing, searching, inserting, and deleting. The module also discusses algorithms, their complexities, and asymptotic notations used for analyzing algorithm performance.

Uploaded by

ruchithabegum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Module 1- Introduction to Data Structures and Algorithms

Data may be organized in many different ways. The logical or mathematical model of a
particular organization of data is called a data structure.
The choice of a particular data model depends on the two considerations
1. It must be rich enough in structure to mirror the actual relationships of the data in the real
world. 2. The structure should be simple enough that one can effectively process the data
whenever necessary.
Basic Terminology: Elementary Data Organization:
Data: Data are simply values or sets of values.
Data items: Data items refers to a single unit of values. Data items that are divided into sub-
items are called Group items.
Ex: An Employee Name may be divided into three sub items- first name, middle name, and
last name.
Data items that are not able to divide into sub-items are called Elementary items.
Ex: SSN Entity: An entity is something that has certain attributes or properties which may be
assigned values. The values may be either numeric or non-numeric.
Ex: Attributes- Names, Age, Sex, SSN Values- Rohland Gail, 34, F, 134-34-5533
Entities with similar attributes form an entity set. Each attribute of an entity set has a range of
values, the set of all possible values that could be assigned to the particular attribute.

The term “information” is sometimes used for data with given attributes, of, in other words
meaningful or processed data.

Field is a single elementary unit of information representing an attribute of an entity.

Record is the collection of field values of a given entity.

File is the collection of records of the entities in a given entity set.

Each record in a file may contain many field items but the value in a certain field may uniquely
determine the record in the file. Such a field K is called a primary key and the values k1, k2,
….. in such a field are called keys or key values.

Records may also be classified according to length. A file can have fixed-length records or
variable-length records.

• In fixed-length records, all the records contain the same data items with the same amount of
space assigned to each data item.

• In variable-length records file records may contain different lengths.

Example: Student records have variable lengths, since different students take different numbers
of courses.

Variable-length records have a minimum and a maximum length.

1
Module 1- Introduction to Data Structures and Algorithms

The above organization of data into fields, records and files may not be complex enough to
maintain and efficiently process certain collections of data. For this reason, data are also
organized into more complex types of structures. The study of complex data structures includes
the following three

steps: 1. Logical or mathematical description of the structure

2. Implementation of the structure on a computer

3. Quantitative analysis of the structure, which includes determining the amount of memory
needed to store the structure and the time required to process the structure.

CLASSIFICATION OF DATA STRUCTURES

Data structures are generally classified into


• Primitive data Structures
• Non-primitive data Structures
The primitive data structures in C are those basic data structures that are already defined
in the C language. Basic data types such as integer, real, character and Boolean are
known as Primitive Data Structures. These data types consist of characters that cannot
be divided and hence they also called simple data types.
Linear data structures in C store the data in a sequential or linear fashion. The memory
location of each element stored can be accessed sequentially. The elements may not be
present adjacently in the memory, however, each element is attached to the next element
in some way. Example - arrays, linked lists, stacks, etc.

2. Non- Primitive data Structures: Non-primitive data structures are those data
structures which are created using primitive data structures. Examples of non-primitive data
structures is the processing of complex numbers, linked lists, stacks, trees, and graphs.

Non-linear data structures in C store the data in a non-sequential manner. The data is
stored in multiple levels. The implementation of non-linear data structures is more complex
than linear data structures. Example - graphs, trees.

2
Module 1- Introduction to Data Structures and Algorithms

Based on the structure and arrangement of data, non-primitive data structures is further
classified into 1. Linear Data Structure 2. Non-linear Data Structure

1. Linear Data Structure: A data structure is said to be linear if its elements form a sequence or
a linear list. There are basically two ways of representing such linear structure in memory. 1.
One way is to have the linear relationships between the elements represented by means of
sequential memory location. These linear structures are called arrays. 2. The other way is to
have the linear relationship between the elements represented by means of pointers or links.
These linear structures are called linked lists. The common examples of linear data structure
are Arrays, Queues, Stacks, Linked lists

2. Non-linear Data Structure: A data structure is said to be non-linear if the data are not arranged
in sequence or a linear. The insertion and deletion of data is not possible in linear fashion. This
structure is mainly used to represent data containing a hierarchical relationship between
elements. Trees and graphs are the examples of non-linear data structure.

On the basis of size, the data structures in C can also be classified as:

• Static Data Structures

The static nature of data structures is exhibited in the memory allocated to them. The size of
such data structures is fixed as the memory is allocated to them during the compile time.
However, the values of the elements stored are not static and can be modified at any time.
Example - Array.

• Dynamic Data Structures

The dynamic data structures in C are capable of resizing themselves during the run time of the
program. The memory space allocated to such data structures can be modified (increased or
decreased), thus providing more flexibility to manipulate the data stored in them. Example -
Linked List.

Arrays: The simplest type of data structure is a linear (or one dimensional) array. A list of a
finite number n of similar data referenced respectively by a set of n consecutive numbers,
usually 1, 2, 3 . . . . . . . n. if A is chosen the name for the array, then the elements of A are
denoted by subscript notation a1, a2, a3….. an or by the parenthesis notation A (1), A (2), A
(3) . . . . . . A (n) or by the bracket notation A [1], A [2], A [3] . . . . . . A [n]

Data Structures and Applications Linear arrays are called one-dimensional arrays because each
element in such an array is referenced by one subscript. A two-dimensional array is a collection
of similar data elements where each element is referenced by two subscripts.

Trees Data frequently contain a hierarchical relationship between various elements. The data
structure which reflects this relationship is called a rooted tree graph or a tree. Some of the
basic properties of tree are explained by means of examples

3
Module 1- Introduction to Data Structures and Algorithms

1. Stack: A stack, also called a fast-in first-out (LIFO) system, is a linear list in which insertions
and deletions can take place only at one end, called the top. This structure is similar in its
operation to a stack of dishes on a spring system as shown in fig. Note that new 4 dishes are
inserted only at the top of the stack and dishes can be deleted only from the top of the Stack.

2. Queue: A queue, also called a first-in first-out (FIFO) system, is a linear list in which
deletions can take place only at one end of the list, the "from'' of the list, and insertions can
take place only at the other end of the list, the “rear” of the list. This structure operates in much
the same way as a line of people waiting at a bus stop, as pictured in Fig. the first person in line
is the first person to board the bus. Another analogy is with automobiles waiting to pass through
an intersection the first car in line is the first car through.

3. Graph: Data sometimes contain a relationship between pairs of elements which is not
necessarily hierarchical in nature. For example, suppose an airline flies only between the cities
connected by lines in Fig. The data structure which reflects this type of relationship is called a
graph.

DATA STRUCTURES OPERATIONS The data appearing in data structures are processed
by means of certain operations.

The following four operations play a major role in this text:

1. Traversing: accessing each record/node exactly once so that certain items in the record may
be processed. (This accessing and processing is sometimes called “visiting” the record.)

2. Searching: Finding the location of the desired node with a given key value, or finding the
locations of all such nodes which satisfy one or more conditions.

3. Inserting: Adding a new node/record to the structure.

4. Deleting: Removing a node/record from the structure.

The following two operations, which are used in special situations:

1. Sorting: Arranging the records in some logical order (e.g., alphabetically according to some
NAME key, or in numerical order according to some NUMBER key, such as social security
number or account number)

2. Merging: Combining the records in two different sorted files into a single sorted file

4
Module 1- Introduction to Data Structures and Algorithms

Abstract Data Types

5
Module 1- Introduction to Data Structures and Algorithms

6
Module 1- Introduction to Data Structures and Algorithms

Advantage:

Recall that abstraction is the idea of separating what something is from how it works, by
separating interface from implementation. Previously, we saw procedural abstraction, which
applies abstraction to computational processes. With procedural abstraction, we use functions
based on their signature and documentation without having to know details about their
definition.

The concept of abstraction can be applied to data as well. An abstract data type
(ADT) separates the interface of a data type from its implementation, and it encompasses both
the data itself as well as functionality on the data. An example of an ADT is the string type in
C++, used in the following code:

string str1 = "hello";


string str2 = "jello";
cout << str1 << endl;
if (str1.length() == str2.length()) {
cout << "Same length!" << endl;
}

This code creates two strings and initializes them to represent different values, prints out one
of them, and compares the lengths of both – all without needing to any details about the
implementation of string . Rather, it relies solely on the interface provided by
the string abstraction

Algorithm
An algorithm is a finite set of instructions, those if followed, accomplishes a particular task. It
is not language specific, we can use any language and symbols to represent instructions.
The criteria of an algorithm
• Input: Zero or more inputs are externally supplied to the algorithm.
• Output: At least one output is produced by an algorithm.
• Definiteness: Each instruction is clear and unambiguous.

7
Module 1- Introduction to Data Structures and Algorithms

• Finiteness: In an algorithm, it will be terminated after a finite number of steps for all
different cases.
• Effectiveness: Each instruction must be very basic, so the purpose of those instructions
must be very clear to us.
Complexities of an Algorithm
The complexity of an algorithm computes the amount of time and spaces required by an
algorithm for an input of size (n). The complexity of an algorithm can be divided into two
types. The time complexity and the space complexity.
Time Complexity of an Algorithm
The time complexity is defined as the process of determining a formula for total time required
towards the execution of that algorithm. This calculation is totally independent of
implementation and programming language.
Space Complexity of an Algorithm
Space complexity is defining as the process of defining a formula for prediction of how much
memory space is required for the successful execution of the algorithm. The memory space is
generally considered as the primary memory.

8
Module 1- Introduction to Data Structures and Algorithms

9
Module 1- Introduction to Data Structures and Algorithms

Calculating time Complexity:

10
Module 1- Introduction to Data Structures and Algorithms

11
Module 1- Introduction to Data Structures and Algorithms

1)#include <stdio.h>

• void main()
• {
• int i, n = 5;
• for (i = 1; i <= n; i++) {
• printf("FACE Prep n");
• }}

complexity is O(n)
2) #include <stdio.h>
int main()
{
int a = 4;
int b = 6;
int c;
c = a + b;
printf(%d, c);
}

Time Complexity Calculation: The time complexity of the above-given program is O(1), as
this program consists of only assignment, arithmetic operations and all those will be executed
only once.

3) int count(int arr[], int n)


{
int sum = 0, i;

12
Module 1- Introduction to Data Structures and Algorithms

for(i = 0; i < n; i++) //Control statement


{
sum = sum + arr[i];
}
return sum;
}

final time complexity of the above-given snippet is O(n).

4) int i,j, n = 8;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
printf("FACE Prep");
}}}

Time Complexity Calculation: In the above snippet, the first & the second for loops get
executed n times individually. So the time complexity accounts to n*n = O(n2)

5) while(low<=high)
{
mid=(low+high)/2;
if(n<arr[mid])
high=mid-1;
elseif(n>arr[mid])
low=mid+1;
elsebreak;
}

Time Complexity Calculation: This is the algorithm of binary search. It breaks the given set
of elements into two halves and then searches for a particular element. Further, it keeps dividing
these two halves into further halves until each individual element is a set. All such algorithms
which work on the principle of recursive division of elements into halves will have a
O(Log n) complexity

13
Module 1- Introduction to Data Structures and Algorithms

Asymptotic Notations:

• Asymptotic Notations are mathematical tools used to analyze the performance of


algorithms by understanding how their efficiency changes as the input size grows.
• These notations provide a concise way to express the behavior of an algorithm's time or
space complexity as the input size approaches infinity.
• Rather than comparing algorithms directly, asymptotic analysis focuses on understanding
the relative growth rates of algorithms' complexities.
• It enables comparisons of algorithms' efficiency by abstracting away machine-specific
constants and implementation details, focusing instead on fundamental trends.
• Asymptotic analysis allows for the comparison of algorithms' space and time complexities
by examining their performance characteristics as the input size varies.
• By using asymptotic notations, such as Big O, Big Omega, and Big Theta, we can
categorize algorithms based on their worst-case, best-case, or average-case time or space
complexities, providing valuable insights into their efficiency.
There are mainly three asymptotic notations:

1. Big-O Notation (O-notation)

2. Omega Notation (Ω-notation)

3. Theta Notation (Θ-notation)

1. Theta Notation (Θ-Notation):


Theta notation encloses the function from above and below. Since it represents the upper and
the lower bound of the running time of an algorithm, it is used for analyzing the average-
case complexity of an algorithm.
.Theta (Average Case) You add the running times for each possible input combination and
take the average in the average case.
Let g and f be the function from the set of natural numbers to itself. The function f is said to
be Θ(g), if there are constants c1, c2 > 0 and a natural number n0 such that c1* g(n) ≤ f(n) ≤
c2 * g(n) for all n ≥ n0

Theta notation

14
Module 1- Introduction to Data Structures and Algorithms

2. Big-O Notation (O-notation):

Big-O notation represents the upper bound of the running time of an algorithm. Therefore, it
gives the worst-case complexity of an algorithm.
.It is the most widely used notation for Asymptotic analysis.
.It specifies the upper bound of a function.
.The maximum time required by an algorithm or the worst-case time complexity.
.It returns the highest possible output value(big-O) for a given input.
.Big-O(Worst Case) It is defined as the condition that allows an algorithm to complete
statement execution in the longest amount of time possible.

If f(n) describes the running time of an algorithm, f(n) is O(g(n)) if there exist a positive
constant C and n0 such that, 0 ≤ f(n) ≤ cg(n) for all n ≥ n0

It returns the highest possible output value (big-O)for a given input.


The execution time serves as an upper bound on the algorithm's time complexity.

3. Omega Notation (Ω-Notation):

Omega notation represents the lower bound of the running time of an algorithm. Thus, it
provides the best case complexity of an algorithm.
The execution time serves as a lower bound on the algorithm's time complexity.

It is defined as the condition that allows an algorithm to complete statement execution in


the shortest amount of time.

Let g and f be the function from the set of natural numbers to itself. The function f is said to be
Ω(g), if there is a constant c > 0 and a natural number n0 such that c*g(n) ≤ f(n) for all n ≥ n0

15
Module 1- Introduction to Data Structures and Algorithms

Arrays

Arrays is a linear data structure where all elements are arranged sequentially. It is a collection
of elements of same data type stored at contiguous memory locations. An array in C is a
fixed-size collection of similar data items stored in contiguous memory locations. It can be
used to store the collection of primitive data types such as int, char, float, etc., as well as derived
and user-defined data types such as pointers, structures, etc.

1. Array Declaration

Array declaration is the process of specifying the type, name, and size of the array. In C, we
have to declare the array like any other variable before using it.

data_type array_name[size];

16
Module 1- Introduction to Data Structures and Algorithms

The above statements create an array with the name array_name, and it can store a specified
number of elements of the same data type.

Example:
// Creates array arr to store 5 integer values.
int arr[5];
When we declare an array in C, the compiler allocates the memory block of the specified size
to the array name.

2. Array Initialization

Initialization in C is the process to assign some initial value to the variable. When the array is
declared or allocated memory, the elements of the array contain some garbage value. So, we
need to initialize the array to some meaningful values.

int arr[5] = {2, 4, 8, 12, 16};

The above statement creates an array arr and assigns the values {2, 4, 8, 12, 16} at the time of
declaration.

Accessing Array Elements

Array in C provides random access to its elements, which means that we can access any element
of the array by providing the position of the element, called the index.

Syntax:

The index values start from 0 and goes up to array_size-1. We pass the index inside square
brackets [] with the name of the array.

array_name [index];

where, index value lies into this range - (0 ≤ index ≤ size-1).

Write a C Program to perform operations on Arrays

#include <stdio.h>

int main() {
int arr[100], size, i, pos, value;

// Initial input
printf("Enter number of elements: ");
scanf("%d", &size);

17
Module 1- Introduction to Data Structures and Algorithms

printf("Enter %d elements:\n", size);


for(i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}

// Traversal
printf("\nArray elements are:\n");
for(i = 0; i < size; i++) {
printf("%d ", arr[i]);
}

// Insertion
printf("\n\nEnter position to insert (0 to %d): ", size);
scanf("%d", &pos);
printf("Enter value to insert: ");
scanf("%d", &value);

for(i = size; i > pos; i--) {


arr[i] = arr[i - 1];
}
arr[pos] = value;
size++;

printf("Array after insertion:\n");


for(i = 0; i < size; i++) {
printf("%d ", arr[i]);
}

// Deletion
printf("\n\nEnter position to delete (0 to %d): ", size - 1);
scanf("%d", &pos);

for(i = pos; i < size - 1; i++) {


arr[i] = arr[i + 1];
}
size--;

printf("Array after deletion:\n");


for(i = 0; i < size; i++) {
printf("%d ", arr[i]);
}

return 0;
}

Multi-dimensional array in C
A multi-dimensional array in C can be defined as an array that has more than one dimension.
Having more than one dimension means that it can grow in multiple directions. Some popular
multidimensional arrays include 2D arrays which grows in two dimensions, and 3D arrays
which grows in three dimensions.

18
Module 1- Introduction to Data Structures and Algorithms

Syntax

The general form of declaring N-dimensional arrays is shown below:

type arrName[size1][size2]....[sizeN];

• type: Type of data to be stored in the array.

• arrName: Name assigned to the array.

• size1, size2,..., sizeN: Size of each dimension.

Examples:

// Two-dimensional array
int two_d[10][20];

// Three-dimensional array:
int three_d[10][20][30];
Types of Multidimensional Arrays
In C, there can be many types of arrays depending on their dimensions but two of them are
most commonly used:
1. 2D Array - Two Dimensional
2. 3D Array - Three Dimensional
2D Arrays in C
A two-dimensional array or 2D array is the simplest form of the multidimensional array.
We can visualize a two-dimensional array as one-dimensional arrays stacked vertically
forming a table with 'm' rows and 'n' columns. In C, arrays are 0-indexed, so the row number
ranges from 0 to (m-1) and the column number ranges from 0 to (n-1).

Declaration of 2D Array

A 2D array with m rows and n columns can be created as:

type arr_name[m][n];

19
Module 1- Introduction to Data Structures and Algorithms

For example, we can declare a two-dimensional integer array with name 'arr' with 10 rows
and 20 columns as:

int arr[10][20];

Initialization of 2D Arrays

We can initialize a 2D array by using a list of values enclosed inside '{ }' and separated by a
comma as shown in the example below:

int arr[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11};


or
int arr[3][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}};
The elements will be stored in the array from left to right and top to bottom. So, the first 4
elements from the left will be filled in the first row, the next 4 elements in the second row, and
so on. This is clearly shown in the second syntax where each set of inner braces represents one
row.

type arr_name[][n] = {...values...};

The below example demonstrates the row-by-row traversal of a 2D array.

#include <stdio.h>

int main() {

// Create and initialize an array with 3 rows


// and 2 columns
int arr[3][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };

// Print each array element's value


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
printf("arr[%d][%d]: %d ", i, j, arr[i][j]);
}
printf("\n");
}
return 0;
}
Output

arr[0][0]: 0 arr[0][1]: 1

20
Module 1- Introduction to Data Structures and Algorithms

arr[1][0]: 2 arr[1][1]: 3

arr[2][0]: 4 arr[2][1]: 5

3D Array in C

A Three-Dimensional Array or 3D array in C is a collection of two-dimensional arrays. It


can be visualized as multiple 2D arrays stacked on top of each other.

Declaration of 3D Array in C

We can declare a 3D array with x 2D arrays each having m rows and n columns using the
syntax shown below:

type arr_name[x][m][n];

For example, we can declare 3d array, which is made by 2-2D array and each 2D array have 2
rows and 2 columns:

int arr[2][2][2];

Initialization of 3D Array in C

Initialization in a 3D array is the same as that of 2D arrays. The difference is as the number
of dimensions increases so the number of nested braces will also increase.

int arr[2][3][2] = {0, 1, 2, 3, 4, 5, 6, 7 , 8, 9, 10, 11}

or

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

{ { 6, 7 }, { 8, 9 }, { 10, 11 } } };

Let's take a simple example to demonstrate about 3D arrays:

#include <stdio.h>

int main() {

21
Module 1- Introduction to Data Structures and Algorithms

// Create and Initialize the


// 3-dimensional array
int arr[2][3][2] = { { { 1, 1 }, { 2, 3 },
{ 4, 5 } }, { { 6, 7 },
{ 8, 9 }, { 10, 11 } } };

// Loop through the depth


for (int i = 0; i < 2; ++i) {

// Loop through the


// rows of each depth
for (int j = 0; j < 3; ++j) {

// Loop through the


// columns of each row
for (int k = 0; k < 2; ++k)
printf("arr[%i][%i][%i] = %d ", i, j, k,
arr[i][j][k]);
printf("\n");
}
printf("\n\n");
}
return 0;
}
Applications of Array Data Structure:

• Storing and accessing data: Arrays store elements in a specific order and allow
constant-time O(1) access to any element.
• Searching: If data in array is sorted, we can search an item in O(log n) time. We can
also find floor(), ceiling(), kth smallest, kth largest, etc efficiently.
• Matrices: Two-dimensional arrays are used for matrices in computations like graph
algorithms and image processing.
• Implementing other data structures: Arrays are used as the underlying data structure
for implementing stacks and queues.
• Dynamic programming: Dynamic programming algorithms often use arrays to store
intermediate results of subproblems in order to solve a larger problem.
• Data Buffers: Arrays serve as data buffers and queues, temporarily storing incoming
data like network packets, file streams, and database results before processing.

Linked List
• A linked list is a linear data structure where each element (called a node) is
connected to the next one using pointers. Unlike array, elements of linked list are
stored in random memory locations.
• In this article, we will learn about the linked list, its types, representation of the
linked list in C, and discuss what link list offers as compared to the similar data
structures.

A linked list is a sequence of nodes in which each node is made up of two parts:

22
Module 1- Introduction to Data Structures and Algorithms

• Data: The value stored in the node.

• Pointer: A reference to the next node in the sequence. (There can be multiple pointers
for different kind of linked list.)

Types of Linked List in C


Linked list can be classified on the basis of the type of structure they form as a whole and the
direction of access. Based on this classification, there are five types of linked lists:

1. Singly Linked List

2. Doubly Linked List

3. Circular Linked List

Singly Linked List in C(Refer manual for the program)


A linked list or singly linked list is a linear data structure that is made up of a group of nodes
in which each node has two parts: the data, and the pointer to the next node. The last node's
(also known as tail) pointers point to NULL to indicate the end of the linked list.

Representation of Singly Linked List

A linked list is represented as a pointer to the first node where each node contains:

• Data: Here the actual information is stored.

• Next: Pointer that links to the next node.

// Structure to represent the


// singly linked list
struct Node {

// Data field - can be of


// any type and count
int data;

// Pointer to the next node

23
Module 1- Introduction to Data Structures and Algorithms

struct Node* next;


}

Doubly Linked List

A doubly linked list is a bit more complex than singly linked list. In it, each node contains
three parts: the data, a pointer to the next node, and one extra pointer which points to the
previous node. This allows for traversal in both directions, making it more versatile than a
singly linked list.

Representation of Doubly Linked List

A doubly linked list is represented as a pointer to the first node (head), where each node
contains:

• Data: The actual information stored in the node.

• Next: A pointer that links to the next node in the sequence.

• Previous: A pointer that links to the previous node in the sequence.

// Structure to represent the doubly linked list


struct Node {

// Data field - can be of any type and count


int data;

// Pointer to the next node


struct Node* next;

// Pointer to the previous node


struct Node* prev;
}

24
Module 1- Introduction to Data Structures and Algorithms

Circular Linked List


A circular linked list is a variation of a singly linked list where the last node points back to
the first node, forming a circle. This means there is no NULL at the end, and the list can be
traversed in a circular manner.

Representation of Circular Linked List

A circular linked list is represented as a pointer to the first node, where each node contains:

• Data: The actual information stored in the node.

• Next: A pointer that links to the next node, with the last node pointing back to the first
node.

// Structure to represent the circular linked list


struct Node {

// Data field - can be of any type and count


int data;

// Pointer to the next node


struct Node* next;
}
Applications of linked list

1. Implementation of stacks and queues


2. Implementation of graphs: Adjacency list representation of graphs is the most popular
which uses a linked list to store adjacent vertices.
3. Dynamic memory allocation: We use a linked list of free blocks.
4. Maintaining a directory of names
5. Performing arithmetic operations on long integers
6. Manipulation of polynomials by storing constants in the node of the linked list
7. Representing sparse matrices

25

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy