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

L27_eng

Lecture 27 of Computer Programming 143 focuses on dynamic data structures, including self-referencing structures, dynamic memory allocation, and linked lists. It covers the characteristics and applications of dynamic data types such as linked lists, stacks, queues, and trees, emphasizing the importance of pointers and memory management. The lecture also discusses operations related to linked lists, such as searching, insertion, and deletion.

Uploaded by

unathimaxaulane
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)
6 views

L27_eng

Lecture 27 of Computer Programming 143 focuses on dynamic data structures, including self-referencing structures, dynamic memory allocation, and linked lists. It covers the characteristics and applications of dynamic data types such as linked lists, stacks, queues, and trees, emphasizing the importance of pointers and memory management. The lecture also discusses operations related to linked lists, such as searching, insertion, and deletion.

Uploaded by

unathimaxaulane
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/ 27

Computer Programming 143 – Lecture 27

Dynamic Data Structures I

Faculty of Engineering

(Eng) CP143 Lecture 27 1 / 19


Copyright & Disclaimer

Copyright

Copyright © 2024 Stellenbosch University


All rights reserved

Disclaimer

This content is provided without warranty or representation of any kind. The use of the content
is entirely at your own risk and Stellenbosch University (SU) will have no liability directly or indir-
ectly as a result of this content.

The content must not be assumed to provide complete coverage of the particular study material.
Content may be removed or changed without notice.

(Eng) CP143 Lecture 27 2 / 19


Module Overview

Chap 1: Introduction to
Computer Systems Chap 6: Arrays

Chap 2: Introduction to C Chap 7: Pointers

Chap 3: Structured
Program Development Chap 10: Structures

Chap 9: Formatted
Input/Output Chap 11: File Processing

Chap 4: Program Control Chap 12: Data Structures

Chap 13 & 15: Sorting


Chap 5: Functions Algorithms and Big O

(Eng) CP143 Lecture 27 3 / 19


Lecture Overview

1 Introduction (12.1)

2 Self-referencing structures (12.2)

3 Dynamic memory allocation (12.3)

4 Linked lists (12.4)

(Eng) CP143 Lecture 27 4 / 19


12.1 Introduction

Static data types


Memory requirements remains unchanged for the entire program execution
Single subscript arrays
Multiple subscript arrays
Structures

Dynamic data types


Memory requirements change during the execution of the program
Linked lists
Stacks
Queues
Trees

(Eng) CP143 Lecture 27 5 / 19


12.1 Introduction

Linked lists
Number of elements stored in a “line”
Insertion and deletion can happen at any point

Stacks
Used by compilers and operating systems
Insertion and deletion only takes place at one point (FILO)

Queues
Used as waiting queue/buffer
Data inserted at one end (tail) and removed at the other (head) (FIFO)

Trees
Used to implement very efficient search and sort algorithms
Binary trees are the most basic type

(Eng) CP143 Lecture 27 6 / 19


12.1 Introduction

Dynamic data structures ...


Each of the data structures have unique and interesting applications
Almost any program of any significance contains these data structures
Dynamic data structures rely HEAVILY on pointers and structures
Good understanding of pointers and structures is very important

(Eng) CP143 Lecture 27 7 / 19


12.2 Self-referencing structures

Self-referencing structures
Self-referencing structures contains a member that is a pointer of the same type as the
structure
Pointer nextPtr points to a variable of the same structure type
Pointer nextPtr can be seen as a link between two elements of that type

struct listNode {
ListNode
char data; data
struct listNode *nextPtr;
};
typedef struct listNode ListNode; nextPtr

Self-referencing structures
Self-referencing structures can therefore be “linked” together to form more useful data
structures that can be used more effectively
A NULL pointer indicates that there is no subsequent node
A NULL pointer usually indicates the end of the data structure

(Eng) CP143 Lecture 27 8 / 19


12.3 Dynamic memory allocation

Dynamic structures and dynamic memory allocation


Static data structures require variable declaration which ensures that memory is allocated
statically
Dynamic data structures require dynamic memory allocation and memory management
Dynamic memory allocation makes it possible for a program to request more memory
Dynamic memory management is made possible by the following functions:
malloc() – memory allocation
free() – memory deallocation
sizeof() – calculates memory requirements

(Eng) CP143 Lecture 27 9 / 19


12.3 Dynamic memory allocation

void * malloc(size_t size);


The argument defines the number of bytes of memory that will be allocated
The number of bytes that are required are generally calculated using the sizeof() function
The function returns a generic pointer that points to the block of memory that has been
allocated – i.e. the address of the first memory position in the block
The malloc() function returns a NULL pointer if it was unable to allocate the memory

void free( void *ptr );


The argument provides the pointer to memory that should be returned to the operating
system so that it can be reused
All allocated memory must be “freed” once it is no longer used – otherwise memory leaks
will occur

(Eng) CP143 Lecture 27 10 / 19


