100 likes | 287 Views
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
E N D
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 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
#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); }
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)
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)
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)
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)
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)
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)