Circular Linked List. COMP104 Circular Linked List / Slide 2 Circular Linked Lists * A Circular Linked List is a special type of Linked List * It supports.

Slides:



Advertisements
Similar presentations
Data Structure Lecture-5
Advertisements

Chapter 17 Linked List Saurav Karmakar Spring 2007.
Circular Linked List. Agenda  What is a Circularly linked list  Why a Circular Linked List  Adding node in a new list  Adding node to list with nodes.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor.
Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use.
Stacks. COMP104 Slide 2 Stacks * A stack, S, is a data structure that supports: n push(x) make x the top element in stack S n Pop Remove the top item.
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class List { public: List(); // constructor List(const.
Classes with dynamic members. int x; void f() { x=10; } main() { x=0; f(); } ‘Class’ matters! int x; void f() { int x; x=10; } main() { x=0; f(); } class.
Abstract Data Type Example l One more example of ADT: l integer linked list using class l A class with dynamic objects: l Copy constructor l Destructor.
Concept of lists and array implementations of lists.
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class list { public: list(); // constructor list(const.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
Linked Lists. COMP104 Lecture 33 / Slide 2 Linked Lists: Basic Idea * Data may be stored consecutively in a linked list. * Each element of the linked.
Abstract Data Type: List (optional, not required).
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Classes with dynamic objects List as a (dynamic) class.
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Motivation * Doubly linked lists are useful for playing video and sound files with “rewind”
List, (dynamic) linked list Let’s first forget about ‘classes’, but only a dynamic list. We make lists with ‘classes’ afterwards.
COMP104 Linked List Algorithms / Slide 1 Some Simple Algorithms on Linked Lists * Write a function that returns the length of a given list. * Write a boolean.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
Algorithms and Data Structures
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Circular Linked List Singly Circular Linked List Doubly Circular Linked List.
Programming Linked Lists. Collections Store collection of data  Online store - Items  University – Students  Library – books Until now we used arrays.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
LINEAR LINKED LISTS The disadvantages of arrays: 1.The size of the array is fixed. 2.Large size of array??? 3. Inserting and deleting elements. If the.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
LINKED LISTS.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
Programming Circular Linked List.
UNIT – I Linked Lists.
What’s New? Six homework programming assignments. New: five
Exercise 1 From Lec80b-Ponter.ppt.
Doubly Linked List Review - We are writing this code
Chapter 4 Linked Lists.
Chapter 4 Linked Lists
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Summary on linked lists
Chapter 16-2 Linked Structures
as an abstract data structure and
Chapter 4 Linked Lists.
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Programming Abstractions
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
Linked Lists.
General List.
Linked Lists.
Data Structures & Programming
Presentation transcript:

Circular Linked List

COMP104 Circular Linked List / Slide 2 Circular Linked Lists * A Circular Linked List is a special type of Linked List * It supports traversing from the end of the list to the beginning by making the last node point back to the head of the list * A Rear pointer is often used instead of a Head pointer Rear

COMP104 Circular Linked List / Slide 3 Motivation * Circular linked lists are usually sorted * Circular linked lists are useful for playing video and sound files in “looping” mode * They are also a stepping stone to implementing graphs, an important topic in comp171

#include using namespace std; struct Node{ int data; Node* next; }; typedef Node* NodePtr; Circular Linked List Definition

COMP104 Circular Linked List / Slide 5 Circular Linked List Operations * insertNode(NodePtr& Rear, int item) //add new node to ordered circular linked list * deleteNode(NodePtr& Rear, int item) //remove a node from circular linked list * print(NodePtr Rear) //print the Circular Linked List once

COMP104 Circular Linked List / Slide 6 void print(NodePtr Rear){ NodePtr Cur; if(Rear != NULL){ Cur = Rear->next; do{ cout data << " "; Cur = Cur->next; }while(Cur != Rear->next); cout << endl; } Traverse the list Rear

COMP104 Circular Linked List / Slide 7 Insert Node * Insert into an empty list Rear 10 New NotePtr New = new Node; New->data = 10; Rear = New; Rear->next = Rear;

COMP104 Circular Linked List / Slide 8 * Insert to head of a Circular Linked List Rear New New->next = Cur; // same as: New->next = Rear->next; Prev->next = New; // same as: Rear->next = New; PrevCur

COMP104 Circular Linked List / Slide 9  Insert to middle of a Circular Linked List between Pre and Cur Prev New New->next = Cur; Prev->next = New; RearCur

COMP104 Circular Linked List / Slide 10 * Insert to end of a Circular Linked List Rear New New->next = Cur;// same as: New->next = Rear->next; Prev->next = New;// same as: Rear->next = New; Rear = New; Prev Cur

void insertNode(NodePtr& Rear, int item){ NodePtr New, Cur, Prev; New = new Node; New->data = item; if(Rear == NULL){// insert into empty list Rear = New; Rear->next = Rear; return; } Prev = Rear; Cur = Rear->next; do{// find Prev and Cur if(item data) break; Prev = Cur; Cur = Cur->next; }while(Cur != Rear->next); New->next = Cur;// revise pointers Prev->next = New; if(item > Rear->data) //revise Rear pointer if adding to end Rear = New; }

COMP104 Circular Linked List / Slide 12 * Delete a node from a single-node Circular Linked List Rear = Cur = Prev Rear = NULL; delete Cur; 10

COMP104 Circular Linked List / Slide 13 Delete Node * Delete the head node from a Circular Linked List Rear Cur Prev->next = Cur->next; // same as: Rear->next = Cur->next delete Cur; Prev

COMP104 Circular Linked List / Slide 14  Delete a middle node Cur from a Circular Linked List Prev Rear Cur Prev->next = Cur->next; delete Cur;

COMP104 Circular Linked List / Slide 15 * Delete the end node from a Circular Linked List Rear Prev->next = Cur->next; // same as: Rear->next; delete Cur; Rear = Prev; Prev Cur

void deleteNode(NodePtr& Rear, int item){ NodePtr Cur, Prev; if(Rear == NULL){ cout << "Trying to delete empty list" << endl; return; } Prev = Rear; Cur = Rear->next; do{// find Prev and Cur if(item data) break; Prev = Cur; Cur = Cur->next; }while(Cur != Rear->next); if(Cur->data != item){// data does not exist cout << "Data Not Found" << endl; return; } if(Cur == Prev){// delete single-node list Rear = NULL; delete Cur; return; } if(Cur == Rear) // revise Rear pointer if deleting end Rear = Prev; Prev->next = Cur->next;// revise pointers delete Cur; }

void main(){ NodePtr Rear = NULL; insertNode(Rear, 3); insertNode(Rear, 1); insertNode(Rear, 7); insertNode(Rear, 5); insertNode(Rear, 8); print(Rear); deleteNode(Rear, 1); deleteNode(Rear, 3); deleteNode(Rear, 8); print(Rear); insertNode(Rear, 1); insertNode(Rear, 8); print(Rear); } Result is: