the circular linked list: add, change, & delete operations

7
THE CIRCULAR LINKED LIST FRANCISCUS AGNEW ADD, CHANGE, & DELETE OPERATIONS

Upload: franciscus-agnew

Post on 14-Aug-2015

80 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: The Circular Linked List: Add, Change, & Delete Operations

THECIRCULAR

LINKED LIST

FRANCISCUSAGNEW

ADD, CHANGE, & DELETEOPERATIONS

Page 2: The Circular Linked List: Add, Change, & Delete Operations

IntroductionLinked lists come in almost limitless varieties. Depending upon the situation, it is possible to craft a linked list representation for almost any purpose.

In this presentation we will look at one of the more common varieties of linked list implementations:

CIRCULARLINKED LIST

Page 3: The Circular Linked List: Add, Change, & Delete Operations

Typical operations performed on the list are:Add a node to the right of the node pointed to by StartÇAdd a node to the right of the node pointed to by Start AND make it the node pointed to by StartDelete the node to the right of the node pointed to by Start text

DATA

DATA

DATA

DATA

DATA

DATA

START

circularLinked List

A circular linked list has theproperty that its last node linksback to the first node, insteadof being NULL. This thenallows access to the entire liststarting from any node in thelist. Thus, we can think of thelist as not having a first and lastnode. In fact, it is actually betterto think of it as a circle:

CIrcular Linked List?

Page 4: The Circular Linked List: Add, Change, & Delete Operations

Add Operation Details

Allocate a new nodeCopy the link from the node pointed to by Start (red link) TO the new node's link (orange link) Copy the address of the new node to the link of the node pointed to by Start (green link overwrites red link, so red link no longer exists)

DATA

DATA

DATA

DATA

DATA

DATA

START

DATA

circularLinked List

Page 5: The Circular Linked List: Add, Change, & Delete Operations

Add and Change Start Operation Details

To add a new node to the right of the node pointed to by Start AND make it the node pointed to by Start, use the same steps as above, and then add:

DATA

DATA

DATA

DATA

DATA

DATA

START

DATA

Assign the address of the new node to the pointer variable Start (the yellow link replaces the red link from Start)

circularLinked List

Page 6: The Circular Linked List: Add, Change, & Delete Operations

Delete Operation Details

To delete the node to the right of the node pointed to by Start:

DATA

DATA

DATA

DATAcircular

Linked List

DATA

START

P

Copy the link in the node pointed to by the pointer variable Start to a separate pointer P (orange link)Assign the link in the node that is pointed to by P to the link in the node that is pointed to by Start (red link)Free the memory that is pointed to by P for future use (the red links and node no longer exist)

Page 7: The Circular Linked List: Add, Change, & Delete Operations

SUMMARYOF

C I R C U L A R L I N K E D L I S T

Keep Track of Starting PointSince the circular linked list does not have a node that is defined as the end node (i.e. a node in which the link is NULL), we must be careful to keep track of our starting point if we are traversing the list in a search operation. Without the NULL link we could end up in an infinite loop.

Add, Change, & DeleteAdd, change, and delete operations allow the circular linked list to be used either as a Stack (operations add & delete) or a Queue (operations change & delete).

Watch Out For Null PointersWe would also need to be careful that we did not apply these operations to an empty list. To make sure this does not happen, the pointer variable Start could be assigned the value NULL when creating a new list, or when the last node was deleted from the list.