Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Implementing Data Structures: Lists in Array and Linked Memory, Lecture notes of Data Structures and Algorithms

An in-depth explanation of how to implement lists as a data structure, discussing the use of arrays and linked memory. Topics include adding, removing, and finding elements, as well as analyzing the time complexity of various operations.

What you will learn

  • What are the advantages of using linked memory to implement a list?
  • What are the special cases for positioning the current pointer in a list implemented using an array?
  • How is a list implemented using an array?
  • How does the remove operation work in a list implemented using an array?
  • What is the time complexity of finding an element in a list implemented using an array?

Typology: Lecture notes

2020/2021

Uploaded on 01/12/2021

umargumn
umargumn 🇮🇳

2 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
AL 1
Lecture No.02
Data Structures
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Implementing Data Structures: Lists in Array and Linked Memory and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Lecture No.

Data Structures

Implementing Lists

▪ We have designed the interface for the

List; we now must consider how to

implement that interface.

AL 4

List Implementation

▪ (^) add(9); current position is 3. The new list would thus be: (2, 6, 8, 9, 7, 1) ▪ (^) We will need to shift everything to the right of 8 one place to the right to make place for the new element ‘9’. curre nt 3 siz e 5 step 1:

A 6 8 7

1 2 3 4 5

6 curre nt 4 siz e 6 step 2:

A 6 8 7

1 2 3 4 5

6

notice: current points to new element

Implementing Lists

▪ next():

curre nt 4 siz e 6

A 6 8 7

1 2 3 4 5

6

5

Implementing Lists

▪ There are special cases for positioning

the current pointer:

a. past the last array cell b. before the first cell

▪ We will have to worry about these when

we write the actual code.

Implementing Lists

▪ remove(): removes the element at the

current

index

curre nt 5 siz e 6

A 6 8

1 2 3 4 5

6

5 Step 1: curre nt 5 siz e 5

A 6 8

1 2 3 4 5 Step 2 9 2:

Implementing Lists

find(X): traverse the array until X is located. int find(int X) { int j; for(j=1; j < size+1; j++ ) if( A[j] == X ) break; if( j < size+1 ) { // found X current = j; // current points to where X found return 1; // 1 for true } return 0; // 0 (false) indicates not found }

Implementing Lists

▪ Other operations:

get() → return A[current];

update(X) → A[current] = X;

length() → return size;

back() → current--;

start() → current = 1;

end() → current = size;

Analysis of Array Lists

▪ remove

▪ (^) Worst-case: remove at the beginning, must shift all remaining elements to the left. ▪ (^) Average-case: expect to move half of the elements.

▪ find

▪ (^) Worst-case: may have to search the entire array ▪ (^) Average-case: search at most half the array.

▪ Other operations are one-step.

List Using Linked Memory

▪ Various cells of memory are not allocated

consecutively in memory.

List Using Linked Memory

▪ Various cells of memory are not allocated

consecutively in memory.

▪ Not enough to store the elements of the

list.

▪ With arrays, the second element was right

next to the first element.

List Using Linked Memory

▪ Various cells of memory are not allocated

consecutively in memory.

▪ Not enough to store the elements of the

list.

▪ With arrays, the second element was right

next to the first element.

▪ Now the first element must explicitly tell

us where to look for the second element.

Linked List

▪ Create a structure called a Node.

object nex t

▪ The object field will hold the actual list

element.

▪ The next field in the structure will hold the

starting location of the next node.

▪ Chain the nodes together to form a linked

list.

Linked List

▪ Picture of our list (2, 6, 7, 8, 1) stored as a

linked list:

2 6 8 7 1 hea d curre nt size= 5