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

Week 9 - Data Structures Self-Referential Structures, Dynamic Memory Allocation, and Linked Lists in C

The document covers the concepts of dynamic memory allocation, self-referential structures, and linked lists in C programming. It outlines the objectives, introduces key data structures, and details functions for inserting and deleting nodes in linked lists. Additionally, it discusses memory management practices to prevent leaks and ensure efficient data handling.

Uploaded by

batam54516
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Week 9 - Data Structures Self-Referential Structures, Dynamic Memory Allocation, and Linked Lists in C

The document covers the concepts of dynamic memory allocation, self-referential structures, and linked lists in C programming. It outlines the objectives, introduces key data structures, and details functions for inserting and deleting nodes in linked lists. Additionally, it discusses memory management practices to prevent leaks and ensure efficient data handling.

Uploaded by

batam54516
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CENG 110 – PROGRAMMING AND

COMPUTING II

Week 9 : Data Structures: Self-Referential


Structures, Dynamic Memory Allocation, and
Linked Lists in C

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


Objectives
• Allocate and free memory dynamically for data objects.
• Form linked data structures using pointers, self-referential
structures and recursion.

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


Outline
12.1 Introduction

12.2 Self-Referential Structures

12.3 Dynamic Memory Management

12.4 Linked Lists


12.4.1 Function insert
12.4.2 Function delete
12.4.3 Functions isEmpty and printList

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


12.1 Introduction
• Dynamic data structures that can grow and shrink at execution time:
– Linked lists are collections of data items “lined up in a row.” You
can insert and delete items anywhere in a linked list.
– Stacks are important in compilers and operating systems. You
can insert and delete items only at one end of a stack, known as
its top.
– Queues represent waiting lines. You can insert only at the
queue’s back and delete only from its front. The back and front
are known as the queue’s tail and head.
– Binary trees facilitate high-speed searching and sorting of data,
efficiently eliminating duplicate data items and compiling
expressions into machine language.
• Each has many other interesting applications

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


12.2 Self-Referential Structures (1 of 2)
• Contains a pointer member that points to a structure of the same type
– struct node {
int data;
struct node *nextPtr;
};
– nextPtr is a link—it points to another struct node
– Pointer is same type as the struct we’re defining, hence “self-
referential structure”
– nextPtr used to link a struct node to another struct node

• Can link self-referential structure objects to form lists, queues, stacks and
trees

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


12.2 Self-Referential Structures (2 of 2)
• The \ in the last node’s link member is a NULL pointer

• Represents end of a data structure

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


12.3 Dynamic Memory Management (1 of 3)
• Dynamic memory management has two components:
– obtaining more memory at execution time to hold new nodes
– releasing memory no longer needed

malloc Function
• Request memory by passing to malloc the number of bytes to
allocate
• If successful, it returns a void * pointer to the allocated memory
• Commonly used with sizeof
– newPtr = malloc(sizeof(struct node));

• Not guaranteed to initialize memory, but many implementations do


for security
Copyright © 2022 Pearson Education, Inc. All Rights Reserved
12.3 Dynamic Memory Management (2 of 3)
• If no memory is available, malloc returns NULL
– Always test for a NULL pointer before accessing the dynamically
allocated memory

free Function

• Return dynamic memory to the system when no longer needed


– free(newPtr);

• After deallocating memory, set the pointer to NULL


– Prevents accidentally referring to that memory

• Not freeing dynamically allocated memory cause a “memory leak”


– Could eventually lead to running out of memory

• Accessing freed memory typically causes a program to crash

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


12.3 Dynamic Memory Management (3 of 3)
Functions calloc and realloc

• Used to create and modify the size of dynamic arrays

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


12.4 Linked Lists (1 of 3)
• Linear collections of self-referential struct objects
(nodes) connected by pointer links—hence the term
“linked” list
• Access a via a pointer to the first node
• Access subsequent nodes via the nodes’ pointer link
members
• Store data in a linked list by creating each node as
necessary
• Stacks and queues are also linear data structures
– Constrained versions of linked lists.
Copyright © 2022 Pearson Education, Inc. All Rights Reserved
12.4 Linked Lists (2 of 3)
Arrays v s Linked Lists
ersu

• Linked lists provide several advantages:


– A linked list’s length can increase or decrease as necessary. Arrays are
fixed-size.
– An array may contain more elements than needed, but this can waste
memory. Using linked lists can save memory, however, the pointers in a
list’s nodes require additional memory and memory allocation incurs the
overhead of function calls.
– Fixed-size arrays can become full. Linked lists become full only when
the system has insufficient memory to satisfy dynamic storage-
allocation requests.
– Linked lists can be maintained in sorted order by inserting each new
element at the appropriate point in the list. Inserting into and deleting
from a sorted array can be time-consuming—all elements following the
inserted or deleted element must be shifted appropriately.
Copyright © 2022 Pearson Education, Inc. All Rights Reserved
12.4 Linked Lists (3 of 3)
Arrays Are Faster for Direct Element Access

