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

SWE233 - Advanced Data Structures I - Lecture 3

The document provides an overview of data structures, explaining their importance in organizing and manipulating data efficiently. It distinguishes between primitive and non-primitive data structures, discusses linear and non-linear data structures, and details operations performed on data structures. Additionally, it covers linked lists and arrays, including their definitions, types, advantages, disadvantages, and applications in programming.

Uploaded by

terasapcommunity
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)
10 views

SWE233 - Advanced Data Structures I - Lecture 3

The document provides an overview of data structures, explaining their importance in organizing and manipulating data efficiently. It distinguishes between primitive and non-primitive data structures, discusses linear and non-linear data structures, and details operations performed on data structures. Additionally, it covers linked lists and arrays, including their definitions, types, advantages, disadvantages, and applications in programming.

Uploaded by

terasapcommunity
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/ 7

DATA STRUCTURES

Data Structure is the way of collecting and organizing the data in such a way that we can perform
operations on these data in an effective way. For Example: We have the data Student's Name
"Bello" and Age 16. Here "Bello" is a string data type and 16 is an integer type. We can now
organize this data as a record like a Student Record. We can collect and store other Student's
Record in a file or a database as a data structure.

For many problems, the ability to formulate an efficient algorithm depends on being able to
organize the data in an appropriate manner. Data may be organized in many different ways logical
or mathematical models of a program particularly organization of data. This organized data is
called “Data Structure”. Or the organized collection of data is called a ‘Data Structure’.

Data Structure involves two complementary goals. The first goal is to identify and develop useful,
mathematical entities and operations and to determine what class of problems can be solved by
using these entities and operations. The second goal is to determine representation for those
abstract entities to implement abstract operations on this concrete representation.

Primitive Data structures are directly supported by the language ie; any operation is directly
performed in these data items. Example: integer, Character, Real numbers etc.

Non-primitive data types are not defined by the programming language but are instead created
by the programmer.

Thomas (Person) MUNJAM | [email protected] Compiled for Higher National Diploma (HND)
https://www.personlinks.com | [email protected]
+237 678686249 | 659496501 | [email protected] SWE233: Advanced Data Structures I 18
Linear data structures organize their data elements in a linear fashion, where data elements are
attached one after the other. Linear data structures are very easy to implement, since the memory
of the computer is also organized in a linear fashion. Some commonly used linear data structures
are arrays, linked lists, stacks and queues.

In non-linear data structures, data elements are not organized in a sequential fashion. Data
structures like multidimensional arrays, trees, graphs, tables and sets are some examples of widely
used nonlinear data structures.

Pointers and Dynamic Memory Allocation

A pointer is a variable that can store an address of a variable. We say that a pointer points to a
variable that is stored at that address. A pointer itself usually occupies 4 bytes of memory.

Application of Pointers

 A pointer enables us to access a variable that is defined outside the function.


 Pointers are more efficient in handling the data tables.
 Pointers reduce the length and complexity of a program.
 They increase the execution speed.

Operations performed on data structures

Following operations can be performed on the data structures:

7. Traversing: It is used to access each data item exactly once so that it can be processed.
8. Searching: It is used to find out the location of the data item if it exists in the given
collection of data items.
9. Inserting: It is used to add a new data item in the given collection of data items.
10. Deleting: It is used to delete an existing data item from the given collection of data items.
11. Sorting: It is used to arrange the data items in some order i.e., in ascending or descending
order in case of numerical data and in dictionary order in case of alphanumeric data.
12. Merging: It is used to combine the data items of two sorted files into single file in the
sorted form.

Thomas (Person) MUNJAM | [email protected] Compiled for Higher National Diploma (HND)
https://www.personlinks.com | [email protected]
+237 678686249 | 659496501 | [email protected] SWE233: Advanced Data Structures I 19
1. LINKED LISTS

A linked list is a linear data structure where each element, known as a node, is connected to the
next one using pointers. Unlike array, elements of linked list are stored in random memory
locations. In a linked list, each node contains two parts:

 Data: The value stored in the node.


 Pointer: A reference to the next node in the sequence.

Unlike arrays, linked lists do not store elements in contiguous memory locations. Instead, each
node points to the next, forming a chain-like structure and to access any element (node), we need
to first sequentially traverse all the nodes before it. It is a recursive data structure in which any
smaller part of it is also a linked list in itself.

