EENG212 Algorithms and Data Structures

Slides:



Advertisements
Similar presentations
Linked Lists.
Advertisements

CSCE 3110 Data Structures & Algorithm Analysis
Data Structures ADT List
Linked Lists Linked Lists Representation Traversing a Linked List
LINKED LIST, STACKS AND QUEUES Saras M Srivastava, PGT – Comp. Sc. Kendriya Vidyalaya TengaValley.
Data Structures Using C++
CSEB324 Data Structures & Algorithms
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Linked List Variations
Data Structure Lecture-5
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.
Doubly-linked list library.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Data Structures: Doubly Linked List1 Doubly linked list l The linear linked list is accessed from the first node each time we want to insert or delete.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Ceng-112 Data Structures I 1 Chapter 3 Linear Lists.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
Variations of Linked Lists CS 308 – Data Structures.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Reference: Vinu V Das, Principles of Data Structures using C and C++
Implementation of Linked List For more notes and topics visit: eITnotes.com.
UNIT 1 Data Structures Using C Linked List By Rohit Khokher Department of Computer Science, Vidya College of Engineering, Meerut, India.
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
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.
Data Structures Using Java1 Chapter 4 Linked Lists.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
Linked Lists EENG 212 ALGORITHMS And DATA STRUCTURES.
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.
Data Structures Using C++1 Chapter 5 Linked Lists.
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,
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
CS 201 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - I Text: Read Weiss, §3.1 – 3.5 1Izmir University of Economics.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
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.
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.
Linked List. LINKED LIST Link is collection of similar type of elements. There are two ways of maintaining a list in memory. The first way is store the.
Data Structure & Algorithms
Stacks This presentation shows – how to implement the stack – how it can be used in real applications.
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.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
LINKED LISTS.
Programming Circular Linked List.
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Lecture - 6 On Data Structures
UNIT-3 LINKED LIST.
Linked-list.
Linked lists.
Data Structures 7th Week
Algorithms and Data Structures
Recursion.
Chapter 16-2 Linked Structures
EENG 212 ALGORITHMS And DATA STRUCTURES
Binary Trees (and Big “O” notation)
CSE 1002 Fundamentals of Software Development 2 More Linked Structures
General List.
Linked lists.
Presentation transcript:

EENG212 Algorithms and Data Structures Circular Linked List EENG212 Algorithms and Data Structures

Circular Linked Lists In linear linked lists if a list is traversed (all the elements visited) an external pointer to the list must be preserved in order to be able to reference the list again. Circular linked lists can be used to help the traverse the same list again and again if needed. A circular list is very similar to the linear list where in the circular list the pointer of the last node points not NULL but the first node.

Circular Linked Lists A Linear Linked List

Circular Linked Lists

Circular Linked Lists

Circular Linked Lists In a circular linked list there are two methods to know if a node is the first node or not. Either a external pointer, list, points the first node or A header node is placed as the first node of the circular list. The header node can be separated from the others by either heaving a sentinel value as the info part or having a dedicated flag variable to specify if the node is a header node or not.

PRIMITIVE FUNCTIONS IN CIRCULAR LISTS The structure definition of the circular linked lists and the linear linked list is the same: struct node{ int info; struct node *next; }; typedef struct node *NODEPTR;

PRIMITIVE FUNCTIONS IN CIRCULAR LISTS The delete after and insert after functions of the linear lists and the circular lists are almost the same. The delete after function: delafter( ) void delafter(NODEPTR p, int *px) { NODEPTR q; if((p == NULL) || (p == p->next)){ /*the empty list contains a single node and may be pointing itself*/ printf(“void deletion\n”); exit(1); } q = p->next; *px = q->info; /*the data of the deleted node*/ p->next = q->next; freenode(q);

PRIMITIVE FUNCTIONS IN CIRCULAR LISTS The insertafter function: insafter( ) void insafter(NODEPTR p, int x) { NODEPTR q; if(p == NULL){ printf(“void insertion\n”); exit(1); } q = getnode(); q->info = x; /*the data of the inserted node*/ q->next = p->next; p->next = q;

CIRCULAR LIST with header node The header node in a circular list can be specified by a sentinel value or a dedicated flag: Header Node with Sentinel: Assume that info part contains positive integers. Therefore the info part of a header node can be -1. The following circular list is an example for a sentinel used to represent the header node: struct node{ int info; struct node *next; }; typedef struct node *NODEPTR;

CIRCULAR LIST with header node

CIRCULAR LIST with header node Header Node with Flag: In this case a extra variable called flag can be used to represent the header node. For example flag in the header node can be 1, where the flag is 0 for the other nodes. struct node{ int flag; int info; struct node *next; }; typedef struct node *NODEPTR;

CIRCULAR LIST with header node

Example Consider a circular linked list with a header node, where each node contains the name, account number and the balance of a bank customer. The header node contains a sentinel account number to be -99. (a) Write an appropriate node structure definition for the circular linked list. (b) Write a function to display the full records of the customers with negative balance.

a) struct node{ char Name[15]; int AccNo; float Balance; struct node *next; }; typedef struct node *NODEPTR; b) Assume that the list pointer points the header with the sentinel account number -99. void DispNegBalanca(NODEPTR *plist) { NODEPTR p; p=*plist; if(p == NULL){ printf(“There is no list!\n”); exit(1); } p=p->next; while(p->AccNo!=-99){ if(p->Balance < 0.0) printf(“The Customer Name:%s\nThe Account No:%d\nThe Balance:%.2f\n”, p->Name, p->AccNo, p->Balance);

Example Write a function that returns the average of the numbers in a circular list. Assume that the following node structure is used, where the flag variable is 1 for the header node and 0 for all the other nodes. struct node{ int flag; float info; struct node *next; }; typedef struct node *NODEPTR;

printf(“Empty list\n”); exit(1); } float avList(NODEPTR *plist)/*assume that plist points the header node*/ { int count=0; float sum =0.0; NODEPTR p; p=*plist; if((p == NULL)){ printf(“Empty list\n”); exit(1); } do{ sum=sum + p->info; p =p->next; count++; }while(p->flag !=1); return sum/count;