SWE233 - Advanced Data Structures I - Lecture 3
SWE233 - Advanced Data Structures I - Lecture 3
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.
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
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:
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.
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.
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.
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
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.
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.
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.
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.
The size of the above arrays is 5 which is automatically deduced by the compiler.
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.
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