Linked Lists, Queues, and Stacks Linked Lists: 5 3 8 Head

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Linked Lists

Linked Lists, Queues, Dynamic data structure


and Stacks Size is not fixed at compile time

Each element of a linked list:


CSE 130: Introduction to C Programming
Fall 2004 holds a value

points to the next list element

Linked List Illustration List Operations


1. Create list

2. Destroy list
head 3 5 8
3. Retrieve value at specified position

4. Insert value at specified position

5. Delete value at specified position

6. Get number of elements in list

Sample Code List Creation


struct listNode

{ void initializeList()
int value;
{
listNode *next;
head = NULL;
};
numItems = 0;

}
static listNode *head; /* points to first list item */

static int numItems; /* number of list elements */


List Deletion List Retrieval
void deleteList() int retrieve (int pos)
{ { /* Assumption: pos < list length */
listNode *temp = NULL; listNode *ptr = head;
while (head != NULL)
int i;
{
temp = head;
for (i = 0; i < pos; i++)
head = head->next;
ptr = ptr->next;
free(temp);
}
numItems = 0; return ptr->value;
} }

Locating The
Inserting Values
Insertion Point

Two steps:
If the list is sorted, this requires a loop
Locate insertion point
We want a pointer to the node BEFORE the
Insert new list element
insertion point
Step two requires some care!

Inserting The
Order Matters!
New Element
insPt insPt
insPt->next is our only
insPt points to the
reference to the rest
node BEFORE the
insertion point 3 5 of the list 3 5
If we change that
newEl points to the
first, we lose the rest
new list element
newEl 4 of the list! newEl 4
Algorithm: First Try Algorithm: Second Try

insPt insPt

insPt->next = newEl; 3 5 newEl->next = insPt->next; 3 5


newEl->next = insPt->next;
1 insPt->next = newEl;
2 1

newEl 4 2 newEl 4

Algorithm Summary Removing An Element

Similar to inserting an element

1. Locate insertion point Three steps:

2. Set next pointer of new element Locate the element to be removed

3. Insert new element Redirect pointers

Delete the selected element

Finding The
Pointers for Removal
Removal Point

Depending on the type of list, this may prevPtr delPtr


require a loop

We need two pointers:


3 5 8
delPtr — points to node to remove

prevPtr — points to node BEFORE delPtr


Removal Steps Algorithm: First Try
delPtr
To remove a list element, we need to: prevPtr 1

Set the preceding element to point to the 5


following element of the list 3 2 8
Delete the former list element

As with list insertion, order matters! delPtr->next = null;


delete delPtr;
prevPtr->next = delPtr->next;

Algorithm: Second Try Algorithm Summary


delPtr
prevPtr 2
1. Locate element to be removed
5 2. Re-route pointers around element
3 1 8
3. Perform cleanup

prevPtr->next = delPtr->next; 4. Delete element


delPtr->next = null;
delete delPtr;

Insertion Summary Removal Summary

Find removal point


Find insertion point
Use two pointers
Set the new element to point to the rest of
the list Re-route pointers around element to be
removed
Insert the new element
Clean up and delete the unwanted element
List Variations Queues

Doubly-linked list
first-in, first-out (FIFO) data structure
Each node also contains a pointer to the
Real-world examples:
preceding list node
grocery checkout, on/off-ramp
Circular linked lists
Programming examples
Last node points to first node, not NULL
print jobs, processes running on a CPU
One pointer (to the tail) stores entire list

Queue Operations Straight Queues

1. Create queue
(as opposed to circular queues)
2. Destroy queue
Maintain two pointers
3. Enqueue (insert element at tail)
one to head of queue (for removal)
4. Dequeue (remove element at head)
one to tail of queue (for insertion)
5. Get number of elements

Queue Insertion Queue Removal

Special case: empty queue Special case: only one element in queue

set head and tail to point to new element set tail to point to NULL

Otherwise: Otherwise:

1. newNode->next = tail->next; /*or NULL*/ 1. newPtr = head;

2. tail->next = newNode; 2. head = head->next;


Circular Queues Stacks

last-in, first-out (LIFO) data structure


Last node points to first node
Real-world example:
Can represent a circular queue with one
stack of books
pointer (to tail)
Programming examples:
Head of queue is just tail->next
activation stack (for function calls)

Stack Operations Stack Insertion (Push)


1. Create stack

2. Destroy stack Very easy — just insert at the head:

3. Push (insert object)

4. Pop (remove object) newNode->next = head;

5. Peek (view object on top of stack) head = newNode;

6. Get number of elements

Array-Based
Stack Deletion (Pop)
Implementations
Just as easy as insertion
Stacks and queues are also easy to
implement using arrays

oldNode = head; Track indices of stack top, or queue head and


tail
head = head->next;
Problem is that array is still of fixed size

You might also like