Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

PAMANTASAN NG CABUYAO

COLLEGE OF COMPUTING AND ENGINEERING

COURSE CODE: CPP 104

COURSE DESCRIPTION: DATA STRUCTURES AND ALGORITHM

COURSE INTENDED On the completion of the course, student is expected to be able to do the
LEARNING OUTCOMES: following:

1.Understanding of basic algorithmic complexity.

2.Utilize the ability to perform simple inductive proofs and proofs by


contradiction and reason about program correctness and invariants.

3.Understand the fundamental abstract data types which can include:


Maps, Sets and Vectors.

LEARNING MATERIAL FOR 2


WEEK NUMBER:

I. TITLE: Abstract Data Types and Fundamentals of Linked List

II. OBJECTIVES: By the end of this module you should be able to:

1. Distinguish between formal and informal specifications


2. To be able to compare the performance of our linked list
implementation with Python’s list implementation.
3. To be able to implement ADT list as linked list using the node and
reference pattern

III. INTRODUCTION:
Abstract Data type (ADT) is a type (or class) for objects whose behavior
is defined by a set of values and a set of operations. The definition of ADT
only mentions what operations are to be performed but not how these
operations will be implemented.

It does not specify how data will be organized in memory and what
algorithms will be used for implementing the operations. It is called
“abstract” because it gives an implementation-independent view. The
process of providing only the essentials and hiding the details is known as
abstraction.

IV. CONTENTS:

Lesson Coverage:

ABSTRACT DATA TYPES


INTRODUCTION TO LINKED LIST

LECTURE NOTES COMPILATION Page 1 of 11


1st Semester A.Y. 2022-2023
PAMANTASAN NG CABUYAO
COLLEGE OF COMPUTING AND ENGINEERING

Introduction

Data types such as int, float, double, long, etc. are considered to be in-built data types and we can perform basic
operations with them such as addition, subtraction, division, multiplication, etc. Now there might be a situation
when we need operations for our user-defined data type which have to be defined. These operations can be defined
only as and when we require them. So, in order to simplify the process of solving problems, we can create data
structures along with their operations, and such data structures that are not in-built are known as Abstract Data
Type (ADT).

The user of data type does not need to know how that data type is implemented, for example, we have been using
Primitive values like int, float, char data types only with the knowledge that these data type can operate and be
performed on without any idea of how they are implemented. So a user only needs to know what a data type can
do, but not how it will be implemented. Think of ADT as a black box which hides the inner structure and design
of the data type. Now we’ll define three ADTs namely List ADT, Stack ADT, Queue ADT.

List ADT

The data is generally stored in key sequence in a list which has a head structure consisting of count, pointers and
address of compare function needed to compare the data in the list.

The data node contains the pointer to a data structure and a self-referential pointer which points to the next node
in the list.

The List ADT Functions is given below:

• get() – Return an element from the list at any given position.


• insert() – Insert an element at any position of the list.
• remove() – Remove the first occurrence of any element from a non-empty list.
• removeAt() – Remove the element at a specified location from a non-empty list.
• replace() – Replace an element at any position by another element.
• size() – Return the number of elements in the list.
• isEmpty() – Return true if the list is empty, otherwise return false.

LECTURE NOTES COMPILATION Page 2 of 11


1st Semester A.Y. 2022-2023
PAMANTASAN NG CABUYAO
COLLEGE OF COMPUTING AND ENGINEERING

• isFull() – Return true if the list is full, otherwise return false.

Stack ADT

In Stack ADT Implementation instead of data being stored in each node, the pointer to data is stored.

The program allocates memory for the data and address is passed to the stack ADT.

The head node and the data nodes are encapsulated in the ADT. The calling function can only see the pointer to
the stack.

The stack head structure also contains a pointer to top and count of number of entries currently in stack.

• push() – Insert an element at one end of the stack called top.


• pop() – Remove and return the element at the top of the stack, if it is not empty.
• peek() – Return the element at the top of the stack without removing it, if the stack is not empty.
• size() – Return the number of elements in the stack.
• isEmpty() – Return true if the stack is empty, otherwise return false.
• isFull() – Return true if the stack is full, otherwise return false.

Queue ADT

The queue abstract data type (ADT) follows the basic design of the stack abstract data type.

Each node contains a void pointer to the data and the link pointer to the next element in the queue. The program’s
responsibility is to allocate memory for storing the data.

• enqueue() – Insert an element at the end of the queue.


