Download Document

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Doubly Linked List
Example
Ceng-112 Data Structures I
2007
1
Q &A. 5
(25 Points) Create a doubly link list which have the nodes for 3, 9, 1, 7, and 2.
9 2 7 1 35
5
3
9
1
7
2
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
5
1
2
3
7
9
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.
Ceng-112 Data Structures I
2007
2
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();
Ceng-112 Data Structures I
2007
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);
}
3
Q & A . 5 (continued)
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);
}
Ceng-112 Data Structures I
2007
4
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);
}
Ceng-112 Data Structures I
2007
5
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);
}
Ceng-112 Data Structures I
2007
6
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);
}
Ceng-112 Data Structures I
2007
7
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);
}
Ceng-112 Data Structures I
2007
8
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);
}
Ceng-112 Data Structures I
2007
9
Related documents