Module 3 Linear Data Structures and Linked Storage Representation
Total Page:16
File Type:pdf, Size:1020Kb
Module 3 17CS33 Module 3 Linear data structures and linked storage representation Advantages of arrays Linear data structures such as stacks and queues can be represented and implemented using sequential allocation ie using arrays. Use of arrays have the following advantages Data accessing is faster because we just need to specify the array name and the index of the element to be accssed.. ie arrays are simple to use Disadvantages The size of the array is fixed. This limits the user in determining the required storage well in advance. However memory requirement cannot be determined while writing the program . ie if the allocation is more and required is less then memory is wasted. Similarly if less memory is allocated and requirement is more then we cannot allocate memory during execution. Also array items are stored sequentially. In such a case contiguous memory may not be available. Most of the times chunks of memory will be available but they cannot be used as they are not contiguous. Finally insertion and deletion of items in a array is difficult. Insertion of an element at the ith position requires us to move all the elements from i+1 th position To the next position and only after this movement insertion can be performed. A similar movement is required for deletion also. Linked lists A linked list is a collection of zero or more nodes where each node has some information data link Link will contain the address of the next node data will contain the information. Hence if the address of a node is known we can easily access the subsequent nodes Module 3 17CS33 Types of lists Singly linked lists doubly linked lists circular linked lists circular doubly linked lists Singly linked list A singly linked list is a collection of zero or more nodes. Each node will have one info field and one link field. A chain is a singly list with zero or more nodes. A empty list is as shown \0 A list is represented as follows 10 20 30 40 First Note: List contains 4 nodes where each node contains a info and link fields data filed contains the data Link field contains the address of the next node First is a variable containing the address of the first node of the list Using first we can access any node in the list The link field of the last node will have NULL Module 3 17CS33 Representation Nodes in the list are self referential structures. A structure is a collection of one or more fields which are of the same type or different type. A self refrential structure is a structure which has at least one field which is a pointer to the same structure . ex struct Node { int data; struct node * link; }; data is a int containing integer data Link is a pointer and should contain the address and normally it contains the address of the next node Declaration of variable We have the following declaration struct Node { Int info; Struct Node * link; }; struct Node * first; Creation of a empty list Before creating a list we need to create a empty list first. This is achieved by assigning NULL Module 3 17CS33 to the variable first ie first=NULL creating a new node A new node is created as follows first=(struct Node *) malloc( sizeof(struct Node)); The above statement will allocate memory to the variable first Using first we can access the contents of the node such as First data or first link(for the next node) To store the data. We use firstdata=10; We need to store NULL as this is the first node Firstlink=NULL; Deleting a node Any node which is not required is deleted using the free function free(first); When the above statement is executed the memory which was allocated to first using malloc is deallocated and returned to the Operating System. The variable first will have a invalid address and hence is called as dangling pointer. Operations on a singly linked list Inserting a node Deleting a node Search for a node Display the list Module 3 17CS33 #include<stdio.h> #include<stdlib.h> // Declaration of the node struct Node { int data; struct Node *link; }; struct Node first =NULL; //function to add a node at the end of the list void InsertL( int ele) { struct node *p,*q; p= (struct node *)malloc(sizeof(struct node)); p data=ele; for(q = first; q link != NULL; q=qlink) ; qlink = p; q = p; qlink=NULL; } Void InsertF( int ele ) { struct node *p; p =(struct Node *)malloc(sizeof(struct Node)); pdata=ele; plink =NULL; if (first = = NULL) first=p; else { plink=first; first = p; } } Module 3 17CS33 //function to add a node after a given node void addAfter(int ele, int loc) { int i; struct node *p,*q,*prev = NULL; q = first; for(i=1; i<loc;i++) { prev = q; q = q link; } p=(struct node *)malloc(sizeof(struct node)); pdata= ele; prevlink =p; prev = p; prev link= q; } Note: For all delete Function refer Class notes // function to display a list void display(struct Node *first) { struct Node *p; p = first; if(p = =NULL) { Printf(“List is Empty\n”); return; } while(p !=NULL) { printf("%d ",p data); p=plink; } } Module 3 17CS33 //function to count the number of nodes in a list int count(struct Node *first) { struct Node *p = first; int cnt=0; while(p!=NULL) { p=plink; cnt++; } return cnt; } Function to search for a element in a list struct Node * search(struct Node *first, int key) { struct Node *p while (p != NULL) { if (p data = = key) /* successful search */ return p; else p = p link; } return NULL; /* unsuccessful search */ } Circular linked list Circular Linked List is little more complicated linked data structure. In the circular linked list we can insert elements anywhere in the list whereas in the array we cannot insert element anywhere in the list because it is in the contiguous memory. In the circular linked list the previous element stores the address of the next element and the last element stores the address of the starting element. The elements points to each other in a circular way which forms a circular chain. The circular linked list has a dynamic size which means the memory can be allocated when it is required. Module 3 17CS33 Application of Circular Linked List The real life application where the circular linked list is used is our Personal Computers, where multiple applications are running. All the running applications are kept in a circular linked list and the OS gives a fixed time slot to all for running. The Operating System keeps on iterating over the linked list until all the applications are completed. Another example can be Multiplayer games. All the Players are kept in a Circular Linked List and the pointer keeps on moving forward as a player's chance ends. Circular Linked List can also be used to create Circular Queue. In a Queue we have to keep two pointers, FRONT and REAR in memory all the time, where as in Circular Linked List, only one pointer is required. Note: For Operations on circular Linked List refer class Notes Doubly linked list In computer science, a doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes. The beginning and ending nodes' llink and rlink links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list. If there is only one sentinel node, then the list is circularly linked via the sentinel node. It can be conceptualized as two singly linked lists formed from the same data items, but in opposite sequential orders. A doubly linked list whose nodes contain three fields: an integer value, the link to the next node, and the link to the previous node. The two node links allow traversal of the list in either direction. While adding or removing a node in a doubly linked list requires changing more links than the same operations on a singly linked list, the operations are simpler and potentially more efficient (for nodes other than first nodes) because there is no need to keep track of the previous node during traversal or no need to traverse the list to find the previous node, so that its link can be modified. Note: For Operations on doubly Linked List refer class Notes Applications of linked lists Polynomial addition A polynomial is sum of terms where each term is of the form axe Module 3 17CS33 Where a is the coefficient is the variable, e is the exponent The largest exponent is also called as the degree. Various operations can be performed on the polynomials Iszero(poly)-Returns 0 if the polynomial does not exist Coeff(poly,expo)-returns the coefficient Leadexpo(poly)-returns the largest exponent Attach- Attach a term<coeff,expo> onto the polynomial Remove (poly,expo)-delete a term Add(poly1,poly2)-adds two polynomials General procedure to add two polynomials Three cases exist Case 0 : Powers of the two terms to be added are equal In such a case the coefficients of the two terms are added using Sum=coeff(a,leadexpo(a)+coeff(b,leadexpo(b)) And then insert the resultant term on to the resulting polynomial using the attach function. We move onto the next term using the remove function. Ie A=remove(a,leadexpo(a)); B= remove (b,leadexpo(b)); Case 1: Power of the first term is > power the second term In such a case we insert the first term onto the resulting polynomial using Attach(c,coeff(a,leadexpo(a)) Module 3 17CS33 And move onto the next term using remove function A=remove(a,leadexpo(a) Default case: Here the power of the first term is < the power of the second term In such a case we insert the second term onto the resulting polynomial using Attach(c,coeff(b,leadexpo(b)) And