Circular List Next field in the last node contains a pointer back to the first node rather than null pointer From any point in such a list it is possible.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks, Queues, and Linked Lists
Linear Lists – Linked List Representation
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Data Structure HKOI training /4/2010 So Pak Yeung.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Queue Definition Ordered list with property: –All insertions take place at one end (tail) –All deletions take place at other end (head) Queue: Q = (a 0,
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Lists List L = x0 x1 x2 x3 … xn-1 n = # elements
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
CS Data Structures II Review COSC 2006 April 14, 2017
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
Reference: Vinu V Das, Principles of Data Structures using C and C++
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Linked Structures See Section 3.2 of the text.. First, notice that Java allows classes to be recursive, in the sense that a class can have an element.
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
 Head pointer  Last node  Build a complete linked list  Node deletion  Node insertion  Helpful hints.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Circular Linked List Singly Circular Linked List Doubly Circular Linked List.
STACK Data Structure
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.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
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.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
CS212: Data Structures and Algorithms Lecture # 4 Linked List.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Linked Data Structures
CS505 Data Structures and Algorithms
Stacks and Queues Chapter 4.
Doubly Linked List Review - We are writing this code
UNIT-3 LINKED LIST.
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Stacks and Queues CMSC 202.
Stack and Queue APURBO DATTA.
Stacks Stack: restricted variant of list
Recursion.
Chapter 16-2 Linked Structures
ليست هاي پيوندي.
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
ADT list.
LAB#3 Stacks Nora Albabtin nora albabtin.
General List.
Presentation transcript:

Circular List Next field in the last node contains a pointer back to the first node rather than null pointer From any point in such a list it is possible to reach any other point in the list

Stack as Circular list A circular list can be used to represent a stack or a queue Let stack be a pointer to the last node of circular list bool IsEmpty() { return ( head == NULL); }

Push in circular list void push( int x) { Node * temp; temp = new Node(x); if( IsEmpty()) head = temp; else {temp->next = head->next; head->next = temp; }

Pop in circular list int pop() { int x; Node *temp; if( IsEmpty()) { cout<<“stack empty”; exit(-1); } temp = head->next; x = temp->item; if( temp == head) // only one node on stack head = NULL; else head -> next = temp->next; delete temp; return x; }

Queue as circular list It is easier to represent queue as a circular list than as a linear list As linear list, a queue is specified by two pointers Using a circular list, a queue may be specified by a single pointer q to that list q is back, q->next is front q q->next

fucntions Empty function is same as stack int remove() { Node * temp; temp = q->next; x = temp->item; q->next = temp->next; delete temp; return x; }

insert void insert( int x) { Node *temp = new Node(x); if( isEmpty()) q = temp; else { temp ->next = q->next; q->next = temp; q = temp; }

Josephus problem A group of soldiers surrounded by an overwhelming enemy force. There is no hop for victory without reinforcements, but there is only a single horse available for escape. The soldiers agree to a pact to determine which of them is to escape and summon help. They form a circle and a number n is picked from a hat. One of their names is also picked from a hat. Beginning with the soldier whose name is picked, they begin to count clockwise around the circle. When the count reaches n, that soldier is removed from the circle, and the count begin again with the next solider. The process continues so that each time the count reaches n, another soldier is removed from the circle. Any soldier removed from the circle is no longer counted. The last soldier remaining is to take the horse and escape.

Example Soldiers A,B,C,D,E N = 3 start at A Who is to escape Answer is D

Outline of program Clearly, a circular list in which each node represents one soldier is a natural data structure. Read number n Loop until end of file ( of list of names) read a name and insert in circular list While( there is more than one node) count through n-1 nodes on the list Print the name Delete the n node Print the name of the only node on the list