Polynomial Addition using Linked lists

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 3: Lists, Stacks and Queues Concept of Abstract Data Type (ADTS) How to efficiently perform operations on list Stack ADT and its applications Queue.
Stacks using Linked Lists. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what.
DATA STRUCTURES USING C++ Chapter 5
The Polynomial ADT By Karim Kaddoura For CS146-6.
C and Data Structures Baojian Hua
§1 Abstract Data Type (ADT)
Section P4 Polynomials. How We Describe Polynomials.
Linked list Applications: Polynomial handling. Representing a polynomial using a linked list Store the coefficient and exponent of each term in nodes.
Data Structures: A Pseudocode Approach with C
CS Data Structures Chapter 4 Lists. Dynamically Linked Stacks and Queues (1/8)  When several stacks and queues coexisted, there was no efficient.
Linked List C and Data Structures Baojian Hua
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
Data Structure (Part I) Chapter 2 – Arrays Data Abstraction and Encapsulation in C++ Section 1.3 –Data Encapsulation Also called information hiding.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Dale Roberts, Lecturer
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
CSCE 3110 Data Structures & Algorithm Analysis Arrays and Lists.
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.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Data Structure Sang Yong Han Chung-Ang University Spring
Data Structures & Algorithm Analysis Arrays. Array: a set of pairs (index and value) data structure For each index, there is a value associated with that.
Chapter 19 Data Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Data Structures A data structure.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Copyright Networking Laboratory Chapter 4. LISTS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer.
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.
Polynomial Expressions Unit 2, Lesson 2 A
Linked List (Part I). Introduction  Weakness of storing an ordered list in array: Insertion and deletion of arbitrary elements are expensive. ○ Example:
Polynomial Functions Addition, Subtraction, and Multiplication.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
Linked List Containers. Concatenate Example Concatenate(LinkedList listB): Add all of the elements of a second list to the end of the first list Concatenate(LinkedList.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
1 Joe Meehean. 2  empty is the queue empty  size  enqueue (add) add item to end of queue  dequeue (remove) remove and return item at front of queue.
Pointers A pointer is a variable which stores the address of another variable A pointer is a derived data type in C which is constructed from fundamental.
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
1 Linked Lists Chapter 3. 2 Objectives You will be able to: Describe an abstract data type for lists. Understand and use an implementation of a List ADT.
CSCE 3110 Data Structures & Algorithm Analysis More on lists. Circular lists. Doubly linked lists.
Linked List Containers. Useful Linked List Add-Ons Are there new variables/changes to the lists as they have been defined that could make our jobs as.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
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.
LINKED LISTS.
Programming Application in Civil Engineering
Data Structure Sang Yong Han
Linked List ADT used to store information in a list
Linked List.
UNIT – I Linked Lists.
Linked Lists Chapter 6 Section 6.4 – 6.6
Lists CS 3358.
Data Structure Dr. Mohamed Khafagy.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Linked list.
EEE2108: Programming for Engineers Chapter 4. Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
Linked List (Part I) Data structure.
Further Data Structures
CC 215 Data Structures Pointers (review and examples)
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
Lists.
Data Structures Chapter 4: Linked Lists.
Section 5.3 Polynomials and Polynomial Functions
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Presentation transcript:

Polynomial Addition using Linked lists Data Structures

Polynomial ADT A single variable polynomial can be generalized as: An example of a single variable polynomial: 4x6 + 10x4 - 5x + 3 Remark: the order of this polynomial is 6 (look for highest exponent)

Polynomial ADT (continued) By definition of a data types: A set of values and a set of allowable operations on those values. We can now operate on this polynomial the way we like…

Polynomial ADT What kinds of operations? Here are the most common operations on a polynomial: Add & Subtract Multiply Differentiate Integrate etc…

Polynomial ADT What kinds of operations? Here are the most common operations on a polynomial: Add & Subtract Multiply Differentiate Integrate etc…

Polynomial ADT (continued) Why implement this? Calculating polynomial operations by hand can be very cumbersome. Take differentiation as an example: d(23x9 + 18x7 + 41x6 + 163x4 + 5x + 3)/dx = (23*9)x(9-1) + (18*7)x(7-1) + (41*6)x(6-1) + …

Polynomial ADT (continued) How to implement this? There are different ways of implementing the polynomial ADT: Array (not recommended) Linked List (preferred and recommended)

Index represents exponents Polynomial ADT (continued) Array Implementation: p1(x) = 8x3 + 3x2 + 2x + 6 p2(x) = 23x4 + 18x - 3 p1(x) p2(x) 6 2 3 8 -3 18 23 2 2 4 Index represents exponents

Polynomial ADT (continued) This is why arrays aren’t good to represent polynomials: p3(x) = 16x21 - 3x5 + 2x + 6 6 2 -3 ………… 16 WASTE OF SPACE!

Polynomial ADT (continued) Advantages of using an Array: only good for non-sparse polynomials. ease of storage and retrieval. Disadvantages of using an Array: have to allocate array size ahead of time. huge array size required for sparse polynomials. Waste of space and runtime.

Polynomial ADT (continued) Linked list Implementation: p1(x) = 23x9 + 18x7 + 41x6 + 163x4 + 3 p2(x) = 4x6 + 10x4 + 12x + 8 P1 23 9 18 7 41 6 18 7 3 TAIL (contains pointer) P2 4 6 10 4 12 1 8 NODE (contains coefficient & exponent)

Polynomial ADT (continued) Advantages of using a Linked list: save space (don’t have to worry about sparse polynomials) and easy to maintain don’t need to allocate list size and can declare nodes (terms) only as needed Disadvantages of using a Linked list : can’t go backwards through the list can’t jump to the beginning of the list from the end.

Polynomial ADT (continued) Adding polynomials using a Linked list representation: (storing the result in p3) To do this, we have to break the process down to cases: Case 1: exponent of p1 > exponent of p2 Copy node of p1 to end of p3. [go to next node] Case 2: exponent of p1 < exponent of p2 Copy node of p2 to end of p3.

Polynomial ADT (continued) Case 3: exponent of p1 = exponent of p2 Create a new node in p3 with the same exponent and with the sum of the coefficients of p1 and p2.

Polynomials struct polynode { int coef; int exp; Representation struct polynode { int coef; int exp; struct polynode * next; }; typedef struct polynode *polyptr; coef exp next

Example a 3 14 2 8 1 0 null b 8 14 -3 10 10 6 null

Adding Polynomials 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 a->expon == b->expon d 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 -3 10 a->expon < b->expon

3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 -3 10 2 8 d a->expon > b->expon

C Program to implement polynomial Addition struct polynode { int coef; int exp; struct polynode *next; }; typedef struct polynode *polyptr;

polyptr createPoly() { polyptr p,tmp,start=NULL; int ch=1; while(ch) p=(polyptr)malloc(sizeof(struct polynode)); printf("Enter the coefficient :"); scanf("%d",&p->coef); printf("Enter the exponent : "); scanf("%d",&p->exp); p->next=NULL; //IF the polynomial is empty then add this node as the start node of the polynomial if(start==NULL) start=p; //else add this node as the last term in the polynomial lsit else tmp=start; while(tmp->next!=NULL) tmp=tmp->next; tmp->next=p; }

printf("MORE Nodes to be added (1/0): "); scanf("%d",&ch); } return start; start 3 14 2 8 1 0 null

void display(polyptr *poly) {polyptr list; list=*poly; while(list!=NULL) { if(list->next!=NULL) printf("%d X^ %d + " ,list->coef,list->exp); else printf("%d X^ %d " ,list->coef,list->exp); list=list->next; }

polyptr addTwoPolynomial(polyptr *F,polyptr *S) { polyptr A,B,p,result,C=NULL; A=*F;B=*S; result=C; while(A!=NULL && B!=NULL) switch(compare(A->exp,B->exp)) case 1: p=(polyptr)malloc(sizeof(struct polynode)); p->coef=A->coef; p->exp=A->exp; p->next=NULL; A=A->next; if (result==NULL) result=p; else attachTerm(p->coef,p->exp,&result); break;

case 0: p=(polyptr)malloc(sizeof(struct polynode)); p->coef=A->coef+B->coef; p->exp=A->exp; p->next=NULL; A=A->next; B=B->next; if (result==NULL) result=p; else attachTerm(p->coef,p->exp,&result); break;

case -1:p=(polyptr)malloc(sizeof(struct polynode)); p->coef=B->coef; p->exp=B->exp; p->next=NULL; B=B->next; if (result==NULL) result=p; else attachTerm(p->coef,p->exp,&result); break; }// End of Switch }// end of while

while(A!=NULL) { attachTerm(A->coef,A->exp,&result); A=A->next; } while(B!=NULL) attachTerm(B->coef,B->exp,&result); B=B->next; return result; }//end of addtwopolynomial function

int compare(int x, int y) { if(x==y) return 0; if(x<y)return -1; if(x>y) return 1; }

attachTerm(int c,int e,polyptr *p) { polyptr ptr,tmp; ptr=*p; tmp=(polyptr)malloc(sizeof(struct polynode)); while(ptr->next!=NULL) { ptr=ptr->next; } ptr->next=tmp; tmp->coef=c; tmp->exp=e; tmp->next=NULL;

main() { polyptr Apoly,Bpoly; clrscr(); printf("Enter the first polynomial : \n"); Apoly=createPoly(); display(&Apoly); printf("\n"); Bpoly=createPoly(); display(&Bpoly); printf("\nResult is : "); C=addTwoPolynomial(&Apoly,&Bpoly); display(&C); getch(); }

Exercise Write a program to implement polynomial multiplication.