CS235102 Data Structures Chapter 4 Lists. Dynamically Linked Stacks and Queues (1/8)  When several stacks and queues coexisted, there was no efficient.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

CSCE 3110 Data Structures & Algorithm Analysis
Chapter 4 Lists Pointers Singly Linked Lists
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.
CS Data Structures Chapter 4 Lists. Chain (1/3) Chain: Chain: A singly linked list in which the last node has a null link A singly linked list in.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
CSCI2100B Linked List Jeffrey
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.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
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.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CHAPTER 41 LISTS All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data Structures.
CS Data Structures Chapter 4 Lists.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Dale Roberts, Lecturer
Reference: Vinu V Das, Principles of Data Structures using C and C++
CHAPTER 41 LISTS All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data Structures.
Introduction to Data Structure
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1, 
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Data Structure in C Transparency No. 4-1 Copyright(c) 1997, Sungkyunkwan University Chapter #4: LISTS Fundamentals of Data Structure in C Horowitz, Sahni.
 Array ◦ sequential representation ◦ some operation can be very time-consuming (data movement) ◦ size of data must be predefined ◦ static storage allocation.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Copyright Networking Laboratory Chapter 4. LISTS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
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.
CSCE 3110 Data Structures & Algorithm Analysis More on lists. Circular lists. Doubly linked lists.
LINKED LISTS.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Software Learning Resource Service Platform CHAPTER 4 鏈結串列 (Linked List) 1.
 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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
CSCE 3110 Data Structures & Algorithm Analysis
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Chapter 12 – Data Structures
Data Structure #1: The Array
12 C Data Structures.
Chapter 15 Lists Objectives
Data Structure Dr. Mohamed Khafagy.
CSCE 3110 Data Structures & Algorithm Analysis
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Data Structures 7th Week
Stacks and Queues.
Stack and Queue APURBO DATTA.
EEE2108: Programming for Engineers Chapter 4. Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
Stacks Stack: restricted variant of list
CSCE 3110 Data Structures & Algorithm Analysis
CMSC 341 Lecture 5 Stacks, Queues
Lists.
CSCE 3110 Data Structures & Algorithm Analysis
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Linked List (Part I) Data structure.
Review & Lab assignments
Stacks and Queues.
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Stacks, Queues, and Deques
Presentation transcript:

CS Data Structures Chapter 4 Lists

Dynamically Linked Stacks and Queues (1/8)  When several stacks and queues coexisted, there was no efficient way to represent them sequentially.  Notice that direction of links for both stack and the queue facilitate easy insertion and deletion of nodes.  Easily add or delete a node form the top of the stack.  Easily add a node to the rear of the queue and add or delete a node at the front of a queue.

Dynamically Linked Stacks and Queues (2/8)   Represent n stacks top item link NULL... Stack

Dynamically Linked Stacks and Queues (3/8)   Push in the linked stack void add(stack_pointer *top, element item){ /* add an element to the top of the stack */ /* add an element to the top of the stack */ Push stack_pointer temp = (stack_pointer) malloc (sizeof (stack)); if (IS_FULL(temp)) { fprintf(stderr, “ The memory is full\n”); exit(1);} temp->item = item; temp->link = *top; *top= temp;} top link NULL... item link temp link

Dynamically Linked Stacks and Queues (4/8)   Pop from the linked stack element delete(stack_pointer *top) { /* delete an element from the stack */ Pop stack_pointer temp = *top; element item; if (IS_EMPTY(temp)) { fprintf(stderr, “The stack is empty\n”); exit(1);} item = temp->item; *top = temp->link; free(temp); return item; } item link NULL... link top temp

Dynamically Linked Stacks and Queues (5/8)   Represent n queues front item link NULL... Queue rear Add to Delete from

Dynamically Linked Stacks and Queues (6/8)  in the linked queue  enqueue in the linked queue front link NULL... item temp link rear NULL

Dynamically Linked Stacks and Queues (7/8)  from the linked queue (similar to push)  dequeue from the linked queue (similar to push) link NULL... link front item link temp rear

Dynamically Linked Stacks and Queues (8/8)  The solution presented above to the n-stack, m- queue problem is both computationally and conceptually simple.  We no longer need to shift stacks or queues to make space.  Computation can proceed as long as there is memory available.

Polynomials (1/9)  Representing Polynomials As Singly Linked Lists  The manipulation of symbolic polynomials, has a classic example of list processing.  In general, we want to represent the polynomial:  Where the a i are nonzero coefficients and the e i are nonnegative integer exponents such that e m-1 > e m-2 > … > e 1 > e 0 ≧ 0. e m-1 > e m-2 > … > e 1 > e 0 ≧ 0.  We will represent each term as a node containing coefficient and exponent fields, as well as a pointer to the next term.

Polynomials (2/9)  Assuming that the coefficients are integers, the type declarations are: typedef struct poly_node *poly_pointer; typedef struct poly_node { int coef; int coef; int expon; int expon; poly_pointer link; }; poly_pointer a,b,d;  Draw poly_nodes as: coefexponlink

Polynomials (3/9)   Adding Polynomials   To add two polynomials,we examine their terms starting at the nodes pointed to by a and b.   If the exponents of the two terms are equal 1. 1.add the two coefficients 2. 2.create a new term for the result.   If the exponent of the current term in a is less than b 1. 1.create a duplicate term of b 2. 2.attach this term to the result, called d 3. 3.advance the pointer to the next term in b.   We take a similar action on a if a->expon > b->expon.   Figure 4.12 generating the first three term of d = a+b (next page)

Polynomials (4/9)

Polynomials (5/9)  Add two polynomials

Polynomials (6/9)  Attach a node to the end of a list void attach(float coefficient, int exponent, poly_pointer *ptr){ /* create a new node with coef = coefficient and expon = exponent, attach it to the node pointed to by ptr. Ptr is updated to point to this new node */ poly_pointer temp; temp = (poly_pointer) malloc(sizeof(poly_node)); /* create new node */ if (IS_FULL(temp)) { fprintf(stderr, “The memory is full\n”); exit(1); } temp->coef = coefficient;/* copy item to the new node */ temp->expon = exponent; (*ptr)->link = temp;/* attach */ *ptr = temp; /* move ptr to the end of the list */ }

Polynomials (7/9)  Analysis of padd 1. 1.coefficient additions 0  additions  min(m, n) where m (n) denotes the number of terms in A (B) exponent comparisons extreme case: e m-1 > f m-1 > e m-2 > f m-2 > … > e 1 > f 1 > e 0 > f 0 m+n-1 comparisons 3. 3.creation of new nodes extreme case: maximum number of terms in d is m+n m + n new nodes summary: O(m+n)

Polynomials (8/9)  A Suite for Polynomials e(x) = a(x) * b(x) + d(x) poly_pointer a, b, d, e;... a = read_poly(); b = read_poly(); d = read_poly(); temp = pmult(a, b); e = padd(temp, d); print_poly(e); temp is used to hold a partial result.By returning the nodes of temp, wemay use it to hold other polynomials read_poly() print_poly() padd() psub() pmult()

Polynomials (9/9)  Erase Polynomials  erase frees the nodes in temp void erase (poly_pointer *ptr){ /* erase the polynomial pointed to by ptr */ poly_pointer temp; while ( *ptr){ temp = *ptr; *ptr = (*ptr) -> link; free(temp);}}