• dequeue() – Remove and return the first element of the queue, if the queue is not empty.
• peek() – Return the element of the queue without removing it, if the queue is not empty.
• size() – Return the number of elements in the queue.
• isEmpty() – Return true if the queue is empty, otherwise return false.
• isFull() – Return true if the queue is full, otherwise return false.

Features of ADT:

Abstraction: The user does not need to know the implementation of the data structure.

Better Conceptualization: ADT gives us a better conceptualization of the real world.

Robust: The program is robust and has the ability to catch errors.

From these definitions, we can clearly see that the definitions do not specify how these ADTs will be represented
and how the operations will be carried out. There can be different ways to implement an ADT, for example, the
List ADT can be implemented using arrays, or singly linked list or doubly linked list. Similarly, stack ADT and
Queue ADT can be implemented using arrays or linked lists.

LECTURE NOTES COMPILATION Page 3 of 11


1st Semester A.Y. 2022-2023
PAMANTASAN NG CABUYAO
COLLEGE OF COMPUTING AND ENGINEERING

LINKED LIST

A linked list is a sequence of data elements, which are connected together via links. Each data element contains
a connection to another data element in form of a pointer. Python does not have linked lists in its standard library.

We have already seen how we create a node class and how to traverse the elements of a node. In this type of data
structure there is only one link between any two data elements. We create such a list and create additional methods
to insert, update and remove elements from the list.

Creation of Linked list

A linked list is created by using the node class we studied in the last chapter. We create a Node object and create
another class to use this ode object. We pass the appropriate values through the node object to point the to the
next data elements. The below program creates the linked list with three data elements. In the next section we will
see how to traverse the linked list.

class Node:

def __init__(self, dataval=None):


self.dataval = dataval
self.nextval = None

class SLinkedList:
def __init__(self):
self.headval = None

list1 = SLinkedList()
list1.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")
# Link first Node to second node
list1.headval.nextval = e2

# Link second Node to third node


e2.nextval = e3

Traversing a Linked List

Singly linked lists can be traversed in only forward direction starting form the first data element. We simply print
the value of the next data element by assigning the pointer of the next node to the current data element.

Example

class Node:

def __init__(self, dataval=None):

self.dataval = dataval

self.nextval = None

LECTURE NOTES COMPILATION Page 4 of 11


1st Semester A.Y. 2022-2023
PAMANTASAN NG CABUYAO
COLLEGE OF COMPUTING AND ENGINEERING

class SLinkedList:

def __init__(self):

self.headval = None

def listprint(self):

printval = self.headval

while printval is not None:

print (printval.dataval)

printval = printval.nextval

list = SLinkedList()

list.headval = Node("Mon")

e2 = Node("Tue")

e3 = Node("Wed")

# Link first Node to second node

list.headval.nextval = e2

# Link second Node to third node

e2.nextval = e3

list.listprint()

Output

When the above code is executed, it produces the following result −

Mon

Tue

Wed

LECTURE NOTES COMPILATION Page 5 of 11


1st Semester A.Y. 2022-2023
PAMANTASAN NG CABUYAO
COLLEGE OF COMPUTING AND ENGINEERING

Insertion in a Linked List

Inserting element in the linked list involves reassigning the pointers from the existing nodes to the newly inserted
node. Depending on whether the new data element is getting inserted at the beginning or at the middle or at the
end of the linked list, we have the below scenarios.

Inserting at the Beginning

This involves pointing the next pointer of the new data node to the current head of the linked list. So the current
head of the linked list becomes the second data element and the new node becomes the head of the linked list.

Example

class Node:
def __init__(self, dataval=None):
self.dataval = dataval
self.nextval = None

class SLinkedList:
def __init__(self):
self.headval = None
# Print the linked list
def listprint(self):
printval = self.headval
while printval is not None:
print (printval.dataval)
printval = printval.nextval
def AtBegining(self,newdata):
NewNode = Node(newdata)

# Update the new nodes next val to existing node


NewNode.nextval = self.headval
self.headval = NewNode

list = SLinkedList()
list.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")

list.headval.nextval = e2
e2.nextval = e3

list.AtBegining("Sun")
list.listprint()

LECTURE NOTES COMPILATION Page 6 of 11


1st Semester A.Y. 2022-2023
PAMANTASAN NG CABUYAO
COLLEGE OF COMPUTING AND ENGINEERING

Output

When the above code is executed, it produces the following result −

Sun

Mon

Tue

Wed

Inserting at the End

This involves pointing the next pointer of the the current last node of the linked list to the new data node. So the
current last node of the linked list becomes the second last data node and the new node becomes the last node of
the linked list.

Example

