Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
DATA AND FILE STRUCTURE USING C MCA110 LTPC 3 02 4 M K Pachariya Email Id. [email protected] Department of Computer Application, Galgotias University, Greater Noida www.sites.google.com/a/galgotiasuniversity.edu.in/manojkumarpachariya Topics to covered • Linked Lists • Singly Linked Lists – Circular Linked Lists – Doubly Linked Lists – Implementation of Lists, Polynomial representation and addition, Generalized linked list, Header Linked lists. COMP171 Fall 2005 Lists Outline • • • • • Abstract Data Type (ADT) List ADT List ADT with Array Implementation Linked lists Basic operations of linked lists – Insert, find, delete, print, etc. • Variations of linked lists – Circular linked lists – Doubly linked lists Abstract Data Type (ADT) • Data type – a set of objects + a set of operations – Example: integer • set of whole numbers • operations: +, -, x, / • Can this be generalized? – (e.g. procedures generalize the notion of an operator) – Yes! Abstract data type – high-level abstractions (managing complexity through abstraction) – Encapsulation • Examples ADT… – the set ADT • A set of elements • Operations: union, intersection, size and complement – the queue ADT • A set of sequences of elements • Operations: create empty queue, insert, examine, delete, and destroy queue • Two ADT’s are different if they have the same underlying model but different operations – E.g. a different set ADT with only the union and find operations – The appropriateness of an implementation depends very much on the operations to be performed Pros and Cons Implementation of the ADT is separate from its use Modular: one module for one ADT – Easier to debug – Easier for several people to work simultaneously Code for the ADT can be reused in different applications Information hiding – A logical unit to do a specific job – implementation details can be changed without affecting user programs Allow rapid prototying – Prototype with simple ADT implementations, then tune them later when necessary Loss of efficiency The List ADT • A sequence of zero or more elements A1, A2, A3, … AN • N: length of the list • A1: first element • AN: last element • Ai: position i • If N=0, then empty list • Linearly ordered – Ai precedes Ai+1 – Ai follows Ai-1 Operations • printList: print the list • makeEmpty: create an empty list • find: locate the position of an object in a list – list: 34,12, 52, 16, 12 – find(52) 3 • insert: insert an object to a list – insert(x,3) 34, 12, 52, x, 16, 12 • remove: delete an element from the list – remove(52) 34, 12, x, 16, 12 • findKth: retrieve the element at a certain position Implementation of an ADT • Choose a data structure to represent the ADT – E.g. arrays, records, etc. • Each operation associated with the ADT is implemented by one or more subroutines • Two standard implementations for the list ADT – Array-based – Linked list Array Implementation • Elements are stored in contiguous array positions Array Implementation... • Requires an estimate of the maximum size of the list waste space • printList and find: linear • findKth: constant • insert and delete: slow – e.g. insert at position 0 (making a new element) • requires first pushing the entire array down one spot to make room – e.g. delete at position 0 • requires shifting all the elements in the list up one – On average, half of the lists needs to be moved for either operation Pointer Implementation (Linked List) • Ensure that the list is not stored contiguously – use a linked list – a series of structures that are not necessarily adjacent in memory Each node contains the element and a pointer to a structure containing its successor the last cell’s next link points to NULL Compared to the array implementation, the pointer implementation uses only as much space as is needed for the elements currently on the list but requires space for the pointers in each cell Linked Lists A B C Head • A linked list is a series of connected nodes • Each node contains at least – A piece of data (any type) – Pointer to the next node in the list • Head: pointer to the first node • The last node points to NULL node A data pointer A Simple Linked List Class • We use two classes: Node and List • Declare Node class for the nodes – data: double-type data in this example – next: a pointer to the next node in the list Typedef struct Node { double Node* }; data; next; // data // pointer to next 11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains two parts: data and link. The name of the list is the same as the name of this pointer variable. Figure 11.9 shows a linked list called scores that contains four elements. We define an empty linked list to be only a null pointer: Figure 11.9 also shows an example of an empty linked list. 11.16 11.17 Figure 11.9 Linked lists Before further discussion of linked lists, we need to explain the notation we use in the figures. We show the connection between two nodes using a line. One end of the line has an arrowhead, the other end has a solid circle. 11.18 Figure 11.10 The concept of copying and storing pointers Arrays versus linked lists Both an array and a linked list are representations of a list of items in memory. The only difference is the way in which the items are linked together. Figure 11.11 compares the two representations for a list of five integers. 11.19 Figure 11.11 Array versus linked list Linked list names versus nodes names As for arrays and records, we need to distinguish between the name of the linked list and the names of the nodes, the elements of a linked list. A linked list must have a name. The name of a linked list is the name of the head pointer that points to the first node of the list. Nodes, on the other hand, do not have an explicit names in a linked list, just implicit ones. Figure 11.12 The name of a linked list versus the names of nodes 11.20 Types of Linked list Linear Linked List (One Way List) Doubely Linked List (Two Way list) Circular Linked List Header Linked list Head is used to store the address of first element or node of linked list. Figure 11.12 The name of a linked list versus the names of nodes 11.21 Linear Linked List or One way Linked List Circular Linked List Doubly Linked List Header Linked List Header Node Head X Header Node Head Header list or Grounded list contains a special node called header node contains the information about linked list such as number of element, sorted or not and pointer to first node Operations on linked lists Creating the empty linked list Traversing the linked list (In order, reverse order) Searching the element in linked list (unsorted, sorted) Inserting the elements in linked list ( at Beginning, at end, after given element) Inserting the elements in sorted linked list Deleting the elements in linked list ( at Beginning, at end, after given element) Deleting the elements in sorted linked list Reversing the linked list Deleting the entire list 11.26 Figure 11.13 Moving of pre and cur pointers in searching a linked list 11.27 11.28 Figure 11.14 Values of pre and cur pointers in different cases Inserting a node Before insertion into a linked list, we first apply the searching algorithm. If the flag returned from the searching algorithm is false, we will allow insertion, otherwise we abort the insertion algorithm, because we do not allow data with duplicate values. Four cases can arise: ❑ Inserting into an empty list. ❑ Insertion at the beginning of the list. ❑ Insertion at the end of the list. ❑ Insertion in the middle of the list. 11.29 Figure 11.15 Inserting a node at the beginning of a linked list 11.30 Figure 11.16 Inserting a node at the end of the linked list 11.31 Figure 11.17 Inserting a node in the middle of the linked list 11.32 Deleting a node Before deleting a node in a linked list, we apply the search algorithm. If the flag returned from the search algorithm is true (the node is found), we can delete the node from the linked list. However, deletion is simpler than insertion: we have only two cases—deleting the first node and deleting any other node. In other words, the deletion of the last and the middle nodes can be done by the same process. 11.33 11.34 Figure 11.18 Deleting the first node of a linked list Figure 11.19 Deleting a node at the middle or end of a linked list 11.35 Retrieving a node Retrieving means randomly accessing a node for the purpose of inspecting or copying the data contained in the node. Before retrieving, the linked list needs to be searched. If the data item is found, it is retrieved, otherwise the process is aborted. Retrieving uses only the cur pointer, which points to the node found by the search algorithm. Algorithm 11.6 shows the pseudocode for retrieving the data in a node. The algorithm is much simpler than the insertion or deletion algorithm. 11.36 Traversing a linked list To traverse the list, we need a “walking” pointer, which is a pointer that moves from node to node as each element is processed. We start traversing by setting the walking pointer to the first node in the list. Then, using a loop, we continue until all of the data has been processed. Each iteration of the loop processes the current node, then advances the walking pointer to the next node. When the last node has been processed, the walking pointer becomes null and the loop terminates (Figure 11.20). 11.37 11.38 Figure 11.20 Traversing a linked list Implementation of Operations on linked lists Creating the empty linked list Traversing the linked list (In order, reverse order) Searching the element in linked list (unsorted, sorted) Inserting the elements in linked list ( at Beginning, at end, after given element) Inserting the elements in sorted linked list Deleting the elements in linked list ( at Beginning, at end, after given element) Deleting the elements in sorted linked list Reversing the linked list Deleting the entire list 11.39 Implementation of linked List Operation Representation of Linear Lined List • Typedef struct nodetype • { • Int info; • Struct nodetype *next; • } node; • node * head Creating Empty List • Void createEmptyList( node **head) • { • *head=NULL;} Implementation of linked List Reverse Order Traversal In-oder Traversing • Void traverseinoder1( node *head) • { • While (head !=NULL) • {printf(“%d”, head->info); • head=head->next;} • } • Void traverseinoder2( node *head) • {For (;head !=NULL; head=head->next) • printf(“%d”, head->info);} • • • • • • • • • • • • • • Void traverseinReversoder1( node *head) { node *ptr,*stack; Createstack(&stack); For (;head !=NULL; head=head->next) Push(&stack, head->info); While ( ! isempty(stack)) {printf(“%d”, pop(&stack) ); Destroystack(&stack); } Void traverseinReverseoder2( node *head) { If ( head->next !=NULL) traverseinReverseoder2(head->next) printf(“%d”, head->info);} Implementation of linked List Searching an element • • • • • • • • • • • • • • • • Node * searchinUlist( node *head, int item) { While ((head !=NULL) && (head>info!=item)) head=head->next; Return head; } Node * searchinSlist( node *head, int item) { While (head !=NULL) If (head->info==item) Return head; else if (item< head->info) Return null; Else head=head->next;} Return NULL; } Insert an element at begin • Void insertatbeg( node **head, int item) • { node *ptr; • Ptr=(*node) malloc(sizeof head (node)); • Ptr->info=item; • if (*head ==NULL) index’th • Ptr->next=NULL; element • Else • Ptr->next=*head; • * head=ptr; newNode • } Implementation of linked List Insert an element at end • • • • • • • • • • • • • • Void insertatEnd( node **head, int item) { node *ptr, *loc; Ptr=(*node) malloc(sizeof (node)); Ptr->info=item; Ptr->next=NULL; if (*head ==NULL) *head=ptr; Else { Loc=*head; While(loc->next != NULL) Loc=loc->next; Loc->next=ptr; } // end of else } // end of function Insert an element after given element • Void insertafter( node **head, int item, int after) {node *ptr, *loc; Loc=search(head, after); If ( loc==(node *) NULL) //element not found Return; Ptr=(*node) malloc(sizeof (node)); Ptr->info=item; Ptr->next=loc->next; Loc->next=ptr; } Deleting the element Delete at begin Void Deletefrombeg( node **head) • • • • • • • • { node *ptr; If (*head ==NULL) Return; head currNode Else{ Ptr=*head; *head=(*head)->next; Free(ptr);} } prevNode currNode Delete from end Void DeletefromEnd( node **head) • { node *ptr, *loc; • If (*head ==NULL) • Return; Else if ((*head)->==(node *) NULL) {Ptr=*head; *head=NULL; Free(ptr);} • Else{ • Loc=*head; • Ptr=(*head)->next; • While(ptr->next !=NULL) • {loc=ptr; • Ptr=ptr->next;} • Loc->next=NULL • Free(ptr);}} Deleting the element Delete at After given element Delete Entire list Void deleteafterelement(node *head, int after) • Void deleteList( node **head) • • • • • • • • { node *ptr, *loc; Loc=search(head,after); If (loc==NULL) Return; Ptr=loc->next; Loc->next=ptr->next; Free(ptr); } • • • • • • { node *ptr; While(*head !=NULL) {ptr=*head; *head=(*head)->next; Free(ptr); }} Reverse the list Reverse the list Reverse the List • Use three pointers • currentNode, previousNode, nextNode • Assign currentNode to PreviousNode • Assign nextNode to currentNode • Assign currentNode to Next of Nextnode • Assign previousNode to currentNode->next • Void ReverseList(node **head) • { node *previousnode, *currentnode, *nextnode; • Currentnode=*head; • Nextnode=currentnode->next; • Previousnode=NULL; • Currentnode->next=NULL; • While( nextnode !=NULL) • { Previousnode=currentnode; • Currentnode=nextnode; • Nextnode=currentNode->next; • Currentnode->next=previousnode; • } • *head=currentnode; • } Doubly Linked Lists CS 308 – Data Structures Variations of Linked Lists • Doubly linked lists – Each node points to not only successor but the predecessor – There are two NULL: at the first and last nodes in the list – Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists backwards A Head B C Node data • info: the user's data • next, back: the address of the next and previous node in the list .back .info .next Finding a List Item • We no longer need to use prevLocation (we can get the predecessor of a node using its back member) Finding a List Item (cont.) Inserting into a Doubly Linked List 1. newNode->back = location->back; 3. location->back->next=newN 2. newNode->next = location 4. location->back = newNode FindItem(listData, item, location, found) • RetrieveItem, InsertItem, and DeleteItem all require a search ! • Write a general non-member function FindItem that takes item as a parameter and returns location and found. • InsertItem and DeleteItem need location (ignore found) • RetrieveItem needs found (ignores location) How can we distinguish between the following two cases? Special case: inserting in the beginning Deleting from a Doubly Linked List • Be careful about the end cases!! Headers and Trailers • Special cases arise when we are dealing with the first or last nodes • How can we simplify the implementation? – Idea: make sure that we never insert or delete the ends of the list – How? Set up dummy nodes with values outside of the range of possible values Headers and Trailers (cont.) • Header Node: contains a value smaller than any possible list element • Trailer Node: contains a value larger than any possible list element Doubly Linked List Two way linked List • Each node is divided into three parts Representation • First part called Previous pointer field • Second part contains the information • Third part contains next pointer field • In addition, two pointer variable • Head contains the address of first node • Tail contains the address of Last node • First node contains NULL in previous pointer field • Last node contains NULL in next pointer field • Doubly link list can traverse in both direction Operations on Doubly linked lists Creating the empty linked list Traversing the linked list (In order, reverse order) Searching the element in linked list (unsorted, sorted) Inserting the elements in linked list ( at Beginning, at end, after given element) Inserting the elements in sorted linked list Deleting the elements in linked list ( at Beginning, at end, after given element) Deleting the elements in sorted linked list Reversing the linked list Deleting the entire list 11.61 Doubly Linked list Implementation • Typedef struct nodeType • { • Struct nodeType *prev; • Int info; • Struct nodeType *next; • }node; • Node *head,*tail; Creating empty DLL • Void createEmptyList( node **head, node **tail) • {*head=*tail=NULL;} Head Tail Prev Info Next Implementation of Doubly linked List Reverse Order Traversal In-order Traversing • Void traverseinoder1( node *head) • { • While (head !=NULL) • {printf(“%d”, head->info); • head=head->next;} • } • Void traverseinoder2( node *head) • {For (;head !=NULL; head=head->next) • printf(“%d”, head->info);} • • • • • • • • • • • • • Void traverseinReversoder1( node *tail) { For (;tail !=NULL; tail=tail->prev) Printf(“%d”,tail->info);} Void traverseinReversoder2( node *tail) {While (tail !=NULL) {printf(“%d”, tail->info ); tail=tail->prev;} } Void traverseinReverseoder3( node *head) { If ( head->next !=NULL) traverseinReverseoder2(head->next) printf(“%d”, head->info);} Implementation of D-linked List Searching an element Node * searchinUlist( node *head, int item) • { • While ((head !=NULL) && (head>info!=item)) • head=head->next; • Return head; • } Node * searchinSlist( node *head, int item) • { • While (head !=NULL) • {If (head->info==item) • Return head; • head=head->next;} • Return NULL; } Insert an element • • • • At Beginning of List At end of list After given element Before given element Head Tail Prev Info Next Insert element in DDL Insert at begin • If list is empty then new node will be first node then • Assign NULL value to Prev and next field of new node • Assign the address of new node to head and tail • Else • Assign NULL to prev of new node • Assign the value of head variable to next field of new node • Assign the address of new node to prev field of node pointed by head node (old first node) • Assign the address of new node to head variable Insert an element at begin • Void insertatbeg( node **head, node **tail, int item) • { node *ptr; • Ptr=(*node) malloc(sizeof (node)); • Ptr->info=item; • if (*head ==NULL) • {Ptr->next=ptr->prev=NULL; • *head=*tail=ptr;} Else {ptr->prev=NULL; Ptr->next=*head; (* head)->prev=ptr; *head=ptr;} }// end of function Tail Insert element in DDL Insert at End • If list is empty then new node will be first node then • Assign NULL value to Prev and next field of new node • Assign the address of new node to head and tail • Else // new node will be last element • Assign NULL to next of new node • Assign the value of tail variable to prev field of new node • Assign the address of new node to next field of node pointed by tail node (old last node) • Assign the address of new node to tail variable Insert an element at End • Void insertatend( node **head, node **tail, int item) • { node *ptr; • Ptr=(*node) malloc(sizeof (node)); • Ptr->info=item; • if (*head ==NULL) • {Ptr->next=ptr->prev=NULL; • *head=*tail=ptr;} Else {ptr->next=NULL; Head Ptr->prev=*tail; (* tail)->next=ptr; *tail=ptr;} }// end of function Tail Insert element in DDL Insert at After given element Insert an element after given element • • • • • • • • • • • • • If item not found , no insertion else Allocate memory and assign item to info field of new node If after element is last element in list then Assign NULL value to next field of new node Assign the address of new node to next of location (after ele) Assign the tail variable to prev field of new node Assign the address of new node to tail variable Else // after element is first or any before last Assign address of after element (loc) to prev field of new node Assign the address of next element of after to next field of new node Assign the address of new node to prev field of Next element to after element Assign the address of new node to next field of node pointed by loc (after node) Void insertatafterelement( node *head, node **tail, int item, int after) • { node *ptr, *loc; • Ptr=head; • Loc=search(ptr,after); • If (loc==NULL) // item not found • Return; • Ptr=(*node) malloc(sizeof (node)); • Ptr->info=item; • if (loc->next ==NULL) // last element • {Ptr->next=NULL; • Loc->next=ptr; • ptr->prev=*tail; • *tail=ptr;} Else {ptr->prev=loc ptr->next=loc->next; (Loc->next)->prev=ptr; loc->next=ptr;} }// end of function Insert element before given element in DDL • • • • • • • • • • • • • Insert at before given element Insert an element before given element If item not found , no insertion else Allocate memory and assign item to info field of new node If before element is first element in list then Assign NULL value to prev field of new node Assign the address of new node to prev of location (after ele) Assign the head variable to next field of new node Assign the address of new node to head variable Else // before element is last or any before last element Assign address of prev of before element (loc) to prev field of new node Assign the address of before element (loc) to next field of new node Assign the address of new node to next field of prev element of before or loc element Assign the address of new node to prev field of node pointed by loc (before node) • Void insertbeforeelement( node **head, node **tail, int item, int before) • { node *ptr, *loc; • Ptr=head; • Loc=search(ptr, before); • If (loc==NULL) // item not found • Return; • Ptr=(*node) malloc(sizeof (node)); • Ptr->info=item; • if (loc->prev ==NULL) // first element • {Ptr->prev=NULL; • Loc->prev=ptr; • ptr->prev=*head; • *head=ptr;} Else {ptr->prev=loc->prev; ptr->next=loc; (Loc->prev)->next=ptr; loc->prev=ptr;} }// end of function Delete an element in DD list Delete at begin • • • • • • Assign the address of head variable to temp variable ptr If (*head==*tail) only one element in list then Assign Null to *head and *tail If there are more than one element in list then Assign NULL to prev field of follower(after) node to deleted node Assign the address of follower node to head variable Deallocate memory occupied by deleted node Void Deletefrombeg( node **head, node **tail) • • • • • • • • • • { node *ptr; If (*head ==NULL) Return; Ptr=*head; If (*tail==*head) // one element *head=*tail=NULL; Else (Ptr->next)->prev=NULL; *head=ptr->next;} Free(ptr);} Delete Operation • • • • At Beginning of List At end of list After given element Before given element Head Tail Prev Info Next Deleting the element • • • • • • Delete from end Assign the value of tail to temp variable ptr If (*head==*tail) only one element in list then Assign Null to *head and *tail If there are more than one element in list then Assign NULL to next field of predecessor(before) node of deleted node Assign the address of predecessor node to tail variable Deallocate memory occupied by deleted node Delete from end Void DeletefromEnd( node **head, node **tail) • • • • • • • • • • { node *ptr, *loc; If (*head ==NULL) Return; Ptr=*tail; If (*tail==*head) // one element *head=*tail=NULL; Else (Ptr->prev)->next=NULL; *tail=ptr->prev;} Free(ptr);} Delete element in DDL Delete at After given element delete an element after given element • • • • • • • • • • • • • Assign the value of tail to temp variable ptr Search for location of after element and store to loc • Void deleteatafterelement( node *head, node **tail, int after) • { node *ptr, *loc; If item not found , no deletion • Ptr=head; else • Loc=search(ptr, after); If next field of successor of after element equals • If (loc==NULL) // item not found NULL i.e. After element is second last element • Return; then Else if ((loc->next)->next== NULL) //Second last Assign the next of after or loc( address of element successor) to ptr • {Ptr=loc->next; Assign NULL to next field of after or loc • Loc->next=NULL; Assign the address of after element to tail variable • *tail=loc; Else // after element is first or any before last • Free(ptr);} Assign address of sucessor element of after Else {ptr=loc->next (element to be deleted) to ptr node loc->next=ptr->next; Assign the address of next element of sucessor (ptr->next)->prev=loc; to next field of after node Free(ptr);} Assign the address of after element to prev }// end of function field of Next element to sucessor Deallocate temp node Delete element in DDL delete an element before given Delete at before given element • • Assign the value of tail to temp variable ptr Search for location of after element and store to loc • • If item not found , no deletion else If prev field of predecessor of before element equals NULL i.e. before element is second element then Assign the prev of before or loc( address of predecessor) to ptr Assign NULL to prev field of before or loc node Assign the address of before element to head variable Else // after element is first or any before last Assign address of predecessor element of before to ptr node (element to be deleted) Assign the address of prev element of predecessor to prev field of before node Assign the address of before element to next field of predecessor element to predecessor pf before element or predecessor of item to be deleted Deallocate temp node • • • • • • • • element • Void deletebeforeelement( node **head, node **tail, int before) • { node *ptr, *loc; • Ptr=head; • Loc=search(ptr, before); • If (loc==NULL) // item not found • Return; Else if ((loc->pre)->prev== NULL) //Second element • {Ptr=loc->prv; • Loc->prev=NULL; • *head=loc; • Free(ptr);} Else {ptr=loc->prev loc->prev=ptr->prev; (ptr->prev)->next=loc; Free(ptr); } }// end of function Delete the entire dd list Delete dd list • Void deletelist(node **head, node **tail) • {node *ptr; • While( *tail !=NULL) • {ptr=*tail; • *tail=(*tail)->prev • Free(ptr) • } • *head=NULL; • } Delete dd list • Void deletelist(node **head, node **tail) • {node *ptr; • While( *head !=NULL) • {ptr=*head; • *head=(*head)->next • Free(ptr) • } • *tail=NULL; • } Variations of Linked Lists • Circular linked lists – The last node points to the first node of the list Head A B C – How do we know when we have finished traversing the list? (Tip: check if the next pointer of the current node is equal to the head.) – For non empty circular linked list, there are no NULL pointers – Memory representation is same as linear linked list Circular List Representation 76 Doubly Circular Linked List Representation • Can you draw a circular doubly linked list with a head at the left end 77 Circular List Representation • Programs that use chains can be simplified or run faster by doing one or both of the following: 1. Represent the linear list as a singly linked circular list (or simply circular list) rather than as a chain 2. Add an additional node, called the head node, at the front 78 Circular Linked List • All operations performed on linear linked list can easily extended for circular linked list with following exceptions: • While inserting the new node at end of list, its next pointer field is made to point to first node • While testing the end of list, compare next pointer field with address of first node or head. A linked list as an array of records • What are the advantages of using linked lists? (1) Dynamic memory allocation (2) Efficient insertion-deletion (for sorted lists) • Can we implement a linked list without dynamic memory allocation ? A linked list as an array of records (cont.) Array versus Linked Lists • Linked lists are more complex to code and manage than arrays, but they have some distinct advantages. – Dynamic: a linked list can easily grow and shrink in size. • We don’t need to know how many nodes will be in the list. They are created in memory as needed. • In contrast, the size of a C++ array is fixed at compilation time. – Easy and fast insertions and deletions • To insert or delete an element in an array, we need to copy to temporary variables to make room for new elements or close the gap caused by deleted elements. • With a linked list, no need to move other nodes. Only need to reset some pointers. Applications of Linked List • To implement the other data structures such as stacks, queues, tree, graph • Maintain the directory of names • Perform the arithmetic operations on long integers • Manipulate Polynomials • To represent the sparse matrices Case Study: Implementing a large integer ADT • The range of integer values varies from one computer to another • For long integers, the range is [-2,147,483,648 to 2,147,483,647] • How can we manipulate larger integers? Case Study: Implementing a large integer ADT (cont.) - A special list ADT Example: The Polynomial ADT • An ADT for single-variable polynomials N f ( x) ai x i • Array implementation i 0 Polynomial Manipulation Coeff icient Poly Coef ficie nt Exp Ne o xt Exp Nex o t Coef ficie nt Exp Ne o xt Coef ficie nt Exp o Typedef Struct nodetype { float coeff; Addition of polynomials int exp; Terms of polynomials are scanned from left to right Struct nodetype *next; Terms with power(exponents) that occur only in one polynomial are simply copied into resulting poly } node; Coefficients of Terms with same power(exponents) Node *poly; that occur in Both polynomials are added and new term simply copied into resulting poly Addition of two polynomials Void addPoly(node *ptr1, node *ptr2, node **ptr3) {int pow; float coef; While((ptr1!=NULL) && (ptr2!=NULL)) { if (ptr1->exp < ptr2->exp) {coef=ptr1->coeff; Pow=ptr1->expo; Ptr1=ptr1->next;} // end if Else if (ptr1->exp > ptr2->exp) {coef=ptr2->coeff; Pow=ptr2->expo; Ptr2=ptr2->next;} //end elseif Else {coef=ptr1->coeff+ptr2->coeff; Pow=ptr1->expo; Ptr1=ptr1->next; Ptr2=ptr2->next} // end else If (coef !=0) addnode(ptr3, coef, pow); } // end of while loop if (ptr1==NULL) { for(; ptr2!=NULL; ptr2=ptr2->next) addnode(ptr3, ptr2->coeff, ptr2->expo);} else if (ptr2==NULL) { for(; ptr1 !=NULL; ptr1=ptr1->next) addnode(ptr3, ptr1->coeff, ptr1->expo); } // end of elseif } // end of function Void addnode (node **ptr, float c, int p) {node *tempptr; Trmpptr=(node *) malloc (sizeof (node)); Tempptr->coeff=c; tempptr->expo=p; If (*ptr==NULL) // empty list {tempptr->next=NULL; *ptr=tempptr;} Else{ Tempptr->next=*ptr; *ptr=tempptr;} } Multiplication of Polynomials • Second polynomial is scaned from left to right • For each term of the second polynomial, first polynomial is scanned from left to right and its each term is multiplied by the term of second polynomial • If product term already exist in resulting polynomial then its coefficients are added otherwise new node is inserted to represent the product term The Polynomial ADT… – Acceptable if most of the coefficients Aj are nonzero, undesirable if this is not the case P ( x) 10 x 5x 1 – E.g. multiply P ( x) 3x 2x 11x 5 1000 14 1 1990 1492 2 • most of the time is spent multiplying zeros and stepping through nonexistent parts of the input polynomials • Implementation using a singly linked list – Each term is contained in one cell, and the cells are sorted in decreasing order of exponents Multiplication of two polynomials Void MultiPoly(node *ptr1, node *ptr2, node Node * search (node *ptr, int degree) **ptr3) • { while(ptr !=NULL) {int pow; float coef; • {if (ptr->expo ==degree) Return ptr; Node *temp; *loc, *tt; • Ptr=ptr->next;}//end of loop While((ptr1!=NULL) • Return ptr; } // end search {temp=ptr2 Void insertnode( node **ptr, float c, int p) while ( temp !=NULL)) • { node *tempptr, *loc, *ploc; {coef=ptr1->coeff * temp->coeff; Trmpptr=(node *) malloc (sizeof (node)); Pow=ptr1->expo + temp->expo; Tempptr->coeff=c; Tt=*ptr3; tempptr->expo=p; Loc=search(tt, pow); If (*ptr==NULL) // empty list If (loc==NULL) // term is not found resulted {tempptr->next=NULL; Insertnode(&ptr3, coef, pow); *ptr=tempptr;} Else // term found in resulted poly Else{ploc=NULL; Loc->coeff +=coef; Loc=*ptr; Temp=temp->next; While((loc !=NULL) && (p< loc->expo)) } // while temp loop {ploc=loc; Ptr1=ptr1->next; Loc=loc->next;} //end of while loc } // end while ptr1 } // end of else } // end of function } // end of function insertnode Header Node • • • • • • • • Sparse Matrices It contains header node Row node Column node row containing at least one non zero element Header node contains four parts no of rows, no of column, no of nonzero elements and head pointer pointing to first Row node contains row index and address of next row and address of first non zero element in that row. Column node contains column index and nonzero value. Next field of Last node of each row contains NULL and column next contains null • • • • • • • • • • • • Typedef struct columnnodetype {int colindex; Float element; Struct columnnode *next; }columnnode; Typedef rownodetype {int rowindex; Struct columnnode *first; Struct rownode* next; }rownode; Typedef struct headertypenode {int nrow, ncol,num;} headernode; Linear Lists – Representation Variations of Linked List • Sorted Linked List • Unsorted Linked List • Array-based (Formula-based) – Uses a mathematical formula to determine where (i.e., the memory address) to store each element of a list • Linked list (Pointer-based) – The elements of a list may be stored in any arbitrary set of locations – Each element has an explicit pointer (or link) to the next element • Indirect addressing – The elements of a list may be stored in any arbitrary set of locations – Maintain a table such that the ith table entry tells us where the ith element is stored • Simulated pointer – Similar to linked representation but integers replace the C++ pointers What is an unsorted list? • A list in which data items are placed in no particular order. What is a sorted list? • A list in which data items are placed in a • particular order. Key: a member of the class whose value is used to determine the order of the items in the list. Operations • • • • MakeEmpty Boolean IsFull int LengthIs RetrieveItem (ItemType& item, Boolean& found) • InsertItem (ItemType item) • DeleteItem (ItemType item) • ResetList • void GetNextItem (ItemType& item) Implementing a Sorted List as a Linked Structure CS 308 – Data Structures Function RetrieveItem Function InsertItem Function InsertItem (cont.) • Can we compare one item ahead?? (like in the unsorted list case?) • In general, we must keep track of the previous pointer, as well as the current pointer. Function InsertItem (cont.) prevLoc = location location= location->next Inserting an element at the beginning of the list Case 1 newNode->next= location; listData=newNode; Inserting an element in the middle of the list Case 2 newNode->next=location; prevLoc->next = newNode; Inserting an element at the end of the list Case 3 prevLoc->next = newNode; newNode->next=location; Inserting into an empty list Case 4 listData=newNode; newNode->next=location; Function DeleteItem • The DeleteItem we wrote for unsorted lists works for sorted lists as well • Another possibility is to write a new DeleteItem based on the following cases Deleting the only element in the list Delete the first element in the list Delete an element in the middle of the list Delete the last element in the list Other SortedList functions • Same as in the case of UnsortedList class