Lecture 6 nodes
Lecture 6: Dynamic Data Structure - Linked Lists
Introduction to Linked Lists
A linked list is a dynamic data structure used to store elements in a sequence.
Each element in a linked list is called a node.
Nodes are connected using pointers, allowing efficient insertion and deletion of elements.
Array vs. Linked List
Array:
Fixed size determined at runtime, e.g., employee database (empdb).
Deleting records requires marking them as deleted.
Cannot add more records if the array is full.
Linked List:
Dynamic size that can increase/decrease at runtime.
Utilizes
mallocfor creating nodes andfreefor removing them.Requires linking nodes together through pointers.
The Node Structure
A node includes two components:
Data field: Stores the data (e.g., an integer, an employee record).
Pointer field: Points to the next node, creating a link between nodes.
Example C struct for a node:
typedef struct listNode {
char data;
struct listNode *nextPtr;
} ListNode;Creating a Node Dynamically
To create and insert a new node, use
malloc:Allocate memory for the new node and store data in it.
Display the contents and the address of the node.
Example Code:
ListNode* newPtr = malloc(sizeof(ListNode));
printf("Enter a character: ");
scanf("%c", &value);
if (newPtr != NULL) {
newPtr->data = value;
newPtr->nextPtr = NULL;
}Running the ListNode.c Program
Upon running the
ListNode.cprogram, the addresses of nodes vary with each execution:Example outputs show data IDs and memory addresses.
Each run displays the memory location where the node is created dynamically.
Class Questions
What is stored in
newPtr?It stores the address of the newly created node.
Data types in statements:
newPtr->value = 8: Data type stored is int (value).newPtr->nextPtr = NULL: Data type is a pointer to a ListNode.newPtr->data = NULL: Data type is char.
Characteristics of Linked Lists
A linked list consists of a series of nodes, each containing:
A data field (any data type, e.g.,
int).A pointer variable pointing to the next node.
Head Pointer:
Points to the first node of the list.
End Condition:
The last node points to NULL, indicating the end of the list.
Linked List Overview
Basic Operations:
Insert, find, delete, print, and edit nodes.
Variations of Linked Lists:
Stacks: Utilize LIFO (Last In, First Out) principle.
Queues: Implement FIFO (First In, First Out) principle for process scheduling.
Simple Linked List Node Structure
Each linked list consists of nodes defined as:
struct listNode {
char data;
struct listNode *nextPtr;
};Here,
dataholds a character andnextPtrserves as a pointer to the next node.
Operations on Linked List
Use a double pointer for ListNode (
sPtr) to manipulate nodes by reference.A single pointer (
currentPtr) is used for traversing. It holds the address of the first node.
Code for Inserting a Node
The
insertfunction of the linked list uses a double pointer to update the head pointer effectively.If the list is empty, the first node starts with a NULL value.
Homework Question
Write a C program to create a node containing:
An employee record as data.
Set the pointer part to NULL.
Display the contents of the node and its address.