class Node:
def __init__(self, dataval=None):
self.dataval = dataval
self.nextval = None
class SLinkedList:
def __init__(self):
self.headval = None
# Function to add newnode
def AtEnd(self, newdata):
NewNode = Node(newdata)
if self.headval is None:
self.headval = NewNode
return
laste = self.headval
while(laste.nextval):
laste = laste.nextval
laste.nextval=NewNode
# Print the linked list
def listprint(self):
printval = self.headval
while printval is not None:
print (printval.dataval)
printval = printval.nextval

list = SLinkedList()
list.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")

list.headval.nextval = e2
e2.nextval = e3

list.AtEnd("Thu")

list.listprint()

LECTURE NOTES COMPILATION Page 7 of 11


1st Semester A.Y. 2022-2023
PAMANTASAN NG CABUYAO
COLLEGE OF COMPUTING AND ENGINEERING

Output

When the above code is executed, it produces the following result −

Mon

Tue

Wed

Thu

Inserting in between two Data Nodes

This involves changing the pointer of a specific node to point to the new node. That is possible by passing in both
the new node and the existing node after which the new node will be inserted. So we define an additional class
which will change the next pointer of the new node to the next pointer of middle node. Then assign the new node
to next pointer of the middle node.

class Node:
def __init__(self, dataval=None):
self.dataval = dataval
self.nextval = None
class SLinkedList:
def __init__(self):
self.headval = None

# Function to add node


def Inbetween(self,middle_node,newdata):
if middle_node is None:
print("The mentioned node is absent")
return

NewNode = Node(newdata)
NewNode.nextval = middle_node.nextval
middle_node.nextval = NewNode

# Print the linked list


def listprint(self):
printval = self.headval
while printval is not None:
print (printval.dataval)
printval = printval.nextval

list = SLinkedList()
list.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Thu")

list.headval.nextval = e2
e2.nextval = e3

LECTURE NOTES COMPILATION Page 8 of 11


1st Semester A.Y. 2022-2023
PAMANTASAN NG CABUYAO
COLLEGE OF COMPUTING AND ENGINEERING

list.Inbetween(list.headval.nextval,"Fri")

list.listprint()

Output

When the above code is executed, it produces the following result −

Mon

Tue

Fri

Thu

Removing an Item

We can remove an existing node using the key for that node. In the below program we locate the previous node
of the node which is to be deleted.Then, point the next pointer of this node to the next node of the node to be
deleted.

Example

class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class SLinkedList:
def __init__(self):
self.head = None

def Atbegining(self, data_in):


NewNode = Node(data_in)
NewNode.next = self.head
self.head = NewNode

# Function to remove node


def RemoveNode(self, Removekey):
HeadVal = self.head

if (HeadVal is not None):


if (HeadVal.data == Removekey):
self.head = HeadVal.next
HeadVal = None
return
while (HeadVal is not None):
if HeadVal.data == Removekey:
break
prev = HeadVal
HeadVal = HeadVal.next

LECTURE NOTES COMPILATION Page 9 of 11


1st Semester A.Y. 2022-2023
PAMANTASAN NG CABUYAO
COLLEGE OF COMPUTING AND ENGINEERING

if (HeadVal == None):
return

prev.next = HeadVal.next
HeadVal = None

def LListprint(self):
printval = self.head
while (printval):
print(printval.data),
printval = printval.next

llist = SLinkedList()
llist.Atbegining("Mon")
llist.Atbegining("Tue")
llist.Atbegining("Wed")
llist.Atbegining("Thu")
llist.RemoveNode("Tue")
llist.LListprint()

Output

When the above code is executed, it produces the following result −

Thu

Wed

Mon

LECTURE NOTES COMPILATION Page 10 of 11


1st Semester A.Y. 2022-2023
PAMANTASAN NG CABUYAO
COLLEGE OF COMPUTING AND ENGINEERING

V. REFERENCES: J. Bullinaria ( 2019) . Data Structure and Algorithm. Birmingham UK.

M. Goodrich (2013). Data Structures and Algorithms. Wiley

M. Weiss (2007) . Data Structures and Algorithms 2nd Edition . Pearson Int.

B. Baka (2018). Hands – On Data Structures and Algorithm in Python. Packt Publishing

Free Online Reference

https://www.tutorialspoint.com/python_data_structure/index.htm

https://www.geeksforgeeks.org/python-data-structures-and-algorithms/

https://www.codecademy.com/learn/learn-data-structures-and-algorithms-with-python

VI. ASSESSMENT TASK:

See Attached file given by the instructor

LECTURE NOTES COMPILATION Page 11 of 11


1st Semester A.Y. 2022-2023

You might also like