调不出来!
#include <stdio.h>
#include <malloc.h>
typedef struct node
{
int elem;
struct node *prior, *next;
}*dLinkList, dListNode;
void CreateList(dLinkList *headPtr, dLinkList *tailPtr);
void InsertElem(dLinkList *headPtr, int index, int newelem);
int DeleteElem(dLinkList *headPtr, int index);
int SearchElem(dLinkList headPtr, int key);
void VisitList(dLinkList headPtr);
void DestroyList(dLinkList *headPtr);
int main(void)
{
int index, elem;
dLinkList newhead = NULL, newtail = NULL;
CreateList(&newhead, &newtail);
VisitList(newhead);
InsertElem(&newhead, 4, 9);
VisitList(newhead);
elem = DeleteElem(&newhead, 6);
index = SearchElem(newhead, 4);
VisitList(newhead);
printf("elem = %d\n", elem);
printf("index = %d\n", index);
DestroyList(&newhead);
return 0;
}
void CreateList(dLinkList *headPtr, dLinkList *tailPtr)
{
dLinkList newPtr, newelem;
printf("Enter new elem:\n");
scanf("%d", &newelem);
while (newelem != 0)
{
newPtr = (dLinkList)malloc(sizeof(dListNode));
if (newPtr == NULL)
{
exit(1);
}
newPtr -> elem = newelem;
if (*headPtr == NULL)
{
*headPtr = newPtr;
*tailPtr = newPtr;
}
else
{
(*tailPtr) -> next = newPtr;
newPtr -> prior = *tailPtr;
*tailPtr = newPtr;
newPtr -> next = NULL;
}
printf("Enter new elem:\n");
scanf("%d", &newelem);
}
if (newPtr != NULL)
newPtr -> next = NULL;
}
void InsertElem(dLinkList *headPtr, int index, int newelem)
{
int pos = 0;
dLinkList newPtr, prev = NULL, cur = NULL;
newPtr = (dLinkList)malloc(sizeof(dListNode));
newPtr -> elem = newelem;
prev = *headPtr;
while (prev && pos < index - 1)
{
prev = prev -> next;
pos ++;
}
prev -> next = cur;
newPtr -> next = prev -> next;
newPtr -> prior = cur -> prior;
prev -> next = newPtr;
cur -> prior = newPtr;
}
int DeleteElem(dLinkList *headPtr, int index)
{
int pos, delelem;
dLinkList prev = NULL, cur = NULL;
prev = *headPtr;
while (prev && pos < index - 1)
{
prev = prev -> next;
pos ++;
}
prev -> next = cur;
prev -> next = cur -> next;
cur -> next -> prior = cur -> prior;
delelem = cur -> elem;
free(cur);
cur = NULL;
return delelem;
}
int SearchElem(dLinkList headPtr, int key)
{
int index = 0;
dLinkList prev;
prev = headPtr;
while (prev && prev -> elem != key)
{
prev = prev -> next;
index ++;
}
return index;
}
void VisitList(dLinkList headPtr)
{
while (headPtr)
{
printf("%d ", headPtr -> elem);
headPtr = headPtr -> next;
}
printf("\n");
}
void DestroyList(dLinkList *headPtr)
{
dLinkList temp;
while (*headPtr != NULL)
{
temp = *headPtr;
*headPtr = (*headPtr) -> next;
free(temp);
}
*headPtr = NULL;
}