Representation of Linked List in C

In C, linked lists are represented as the pointer to the first node in the list. For that reason, the first
node is generally called head of the linked list. Each node of the linked list is represented by a
structure that contains a data field and a pointer of the same type as itself. Such structures are called
self-referential structures.

Types of Linked Lists

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:

A. Singly Linked List: 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. 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.

Thomas (Person) MUNJAM | [email protected] Compiled for Higher National Diploma (HND)
https://www.personlinks.com | [email protected]
+237 678686249 | 659496501 | [email protected] SWE233: Advanced Data Structures I 20
B. 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. 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.

C. 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. 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.

Applications of Linked Lists in C

The following are some major applications of linked list:


 Dynamic memory allocation efficiently manages and allocates dynamic memory in
systems and applications.
 Implementing other data structures such as stacks, queues, etc.
 Represents and manipulates polynomials, with each node storing terms.
 Used in file system management dynamically in operating systems.

Thomas (Person) MUNJAM | [email protected] Compiled for Higher National Diploma (HND)
https://www.personlinks.com | [email protected]
+237 678686249 | 659496501 | [email protected] SWE233: Advanced Data Structures I 21
Advantages of a Linked List

 Linked lists can grow or shrink in size dynamically, as memory is allocated or


deallocated as needed.
 Inserting or deleting nodes in a linked list is efficient and does not require shifting
elements, unlike arrays.
 Memory is utilized more efficiently as linked lists do not require a pre-allocated size,
reducing wasted space.
 They serve as the foundation for implementing more complex data structures like stacks,
queues, and graphs.
 They can utilize non-contiguous memory blocks, making them suitable for applications
where memory is fragmented.

Disadvantages of a Linked List

 Each node requires extra memory for storing a pointer.


 Linked lists do not allow direct access to elements by index. Accessing a specific node
requires traversing from the head, leading to slower access times.
 Managing pointers can be tricky, increasing the complexity of coding.
 Searching for an element or accessing a node by index takes much time, making linked
lists slower for such operations compared to arrays.
 Non-contiguous memory allocation results in more cache misses.
 Singly linked lists do not support easy backward traversal.

2. ARRAYS

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 and also derived and user-defined data
types such as pointers and structures. It is one of the most used data structures in C programming
and it is a simple and fast way of storing multiple values under a single name.

Thomas (Person) MUNJAM | [email protected] Compiled for Higher National Diploma (HND)
https://www.personlinks.com | [email protected]
+237 678686249 | 659496501 | [email protected] SWE233: Advanced Data Structures I 22
In C, we have to declare the array like any other variable before using it. We can declare an array
by specifying its name, the type of its elements, and the size of its dimensions. When we declare
an array in C, the compiler allocates the memory block of the specified size to the array name.

Syntax of Array Declaration

where N is the number of dimensions.

The C arrays are static in nature, i.e., they are allocated memory at the compile time.

C 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 value. There are multiple ways in which we can initialize
an array in C.

 Array Initialization with Declaration

In this method, we initialize the array along with its declaration. We use an initializer list to
initialize multiple elements of the array. An initializer list is the list of values enclosed within
braces { } separated by a comma.

data_type array_name [size] = {value1, value2, ... valueN};

 Array Initialization with Declaration without Size

If we initialize an array using an initializer list, we can skip declaring the size of the array as the
compiler can automatically deduce the size of the array in these cases. The size of the array in

Thomas (Person) MUNJAM | [email protected] Compiled for Higher National Diploma (HND)
https://www.personlinks.com | [email protected]
+237 678686249 | 659496501 | [email protected] SWE233: Advanced Data Structures I 23
these cases is equal to the number of elements present in the initializer list as the compiler can
automatically deduce the size of the array.

data_type array_name[] = {1,2,3,4,5};

The size of the above arrays is 5 which is automatically deduced by the compiler.

 Array Initialization after Declaration (Using Loops)

We initialize the array after the declaration by assigning the initial value to each element
individually. We can use for loop, while loop, or do-while loop to assign the value to each element
of the array.

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


array_name[i] = valuei;
}

Example of Array Initialization in C

Thomas (Person) MUNJAM | [email protected] Compiled for Higher National Diploma (HND)
https://www.personlinks.com | [email protected]
+237 678686249 | 659496501 | [email protected] SWE233: Advanced Data Structures I 24

You might also like