1 / 19

Linked Lists

Linked Lists.

tess
Télécharger la présentation

Linked Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked Lists linked list - Is an ordered 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 data part holds the useful information ; the data to be processed. The link is used to chain the data together; it contains a pointer that identifies the next node in the list. A pointer variable points to the first node in the list. The name of the list is the same as the name of this pointer variable. The link in the last node contains NULL, indicating the end of the list. The following is a logical view of a link list named pList with 3 nodes. node pList data link data link data link We define an empty linked list to be a single pointer having the value of NULL. node - is a structure in a link list that has at least two fields. One of the fields is a data field; the other is a pointer that contains the address of the next node in the sequence. pList

  2. Linked Lists A definition of a type appropriate for a node of a linked list holding student records could be; typedef struct stu_tag { char id[10]; char name[26]; int gradePoints; struct stu_tag *linkp; } STUDENT ; When defining a structure type in C , we have the option of including a structure tag such as stu_tag after the reserved word struct. Then the phrase struct stu_tag is an alternative name for type STUDENT. Here we use the type struct stu_tag * in the declaration of one component to indicate that the linkp component of our node points to another node of the same type. We use struct stu_tag * rather than STUDENT * because the compiler has not yet seen the name STUDENT. We can allocate and initialize the data components of a node as follows: STUDENT *stu1; stu1 = (STUDENT *) malloc(sizeof (STUDENT) ); strcpy(stu1->id, “234567741”; strcpy(stu1->name, “John Doe”; stu1 -> gradPoint = 65; Structure tag Structure tag use

  3. Linked Lists One purpose of using dynamically allocated nodes is to enable us to grow data structures of varying size. We accomplish this by connecting individual nodes. The connection is made by assigning the memory address of one node to the linkp component of another node. Assuming we have allocate memory for another node stud2 we could then link the two nodes together. stu1->linkp = stu2; stu1 stu2 234567741 John Doe 65 589247555 Fred Smith 94 Without a physical relationship between the nodes, we need some way to distinguish the beginning of the list, that is, to identify the first logical node in the list. This pointer is known as the head pointer because it points to the node at the head of the list. In the above list stu1 would be the head pointer. The link in the last node contains null, indicating the end of the list. stu2->linkp = NULL;

  4. Linked Lists Building a simple linked list. - The logic of building a linked list requires us to allocate the first node and then enter a simple loop that allocates additional nodes, filling the components with data and then linking the node to the previous node. STUDENT * get_list(void) { STUDENT * plist; STUDENT * prear; STUDENT * pnew; int numRec = 0, i; printf("How many records are you going to enter ? > "); scanf("%d",&numRec); plist = prear = (STUDENT *)malloc(sizeof (STUDENT) ); printf("enter the data for student No. 1"); scanf("%s%s%d", plist->id, plist->name, &plist->gradePoints); for(i = 2; i <= numRec; i++) { pnew = (STUDENT *)malloc(sizeof (STUDENT) ); printf("enter the data for student No. %d", i); scanf("%s%s%d",pnew->id, pnew->name,&pnew->gradePoints); prear->linkp = pnew; prear = pnew; } prear->linkp = NULL; return(plist); }

  5. Linked Lists Building a simple linked list (continued. - The following builds a link list with data read from a file. STUDENT * get_list_file( FILE *inp ) { STUDENT * plist; STUDENT * prear; STUDENT * pnew; plist = prear = (STUDENT *)malloc(sizeof (STUDENT) ); fscanf(inp,"%s%s%d", plist->id, plist->name, &plist->gradePoints); pnew = (STUDENT *)malloc(sizeof (STUDENT) ); while(fscanf(inp,"%s%s%d", pnew->id, pnew->name, &pnew->gradePoints)!= EOF) { prear->linkp = pnew; prear = pnew; pnew = (STUDENT *)malloc(sizeof (STUDENT) ); } prear->linkp = NULL; return(plist); }

  6. Linked Lists Traversing Linked Lists - Algorithms that traverse a list, start at the first node and examine each node in succession until the last node has been processed. Several different types of functions use list traversal logic. In addition to printing the list, we could count the number of nodes in the list, total a numeric field in the node, or calculate the average of a field. The following function would print the STUDENT linked list. void printList(STUDENT *pList) { STUDENT * pWalker; pWalker = pList; while(pWalker) { printf("Student No. %s\n", pWalker->id); printf("Student Name: %s\n", pWalker->name); printf("Grade: %d\n\n", pWalker->gradePoints); pWalker = pWalker->linkp; } return; }

  7. Linked Lists Traversing Linked Lists (continued)- The following function would search the STUDENT linked list for a student’s name. int searchList(STUDENT *pList, STUDENT **pPre, STUDENT **pFound, char *target) { *pPre = NULL; *pFound = pList; while(*pFound != NULL && strcmp((*pFound)->name, target)) { *pPre = *pFound; *pFound = (*pFound)->linkp; } return(*pFound != 0); }

  8. Linked Lists Primitive Linked List Functions - to work with linked lists, we need some basic operations that manipulate the nodes. For example we need function to add a node or delete a node. The following function would add a student node to the STUDENT linked list: STUDENT *addNode(STUDENT *pList, STUDENT *pPre, STUDENT pInsert) { STUDENT *pNew; pNew = (STUDENT *) malloc( sizeof( STUDENT)); *pNew = pInsert; if( pPre == NULL) { pNew->linkp = pList; pList = pNew; } else { pNew->linkp = pPre->linkp; pPre->linkp = pNew; } return pList; }

  9. Linked Lists Primitive Linked List Functions (continued) The following function would delete a student node from the STUDENT linked list: STUDENT *deleteNode(STUDENT *pList, STUDENT *pPre, STUDENT *pDelete) { if( pPre == NULL) { pList = pDelete->linkp; } else { pPre->linkp = pDelete->linkp; } free( pDelete); return pList; }

  10. Linked Lists Updating a Linked List - The following two high-level functions (insertNode and removeNote) were written with the three low-level functions ( searchList, addNode and deleteNode). The following function deletes a student from the linked list. STUDENT *removeNode(STUDENT *pList) { int searchList(STUDENT *pList, STUDENT **pPre, STUDENT **pFound, char *target); STUDENT *deleteNode(STUDENT *pList, STUDENT *pPre, STUDENT *pDelete); STUDENT *pFound,*pPre; char name[26]; printf("Enter the name of a student to delete >"); scanf("%s",name); if(searchList(pList,&pPre,&pFound, name)) { pList = deleteNode(pList,pPre,pFound); } else printf("student not found\n"); return pList; }

  11. Linked Lists Updating a Linked List (continued - The following function adds a student to the linked list. STUDENT *insertNode(STUDENT *pList) { STUDENT *addNode(STUDENT *pList, STUDENT *pPre, STUDENT pInsert ); STUDENT studData; printf("enter the data for student No. 1"); scanf("%s%s%d", studData.id, studData.name, &studData.gradePoints); pList = addNode(pList, NULL, studData); return pList; }

  12. Linked Lists Representing a Stack with a Linked List - a stack is a data structure in which only the top element can be accessed. To illustrate, the plates stored in the spring-loaded device in a buffet line perform like a stack. A Customer always takes the top plate; when a plate is removed, the plate beneath it moves to the top. The following diagram shows a stack of three characters. The letter C, the character at the top of the stack, is the only one we can access. We must remove C from the stack in order to access the symbol + . Removing the value from a stack is called popping the stack, and storing an item in a stack is called pushing it onto the stack. C + 2 Stack can be implemented with arrays or linked lists. The following code implements a stack with an array structure. The formal parameter *top points to the variable that stores the subscript of the element at the top of the stack. Each push operation increments the value pointed to by the top before storing the new item at the top of the stack . Each pop operation returns the item currently at the top of the stack and then decrements the value pointed to by top. The if condition in push checks that there is room on the stack before storing a new item. The if condition in pop checks that the stack is not empty before popping it. If the stack is empty, STACK_EMPTY (a previously defined constant macro) is returned instead.

  13. Linked Lists Representing a Stack with a Linked List (continued) - void push(char stack[], /* input/output - the stack */ char item, /* input - data being pushed onto the stack */ int *top, /* input/output - pointer to top of stack */ int max_size) /* input - maximum size of stack */ { if (*top < max_size-1) { ++(*top); stack[*top] = item; } }

  14. Linked Lists Representing a Stack with a Linked List (continued) - char pop(char stack[], /* input/output - the stack */ int *top) /* input/output - pointer to top of stack */ { char item; /* value popped off the stack */ if (*top >= 0) { item = stack[*top]; --(*top); } else { item = STACK_EMPTY; } return (item); }

  15. Linked Lists Representing a Stack with a Linked List (continued) - We have seen that in a stack, elements are inserted (pushed) and removed (popped) at the same end of the list, the top of the stack. Since the element that is removed first is the one that has been waiting the shortest length of time, a stack is called a last-in, first-out list (LIFO). A stack can also be implemented as a linked list in which all insertions and deletions are performed at the list head. A logical view of a stack represented by a linked list is shown below: Stack of three characters C C + 2 s data link data link data link + 2 Stack after insertion (push ) of ‘/’ / C + 2 s data link data link data link data link / C + 2

  16. Linked Lists Representing a Stack with a Linked List (continued) - The typedefs below define a node for a stack type. /* header file 14-22.h */ typedef struct stack_node_s { char element; struct stack_node_s *restp; } stack_node_t;

  17. Linked Lists Representing a Stack with a Linked List (continued) - /* * Creates and manipulates a stack of characters */ #include <stdio.h> #include <stdlib.h> #include "14-22.h" /* Include typedefs from revised .h file */ void push(stack_node_t **sp, char c); char pop(stack_node_t **sp); int main(void) { stack_node_t *s = NULL; /* stack of characters - initially empty */ /* Builds first stack of Fig. 14.21 */ push(&s, '2'); push(&s, '+'); push(&s, 'C'); /* Completes second stack of Fig. 14.21 */ push(&s, '/'); /* Empties stack element by element */ printf("\nEmptying stack: \n"); do { printf("%c\n", pop(&s)); }while (s != NULL); return (0); }

  18. Linked Lists Representing a Stack with a Linked List (continued) - /* * The value in c is placed on top of the stack * accessed through topp Pre: the stack is defined */ void push(stack_node_t **topp, char c) { stack_node_t *newp; /* pointer to new stack node */ /* Creates and defines new node */ newp = (stack_node_t *)malloc(sizeof (stack_node_t)); newp->element = c; newp->restp = *topp; /* Sets stack pointer to point to new node */ *topp = newp; }

  19. Linked Lists Representing a Stack with a Linked List (continued) - /* * Removes and frees top node of stack, returning character value * stored there. * Pre: the stack is not empty */ char pop(stack_node_t **topp) { stack_node_t *to_freep; /* pointer to node removed */ char ans; /* value at top of stack */ to_freep = *topp; /* saves pointer to node being deleted*/ ans = to_freep->element; /* retrieves value to return */ *topp = to_freep->restp; /* deletes top node */ free(to_freep); /* deallocates space */ return (ans); } Output: Emptying stack: / C + 2

More Related