1 / 9

Doubly Linked List Example

Doubly Linked List Example. (25 Points) Create a doubly link list which have the nodes for 3, 9, 1, 7, and 2. 9 2 7 1 35

aysha
Download Presentation

Doubly Linked List Example

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. Doubly Linked List Example

  2. (25 Points) Create a doubly link list which have the nodes for 3, 9, 1, 7, and 2. 9 2 7 1 35 After creation of the above doubly link list, try to sort its nodes in ascending order. Do not use an array structure for this operation. 9 7 3 2 15 Write the all needed pseudocodes for create, search, revise and delete operations in functional structure and show that how it is running with a trace operation. 5 5 3 1 9 2 1 3 7 7 2 9 Q & A . 5

  3. #include <stdio.h> #include <stdlib.h> struct eNode { int value; struct eNode *pNext; struct eNode *pPre; }; struct eHead { int count; struct eNode *pFirst; struct eNode *pLast; }; void main(void) { struct eHead *List1, *List2; List1=createList(); List2=createList(); Q & A . 5 addToList(List1, 3); addToList(List1, 9); addToList(List1, 1); addToList(List1, 7); addToList(List1, 2); displayList(List1); sortList(List1, List2); displayList(List2); destroyList(List1); destroyList(List2); }

  4. struct eHead *createList(void) { struct eHead *address; address = (struct eHead *) malloc (sizeof (struct eHead)); if (!address) { printf(“ Not enough memory. \n”); exit (-1); } address->count =0; address->pFirst=Null; address->pLast=Null; return(address); } Q & A . 5 (continued)

  5. int addToList(struct eHead *List, int number) { struct eNode *pNew, *pLast; if (!List) return(0); pNew=(struct eNode*) malloc (sizeof (struct eNode)); if (!pNew) {printf(“ Not enough memory. \n”); exit (-1); } pLast = List->pLast; if (pLast) { pLast->pNext = pNew; pNew->pPre = pLast; } else { List->pFirst = pNew; pNew->pPre=Null; } List->pLast=pNew; List->count=(List->count) + 1; pNew->value= number; pNew->pNext= Null; return(1); } Q & A . 5 (continued)

  6. int destroyList(struct eHead *List) { struct eNode *pGez, *pDel; if (!List) return(0); pGez = List->pFirst; while (pGez) { pDel = pGez; pGez = pGez->pNext; free(pDel); } free(List); printf(“List is destroyed. \n”); return(1); } Q & A . 5 (continued)

  7. int displayList(struct eHead *List) { struct eNode *pGez; if (!List) {printf(“No list.\n”); return(0);} pGez=List->pFirst; if (!pGez) {printf(“Empty list.\n”); return(0);} while (pGez) { printf(“%d,”, pGez->value); pGez= pGez->pNext; } return(1); } Q & A . 5 (continued)

  8. int deleteNode(struct eHead *List, struct eNode *del) { struct eNode *pNode, *nNode; if (!List || !List->count) return(0); nNode=del->pNext; pNode=del->pPre; if (pNode) pNode->pNext= nNode; else List->pFirst = nNode; if (nNode) nNode->pPre = pNode; else List-pLast= pNode; List->count = (List->count) -1; free(del); return(1); } Q & A . 5 (continued)

  9. void sortList(struct eHead *List1, struct eHead *List2) { struct eNode *pGez, *pMin; int min; if (!(List1 && List1->count >0 && List2 && List2->count = 0)) { printf(“The conditions are not met. \n”); return(-1); } while(List1->count) { pGez = List1->pFirst; min= pgez->value; while (pGez) { if (min > pGez->value) { min = pGez->value; pMin=pGez: } pGez= pGez->pNext; } deleteNode(List1, pMin); addToList(List2, min); } Q & A . 5 (continued)

More Related