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

Lecture 02 - Abstract Data Type, Arrays & Linked List

The document discusses abstract data types and linked lists. It begins by defining an abstract data type as focusing on what a data structure does rather than how it works. Arrays and linked lists are provided as examples. The benefits of abstract data types include allowing data structures to change without affecting programs. Arrays store elements in consecutive memory locations, while linked lists do not require consecutive storage. Linked lists are more flexible than arrays for insertion and deletion but slower to access specific elements. The document then explains various operations for inserting, deleting, traversing and searching nodes within a linked list.

Uploaded by

ahababimtiaz
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)
51 views

Lecture 02 - Abstract Data Type, Arrays & Linked List

The document discusses abstract data types and linked lists. It begins by defining an abstract data type as focusing on what a data structure does rather than how it works. Arrays and linked lists are provided as examples. The benefits of abstract data types include allowing data structures to change without affecting programs. Arrays store elements in consecutive memory locations, while linked lists do not require consecutive storage. Linked lists are more flexible than arrays for insertion and deletion but slower to access specific elements. The document then explains various operations for inserting, deleting, traversing and searching nodes within a linked list.

Uploaded by

ahababimtiaz
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/ 24

ِ‫ﺑِﺳْ مِ ٱ ﱠٰ ِ ٱﻟرﱠ ﺣْ َٰﻣ ِن ٱﻟرﱠ ﺣِﯾم‬

In the name of Allah, Most Gracious, Most Merciful

CSE 4303
Data Structure

Topic:Abstract Data Type, Arrays & Linked List

Asaduzzaman Herok
Lecturer | CSE | IUT
[email protected]
What is Abstract Data Type?
An abstract data type (ADT) is the way we look at a data structure, focusing on what it does
and ignoring how it does its job.

For example, stacks and queues are perfect examples of an ADT

Data type: Data type of a variable is the set of values that the variable can take. We
have already read the basic data types in C include int, char, float, and double.

Abstract; The word ‘abstract’ in the context of data structures means considered apart
from the detailed specifications or implementation.

Advantage of using ADTs: In the real world, programs evolve as a result of new
requirements or constraints, so a modification to a program commonly requires a change in
one or more of its data structures. So it is better to separate the use of a data structure from
the details of its implementation.
Asaduzzaman Herok, Lecturer
28 August, 2023 2
Department of Computer Science and Engineering, IUT
Arrays
Scenario: There are 20 students in a class, we have write a program that reads and
prints the marks of all the students. How can we do it?

● We need 20 integers to store and print the marks.


● One way is to have 20 integer variables with different names like, mark01, mark02, mark03…
● For reading we need 20 input (i.e scanf) statements.
● For printing we need 20 output (i.e print) statements.

Now what if we have the same scenario for a


● Entire course? (say 63 students)
● Entire 3rd semester cse+swe (180 students)
● Entire university ( 10,000 students)
???

Asaduzzaman Herok, Lecturer


28 August, 2023 3
Department of Computer Science and Engineering, IUT
Arrays
What we need is a data structure, Simplest form an Array.
● a collection of similar data elements
● elements are stored in consecutive memory locations.

Advantage:
● An array is a pretty obvious way to store a list, with a big advantage: it enables very fast
access of each item.

Disadvantage:
● First, if we want to insert an item at the beginning or middle of an array, we have to slide
a lot of items over one place to make room. This takes time proportional to the length of
the array.
● Second, an array has a fixed length that can’t be changed. If we want to add items to the
list, but the array is full, we have to allocate a whole new array and move all the items
from the old array to the new one.

Asaduzzaman Herok, Lecturer


28 August, 2023 4
Department of Computer Science and Engineering, IUT
Operations on Arrays / Linked List
Operations at the kth entry of the list include:

➢ Traversing Erasing/Deleting
➢ Find / Search Accessing
➢ Insert
➢ Erase / Delete
➢ Update / Replace
➢ Sorting
➢ Merging Inserting
Updating

Asaduzzaman Herok, Lecturer


28 August, 2023 5
Department of Computer Science and Engineering, IUT
Complexity of Operations on Arrays

Given a sorted array, we have the following run times:

Operation \ Front / 1st Arbitrary Back / nth


Position location in terms
of particular
value

Find Good Okay Good

Insert Bad Bad Good* Bad

Erase Bad Bad Good

Good = Θ(1), Okay = Θ(log(n)), Bad = Θ(n) * only if the array is not full

Asaduzzaman Herok, Lecturer


28 August, 2023 6
Department of Computer Science and Engineering, IUT
Complexity of Operations on Arrays

Given an unsorted array, we have the following run times:

Operation \ Front / 1st Arbitrary Back / nth


Position location in terms
of particular
value

Find Good Bad Good

Insert Bad Bad Good* Bad

Erase Bad Bad Good

Good = Θ(1), Okay = Θ(log(n)), Bad = Θ(n) * only if the array is not full

Asaduzzaman Herok, Lecturer


28 August, 2023 7
Department of Computer Science and Engineering, IUT
Linked List
Why don’t we just keep using Array?
➢ While declaring arrays, size should be predefined.
➢ Extending arrays is more costlier.
○ Can we just create array with large size?
○ What if after declaring large size, there is not enough data to fill that size?

???

There must be a data structure that removes the restrictions on the maximum number of
elements and the storage condition to write efficient programs … …

