struct node{ int info; struct node *left, *right; }; typedef struct node nodeptr; A Linear Doubly Linked List

Similar documents
EEE Algorithms & Data Structures Final Exam Instructor: Dr. Hasan Demirel

Data Structure with C. List

Each element of a binary tree is called a node of the tree. The following figure shows a binary tree with 9 nodes where A is the root.

Duration: 110 minutes

CMPE231 DATA STRUCTURES FINAL EXAMINATION / FALL 2010

PROGRAMMAZIONE I A.A. 2017/2018

Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS LINKED LISTS

DC54 DATA STRUCTURES DEC 2014

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

Darshan Institute of Engineering & Technology for Diploma studies Unit 4

Chapter 17: Linked Lists

Ashish Gupta, Data JUET, Guna

Linked List. April 2, 2007 Programming and Data Structure 1

UNIT IV 4 LINKED LIST

Chapter 17: Linked Lists

Lectures 5-6: Introduction to C

CHAPTER 4 Structures

C Programming, Autumn 2013, Exercises for the Second Week

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues

Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

The time and space are the two measure for efficiency of an algorithm.

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

(2-1) Data Structures & The Basics of a Linked List I. Instructor - Andrew S. O Fallon CptS 122 (August 27, 2018) Washington State University

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

LINKED LIST IMPLEMENTATION USING C LANGUAGE: A REVIEW

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #10

Chapter 8: Intraprogram Communication. Lecture 8 1

Arrays & Linked Lists

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

S.E. Sem. III [CMPN] Data Structures. Primitive Linear Non Linear

CSCE 110 PROGRAMMING FUNDAMENTALS

Solution for Data Structure

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

CP2 Revision. theme: dynamic datatypes & data structures

Advanced C Programming and Introduction to Data Structures

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra

IV Unit Second Part STRUCTURES

Lesson 8: Linked List Deque

Introduction to Programming in C Department of Computer Science and Engineering

Data Structures in C. C Programming and Software Tools. N.C. State Department of Computer Science

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

DC104 DATA STRUCTURE JUNE Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

Programming in C. Lecture Tiina Niklander. Faculty of Science

Semantic Analysis with Attribute Grammars Part 5

Intro to C: Pointers and Arrays

S.E. Sem. III [INFT] Data Structures & Analysis. Primitive Linear Non Linear

Lectures 5-6: Introduction to C

CS 11 C track: lecture 6

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

JTSK Programming in C II C-Lab II. Lecture 3 & 4

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

Fall, 2015 Prof. Jungkeun Park

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Self-referential Structures and Linked List. Programming and Data Structure 1

UNIT 4: Linked List Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Abstract Data Types. Definitions, Implementations, and Uses

UNIT-I Fundamental Notations

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

EECE.2160: ECE Application Programming

Elementary Data Structures: Part 1: Arrays, Lists. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

ECE 15B COMPUTER ORGANIZATION

5 Phases of Software Life Cycle

Data Structure. Chapter 4 List (Part II) Department of Communication Engineering National Central University Jhongli, Taiwan.

NCS 301 DATA STRUCTURE USING C

Outline. Computer programming. Usage of time and date functions. Date and Time: time.h. Date and Time: time.h. Time functions:

Computer programming. "An expert is a man who has made all the mistakes which can be made, in a narrow field." Niels Bohr

CS13002 Programming and Data Structures, Spring 2005

CMPT 225. Lecture 6 linked list

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

MARKS: Q1 /20 /15 /15 /15 / 5 /30 TOTAL: /100

EECE.2160: ECE Application Programming

Tin học cơ sở 4$ Structures!

CS300 Final Review Questions 1

CS240: Programming in C

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review

#ifndef DOUBLE_LIST /* this string SHOULD NOT previously exist */ #define DOUBLE_LIST

BBM 201 DATA STRUCTURES

Advanced Pointer Topics

Introduction to Data Structures. Systems Programming

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 20 November 2013

Comp Sci 1MD3 Mid-Term II 2004 Dr. Jacques Carette

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

(Section : Computer Science)

EL2310 Scientific Programming

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #13

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists

Linear Data Structure Linked List

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA

Transcription:

