Programming Circular Linked List.

Slides:



Advertisements
Similar presentations
CS 367 – Introduction to Data Structures
Advertisements

Data Structure Lecture-5
Chapter 17 Linked List Saurav Karmakar Spring 2007.
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.
 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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
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.
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.
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.
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.
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.
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.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
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.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
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 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.
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
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.
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.
Linked lists.
Programming Abstractions
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 17: Linked Lists.
Chapter 16 Linked Structures
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
Linked Lists.
Lists.
Linked Lists.
General List.
Linked lists.
Linked Lists.
Data Structures & Programming
Presentation transcript:

Programming Circular Linked List

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 10 20 40 55 70 Rear

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

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

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

Traverse the list 10 20 40 55 70 void print(NodePtr Rear){ NodePtr Cur; if(Rear != NULL){ Cur = Rear->next; do{ cout << Cur->data << " "; Cur = Cur->next; }while(Cur != Rear->next); cout << endl; } 10 20 40 55 70 Rear

Insert Node Insert into an empty list NotePtr New = new Node; New->data = 10; Rear = New; Rear->next = Rear; Rear 10 New

Insert to head of a Circular Linked List New->next = Cur; // same as: New->next = Rear->next; Prev->next = New; // same as: Rear->next = New; 10 20 40 55 70 New Cur Prev Rear

Insert to middle of a Circular Linked List between Pre and Cur New->next = Cur; Prev->next = New; 10 20 55 70 40 Prev Cur Rear New

Insert to end of a Circular Linked List New->next = Cur; // same as: New->next = Rear->next; Prev->next = New; // same as: Rear->next = New; Rear = New; 10 20 40 55 70 Prev Cur New Rear

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 <= Cur->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

Delete a node from a single-node Circular Linked List Rear = NULL; delete Cur; 10 Rear = Cur = Prev

Delete Node Delete the head node from a Circular Linked List 10 20 40 Prev->next = Cur->next; // same as: Rear->next = Cur->next delete Cur; 10 20 40 55 70 Rear Prev Cur

Delete a middle node Cur from a Circular Linked List Prev->next = Cur->next; delete Cur; 10 20 40 55 70 Rear Prev Cur

Delete the end node from a Circular Linked List Prev->next = Cur->next; // same as: Rear->next; delete Cur; Rear = Prev; 10 20 40 55 70 Cur Prev Rear

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 <= Cur->data) break; Prev = Cur; Cur = Cur->next; }while(Cur != Rear->next); if(Cur->data != item){ // data does not exist cout << "Data Not Found" << endl; if(Cur == Prev){ // delete single-node list Rear = NULL; delete Cur; if(Cur == Rear) // revise Rear pointer if deleting end Rear = Prev; Prev->next = Cur->next; // revise pointers

Result is: 1 3 5 7 8 5 7 1 5 7 8 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); } Result is: 1 3 5 7 8 5 7 1 5 7 8