• Array elements are contiguous in memory and can be accessed directly by


index

• Linked lists do not afford such immediate access to their elements


Illustrating a Linked List

• Linked-list nodes generally are not contiguous in memory.

• Logically, the nodes appear to be contiguous, as in:

Implementing a Linked List

• Figure 12.1 manipulates a list of characters.


Copyright © 2022 Pearson Education, Inc. All Rights Reserved
12.4.1 Function insert (1 of 5)
• Example inserts characters in the list in alphabetical
order
• Function insert receives as an argument the
address of the pointer to the list’s first node and a
character to insert
• Enables insert to modify the caller’s pointer to the
list’s first node to point to a new first node when a data
item is placed at the front
• Passing a pointer’s address creates a pointer to a
pointer—sometimes called double indirection

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


12.4.1 Function insert (2 of 5)
insert performs the following steps:

• Call malloc to create a new node and assign newPtr the allocated
memory’s address

• If memory was allocated


– Assign the character to insert to newPtr->data, and NULL to
newPtr->nextPtr
– Always assign NULL to a new node’s link member initially

• previousPtr and currentPtr store locations of the node preceding and


after the insertion point
• Initialize previousPtr to NULL and currentPtr to *sPtr—the first
node’s address

• Locate the new value’s insertion point

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


12.4.1 Function insert (3 of 5)
– While currentPtr is not NULL and the value to insert is greater than
currentPtr->data, assign currentPtr to previousPtr, then
advance currentPtr to the list’s next node

• Insert the new value in the list


– If previousPtr is NULL, insert the new node as the first node
▪ Assign *sPtr to newPtr->nextPtr (new node’s link points to the
former first node)
▪ Assign newPtr to *sPtr so startPtr in main points to the new
first node.
– Otherwise, insert the new node in place
▪ Assign newPtr to previousPtr->nextPtr (the previous node
points to the new node)
▪ Assign currentPtr to newPtr->nextPtr (the new node link
points to the current node)

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


12.4.1 Function insert (4 of 5)
• For simplicity, we implemented function insert (and
other similar functions in this chapter) with a void return
type
– Function malloc may fail to allocate the requested
memory
– It would be better for insert to return a status that
indicates whether the operation was successful

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


12.4.1 Function insert (5 of 5)
• Illustrating an Insert
– Diagram illustrates
inserting a node
containing 'C' into an
ordered list
– Part (a) shows the list
and the new node just
before the insertion
– Part (b) shows the result
of inserting the new node
– Dotted arrows represent
the reassigned pointers.

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


12.4.2 Function delete (1 of 3)
• Receives the address of the pointer to the list’s first node and a
character to delete.

• Performs the following steps:


– If the character to delete matches the first node’s character, remove the
first node
▪ Assign *sPtr to tempPtr, which we’ll use to free the node’s
memory
▪ Assign (*sPtr)->nextPtr to *sPtr, so startPtr in main
points to what previously was the second node
▪ Call free to deallocate the memory pointed to by tempPtr
▪ Return the deleted character
– Otherwise, initialize previousPtr with *sPtr and currentPtr with
(*sPtr)->nextPtr
– Locate the character to delete

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


12.4.2 Function delete (2 of 3)
▪ While currentPtr is not NULL and the value to delete is not equal
to currentPtr->data, assign currentPtr to previousPtr
and assign currentPtr->nextPtr to currentPtr
▪ If currentPtr is not NULL, return the deleted character
– Assign currentPtr to tempPtr – used to deallocate the
node
– Assign currentPtr->nextPtr to previousPtr-
>nextPtr – connects the nodes before and after the one
being removed
– Free the node pointed to by tempPtr, then return the deleted
character
– If nothing has been returned, return the null character ('\0') to
signify the character was not found in the list

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


12.4.2 Function delete (3 of 3)
• Illustrating a Delete
– Diagram illustrates deleting the
node containing 'C' from a
linked list
– Part (a) shows the list before
deletion
– Part (b) shows the link
reassignments
– tempPtr is used to free the
memory allocated to the node
that stores 'C'.
– We do not set tempPtr to NULL
because it’s a local variable, and
the function returns immediately
after freeing the memory

Copyright © 2022 Pearson Education, Inc. All Rights Reserved


12.4.3 Functions isEmpty and printList
• isEmpty is a predicate function—it does not alter the list
– It determines whether the list is empty—the pointer to the first node is
NULL
– If the list is empty, returns 1; otherwise, returns 0
• printList prints a list
– currentPtr parameter receives a pointer to the list’s first node
– If empty, prints "List is empty." and terminates
– Otherwise, print the list’s data
– While currentPtr is not NULL
▪ prints currentPtr->data
▪ assigns currentPtr->nextPtr to currentPtr to advance to
the next node
– If the link in the last node of the list is not NULL, the printing algorithm
will try to print past the end of the list, which is a logic error
Copyright © 2022 Pearson Education, Inc. All Rights Reserved

You might also like