12.4 Linked lists

Linked lists
A linked list is a linear collection of self-referential structures
Each element that forms part of the list is called a node
Nodes are connected to one another via pointers called links
Linked lists are accessed via a pointer to the first element
Subsequent nodes are accessed via the link pointer member of the current node

startPtr ListNode ListNode ListNode ListNode


data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr

(Eng) CP143 Lecture 27 11 / 19


12.4 Linked lists

Linked lists
By convention, the link in the last node must be set to NULL
This indicates that the current element is the last element
Data is stored in linked lists dynamically
Nodes are created as they are required

A node can contain any data – even other structures – but must contain a self-referential
member
NB! Linked lists can only be accessed via a pointer to the first node – the linked
list is “lost” if one incorrectly make assignments to this pointer

startPtr ListNode ListNode ListNode ListNode


data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr

(Eng) CP143 Lecture 27 12 / 19


12.4 Linked lists

The most important functions associated with linked lists are


searching
insertion
deletion

(Eng) CP143 Lecture 27 13 / 19


12.4 Searching Linked Lists

value = 5;
currentPtr = startPtr;
while ( currentPtr != NULL && value != currentPtr->data ) {
currentPtr = currentPtr->nextPtr;
}

currentPtr

startPtr
data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr

(Eng) CP143 Lecture 27 14 / 19


12.4 Searching Linked Lists

value = 5;
currentPtr = startPtr;
while ( currentPtr != NULL && value != currentPtr->data ) {
currentPtr = currentPtr->nextPtr;
}

currentPtr

startPtr
data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr

(Eng) CP143 Lecture 27 14 / 19


12.4 Searching Linked Lists

value = 5;
currentPtr = startPtr;
while ( currentPtr != NULL && value != currentPtr->data ) {
currentPtr = currentPtr->nextPtr;
}

currentPtr

startPtr
data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr

(Eng) CP143 Lecture 27 14 / 19


12.4 Linked List Insertion

newPtr = malloc( sizeof( ListNode ) );


newPtr->data = value;
newPtr->nextPtr = NULL;
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;

previousPtr currentPtr

startPtr
data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr

newPtr

(Eng) CP143 Lecture 27 15 / 19


12.4 Linked List Insertion

newPtr = malloc( sizeof( ListNode ) );


newPtr->data = value;
newPtr->nextPtr = NULL;
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;

previousPtr currentPtr

startPtr
data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr

newPtr
data
7
nextPtr

(Eng) CP143 Lecture 27 15 / 19


12.4 Linked List Insertion

newPtr = malloc( sizeof( ListNode ) );


newPtr->data = value;
newPtr->nextPtr = NULL;
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;

previousPtr currentPtr

startPtr
data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr

newPtr
data
7
nextPtr

(Eng) CP143 Lecture 27 15 / 19


12.4 Linked List Insertion

newPtr = malloc( sizeof( ListNode ) );


newPtr->data = value;
newPtr->nextPtr = NULL;
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;

previousPtr currentPtr

startPtr
data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr

newPtr
data
7
nextPtr

(Eng) CP143 Lecture 27 15 / 19


12.4 Linked List Deletion

tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
free( tempPtr );

previousPtr currentPtr

startPtr
data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr

tempPtr

(Eng) CP143 Lecture 27 16 / 19


12.4 Linked List Deletion

tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
free( tempPtr );

previousPtr currentPtr

startPtr
data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr

tempPtr

(Eng) CP143 Lecture 27 16 / 19


12.4 Linked List Deletion

tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
free( tempPtr );

previousPtr currentPtr

startPtr
data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr

tempPtr

(Eng) CP143 Lecture 27 16 / 19


12.4 Linked List Deletion

tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
free( tempPtr );

previousPtr currentPtr

startPtr
data data data
1 4 9
nextPtr nextPtr nextPtr

tempPtr

(Eng) CP143 Lecture 27 16 / 19


12.4 Linked lists

See Fig. 12.1 in Deitel & Deitel for a complete example

Linked lists vs. arrays


The information in linked lists can be stored in arrays, but linked lists provide many
advantages:
Linked lists are applicable when the number of elements in the list at any given time is not known
in advance
Linked lists are dynamic and its size can change with the requirements; arrays are static
Arrays can get full; linked lists can almost always grow to accommodate more data

(Eng) CP143 Lecture 27 17 / 19


Perspective

Today
Dynamic Data Structures I
Self-referential structures
Dynamic data allocation
Linked lists

Next lecture
Dynamic Data Structures II
Stacks
Queues

(Eng) CP143 Lecture 27 18 / 19


Homework

1 Study Sections 12.1-12.4 in Deitel & Deitel


2 Do Self Review Exercise 12.4 in Deitel & Deitel
3 Do Exercises 12.6, 12.7 in Deitel & Deitel

(Eng) CP143 Lecture 27 19 / 19

You might also like