0% found this document useful (0 votes)
16 views24 pages

2 CH 2

The document outlines the List Abstract Data Type (ADT) and its operations, including insertion, deletion, updating, and searching for elements. It describes the linear list as a data structure with ordered elements and discusses array-based and linked list implementations. Key operations such as creating, destroying, and managing elements in the list are also detailed, along with time complexities for various operations.

Uploaded by

uaj208
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)
16 views24 pages

2 CH 2

The document outlines the List Abstract Data Type (ADT) and its operations, including insertion, deletion, updating, and searching for elements. It describes the linear list as a data structure with ordered elements and discusses array-based and linked list implementations. Key operations such as creating, destroying, and managing elements in the list are also detailed, along with time complexities for various operations.

Uploaded by

uaj208
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

Data Structures and Algorithm

Department of Computer Science


COMSATS University Islamabad
Outline

■ List Abstract Data Type (ADT)


■ List ADT with Array Implementation
■ Operations
■ Insert a new element in the list
■ insert at the beginning of the list
■ insert at the end of the list
■ insert anywhere in the list
■ Delete an element from the list
■ delete from the beginning of the list
■ delete from the end of the list
■ delete any element in the list
■ Display print the elements of list
■ Update an element in the list (at the beginning, end,
anywhere)
■ Search for an element in the list

Department of Computer Science


Lecture No 2

ABSTRACT DATA TYPE FOR LIST


(List ADT)

(Array Based)

Department of Computer Science


Consider every day Lists

■ Groceries to be purchased
■ Job to-do list
■ List of assignments for a course
■ Dean's list

■ Can you name some others??


■ So, What is LIST?

Department of Computer Science 4


Linear List
■ Definitions
■ Linear list is a data object whose instances are of the form
(e1,e2,…,en)
■ ei is an element of the list.
■ e1 is the first element, and en is the last element.
■ n is the length of the list.
■ When n = 0, it is called an empty list.
■ Linearly ordered
■ ei precedes ei+1
■ ei follows ei-1
■ e1 comes before e2, e2 comes before e3, and so on.
■ Examples
■ student names order by their alphabets
■ a list of exam scores sorted by descending order

Department of Computer Science 5


The List

■ Upshot:
■ A sequence of zero or more elements
A 1, A 2, A 3, … A N

Department of Computer Science


Properties of List

■ Can have a single element


■ Can have No elements
■ There can be lists of lists

We will look at the list as an abstract


data type
Homogeneous
Finite length
Sequential elements

Department of Computer Science 7


Operations

■ Programmer can provide many different


operations for list

Department of Computer Science


ADT List Operation

■ Insert an element into the list


■ insert(x,3) → 34, 12, 52, x, 16, 12
■ Delete an element from the list
■ delete(52) → 34, 12, x, 16, 12
■ Print List: print the list
■ find: locate the position of an object in a list
■ list: 34,12, 52, 16, 12
■ find(52) → 3
■ Find Kth: retrieve the element at a certain
position

Department of Computer Science 9


ADT for Linear List

Abstract Data Type LinearList {


instances/Data Objects/Structure
ordered finite collections of zero or more elements
operations
Create(): create an empty linear list
Destroy(): erase the list
IsEmpty(): return true if empty, false otherwise
Insert(k,x): insert x just after the kth element
Delete(k,x):delete the kth element and return it in x
Length(): return the list size
Search(x): return the position of x in the list
Output(out): put the list into the output stream out
}

Department of Computer Science 10


Implementation of List ADT

■ Two standard implementations for the list ADT


■ Array-based
■ Linked list

Department of Computer Science


Insertion Operation

■ Insert Operation
■ It is possible to insert new elements at various
positions
■ Insert at start
■ Insert at end
■ Insert at specific position

Department of Computer Science 12


Insertion at start

■ First Step: Check whether List has space or not


■ Second Step: Creating the space for new
element
■ Third Step: Put the element at the empty space
i.e. position

Department of Computer Science 13


Insertion at start

■ First Step: Check whether List has space or not


■ An array List ( e.g. int L[5]; size =5)
■ 0 1 2 3 4
9 4 8 3 7

■ Start Index(si) Last Index(li)

if(si=0 AND li= size-1) then


print List is full

Department of Computer Science 14


Insertion at start

■ Check if List is initially empty


0 1 2 3 4

if (si = -1)

Start Index(si)
Last Index(li)

Department of Computer Science 15


Insertion Operation

■ Check if List is initially empty


0 1 2 3 4
9

Start Index(si)
Last Index(li)

Department of Computer Science 16


Insertion at start

■ Check if space is available at the start of list?


■ Array is not full
■ Array is not empty
■ No space at the start of list, that means last element
is not placed at last index of array (li<size-1)
■ 0 1 2 3 4
9 4 8

■ si li
■ We have to shift elements towards end/shift
right to make the empty slot at the start of
array list
Department of Computer Science 17
Insertion at start

9 4 8

9 9 4 8

9 9 4 8

7 9 4 8

Department of Computer Science 18


Insertion at start

■ check if space is available at the start of list


0 1 2 3 4
4 8 6 12
if (Sindex > 0)

■ si li
4 8 6 12

23 4 8 6 12

Department of Computer Science 19


Insertion at start

Time Complexity: O(n)

Department of Computer Science 20


Insertion at end

Time Complexity: O(n)

Department of Computer Science 21


Deletion from start

Time Complexity: O(n)

Department of Computer Science 22


Deletion from end

Time Complexity: O(n)

Department of Computer Science 23


Display Method

Department of Computer Science 24

You might also like