1 EEE 212 Algorithms & Data Structures Spring 05/06 Lecture Notes # 13 Outline Doubly Linked Lists Linear & Circular Doubly Linked Lists Primitive Functions in Doubly Linked Lists Application of the Doubly Linked Lists DOUBLY LINKED LISTS The circular lists have advantages over the linear lists. However, you can only traverse the circular list in one (i.e. forward) direction, which means that you cannot traverse the circular list in backward direction. This problem can be overcome by using doubly linked lists where there three fields in the node structure: info field left pointer right pointer The doubly linked lists can be linear or circular. Each node in doubly linked list can be declared by: struct node int info; struct node *left, *right; ; Flag =1 null Flag =1 null A Linear Doubly Linked List A Circular Doubly Linked List without Header Node Flag=1 header node A Circular Doubly Linked List with a Header Node

2 PRIMITIVE FUNCTIONS IN DOUBLY CIRCULAR LISTS Delete a node: The following function can be used to delete a node pointed by a node pointer can be deleted in a doubly circular linked list. void delete(nodeptr *p, int *px) nodeptr *l,*r; if(p == NULL) printf( void deletion\n ); *px = p->info; /*the data of the deleted node*/ l = p->left; l->right = r; r->left = l; freenode(p); Insert a node: The following function insertright inserts a node with information field x to the right of a node pointed by p in a doubly circular linked list. Insertleft function can be written similarly. void insertright(nodeptr *p, int x) nodeptr *q,*r; if(p == NULL) printf( void insertion\n ); q = getnode(); q->info = x; r->left = q; q->right = r; q->left = p; p->right = q Ex 1: A circular double-linked list with header node is maintained to store circles in 2-D. Each circle is defined by three floating point numbers, which are the coordinates of the center, (x,y) and radius r. a) Define a node structure for this list. (Hint: define a new type called CIRCLE with three float fields and use it a the info field of the node structure). typedef struct float x, float y, float r, CIRCLE;

3 struct node int flag; /* 1 for the header 0 otherwise*/ CIRCLE info; struct node *left, *right; ; b) Write a function to find the number of circles whose area is less than a given reference area value. Use the following prototype for such a function: int NofCircleArea(nodeptr *list, float refarea); Assume that the header node contains a flag which is 1 for the header node and 0 otherwise. int NofCircleArea(nodeptr *list, float refarea) nodeptr *p; int count =0; float a; p=list; if((p == NULL)) printf( Empty list\n ); exit(1); do a = 3.14 * (p->info.r) * (p->info.r);/*area calculated*/ if( a <= refarea) count ++; p =p->right; while(p->flag!=1); return count; Ex 2: Consider a doubly circular linked list, where each node contains the name of a company, the budget of the company as well as the number of employees (workers) in that company. a) Write an appropriate node structure definition for the records of the doubly circular linked list. typedef struct char name[20]; float budget; int employee; COMPANY; struct node COMPANY info; struct node *left; struct node *right; ;

4 b) Assume that the doubly circular linked list contains only a single node which is the header node with a sentinel -1 as the number of employees. Given an array where the records of different companies are stored as elements of the array. Write the definition of a function where the elements of the array are copied/inserted into the doubly linked list in ascending order according to the budget of each company. void CopyArray(nodeptr *dlist, COMPANY *aptr, int size) int i; nodeptr *p=dlist; SORT(aptr,size); /*this function will sort the input array*/ for(i=0;i<size;i++) insertright(p,*(aptr+i)); p = p->right; void SORT(COMPANY *aptr, int size) int i,j; COMPANY hold; for(i=0;i<= size-1;i++) for(j=0;j<= size-2;j++) if( (aptr+j)->budget > (aptr+j+1) ->budget ) hold =*(aptr+j); *(aptr+j) = *(aptr+j+1); *(aptr+j+1)= hold; void insertright(nodeptr *p, COMPANY x) nodeptr *q,*r; if(p == NULL) printf( void insertion\n ); q = getnode(); /* assumed to be allocating necessary node space */ q->info = x; r->left = q; q->right = r; q->left = p; p->right = q

5 c) Given a sorted doubly linked list according to the choice (b). Write a function to display the names of the companies with minimum and maximum budgets. void MinMaxBudget(nodeptr *dlist) nodeptr *rp, *lp; rp = dlist->right;/* sorted list, therefore rp points the minimum */ lp = dlist->left;/* sorted list, therefore lp points the maximum */ printf( Company info with Minimum Budget\n ); printf( Name:%s\nBudget:%.2f\nEmployee:%d\n\n,rp->info.name, rp->info.budget, rp->info.employee); printf( Company info with Maximum Budget\n ); printf( Name:%s\nBudget:%.2f\nEmployee:%d\n\n,lp->info.name, lp->info.budget, lp->info.employee);