Asaduzzaman Herok, Lecturer


28 August, 2023 8
Department of Computer Science and Engineering, IUT
Linked List
A linked list, in simple terms, is a linear collection of data elements. These data elements are
called nodes.

➔ Linked lists are appropriate when the number of data elements to be represented in
the data structure at once is unpredictable.
➔ Linked lists are dynamic, so the length of a list can increase or decrease as necessary.
➔ Each node does not necessarily follow the previous one physically in the memory.
➔ Linked lists can be maintained in sorted order by inserting an element at the proper
point in the list.
➔ Can be used to implement other data structures like binary trees.

❏ However, linked lists have a big disadvantage compared to arrays. Finding the kth item of a linked list
takes time proportional to k. You have to start at the head of the list and walk forward k - 1 nodes, one
"next" at a time.
Asaduzzaman Herok, Lecturer
28 August, 2023 9
Department of Computer Science and Engineering, IUT
Operations on Linked List
Traversing: accessing the nodes of the list in
order to perform some processing on them.

Searching: finding a particular element in the


linked list. If it is present, the algorithm returns
the address of the node that contains the value.

Asaduzzaman Herok, Lecturer


28 August, 2023 10
Department of Computer Science and Engineering, IUT
Insert Operations on Linked List
Case 1: The new node is inserted at the beginning.

Asaduzzaman Herok, Lecturer


28 August, 2023 11
Department of Computer Science and Engineering, IUT
Insert Operations on Linked List
Case 2: The new node is inserted at the end.

Asaduzzaman Herok, Lecturer


28 August, 2023 12
Department of Computer Science and Engineering, IUT
Insert Operations on Linked List
Case 3: The new node is inserted after a given node.

Asaduzzaman Herok, Lecturer


28 August, 2023 13
Department of Computer Science and Engineering, IUT
Insert Operations on Linked List
Case 4: The new node is inserted before a given node

Asaduzzaman Herok, Lecturer


28 August, 2023 14
Department of Computer Science and Engineering, IUT
Delete Operations on Linked List

Case 1: The first node is deleted.

Asaduzzaman Herok, Lecturer


28 August, 2023 15
Department of Computer Science and Engineering, IUT
Delete Operations on Linked List

Case 2: The last node is deleted.

Asaduzzaman Herok, Lecturer


28 August, 2023 16
Department of Computer Science and Engineering, IUT
Delete Operations on Linked List

Case 3: The node after a given node is deleted

Asaduzzaman Herok, Lecturer


28 August, 2023 17
Department of Computer Science and Engineering, IUT
Complexity of Operations on Linked List
We will use the following matrix to describe operations at the locations within the structure

Asaduzzaman Herok, Lecturer


28 August, 2023 18
Department of Computer Science and Engineering, IUT
Complexity of Operations on Singly linked list
Using only a single pointer PTR. No PREPTR is used. (I repeat, Using only single pointer PTR)

Asaduzzaman Herok, Lecturer


28 August, 2023 19
Department of Computer Science and Engineering, IUT
Doubly and Circular Linked List
A doubly linked list or a two-way linked list is a more complex type of linked list which
contains a pointer to the next as well as the previous node in the sequence.

In a circular linked list, the last node contains a pointer to the first node of the list.

A circular doubly linked list or a circular two-way linked list is a more complex type of linked
list which contains a pointer to the next as well as the previous node in the sequence.

Asaduzzaman Herok, Lecturer


28 August, 2023 20
Department of Computer Science and Engineering, IUT
Pros and Cons of Circular Linked / Circular Doubly Linked Lists
Advantage
❏ If we are at a node, then we can go to any node. But in linear linked list it is not
possible to go to previous node.
❏ It saves time when we have to go to the first node from the last node. It can be done
in single step because there is no need to traverse the in between nodes. But in
double linked list, we will have to go through in between nodes.

Disadvantage
❏ Finding end of list and loop control is harder (no NULL's to mark the end)
❏ If we at a node and need to go back to the previous node, then we can not do it in
single step (In case of singly circular list). Instead we have to complete the entire
circle by going through the in between nodes.

Asaduzzaman Herok, Lecturer


28 August, 2023 21
Department of Computer Science and Engineering, IUT
Complexity of Operations on Doubly linked list

Asaduzzaman Herok, Lecturer


28 August, 2023 22
Department of Computer Science and Engineering, IUT
Applications of Linked Lists
❏ Process Scheduling in Operating Systems
❏ Hashing
❏ Stack and Queue
❏ Graph Algorithms
❏ Music Player Application
❏ Consider the history section of web browsers, where it creates a linked list of
webpages visited
❏ Our brain is also a good example of linked list. In the initial stages of learning
something by heart, the natural process is to link one item to next. Also, when we
forget something and try to remember, than our brain follows association and tries
to link one memory with another and so on and we finally recall the lost memory

Asaduzzaman Herok, Lecturer


28 August, 2023 23
Department of Computer Science and Engineering, IUT
Acknowledgement

Rafsanjany Kushol
PhD Student, Dept. of Computing Science,
University of Alberta

Sabbir Ahmed
Assistant Professor
Department of CSE, IUT

Asaduzzaman Herok, Lecturer


28 August, 2023 24
Department of Computer Science and Engineering, IUT

You might also like