void oppsite(part *dest)
{
if(dest->next==0)
cout<<dest->data<<' ';
else
{
oppsite(dest->next);
cout<<dest->data<<' ';
}
}
dest是指向链表第一个接点的指针
比如part *dest是你的头结点,你只需要part *newPart=dest;
然后newPart=newPart->next;这时newPart指向的就是非头结点的第一个结点。可以做为参数传递了
发个咱自己的!
注释写的不好,不好意思啊
#include <stdio.h>
#include <malloc.h>
typedef struct node
{
int item;
struct node *prior, *next;
}*dLinkList, dListNode;
void CreateList(dLinkList *headPtr, dLinkList *tailPtr);//创建链表
void InsertElem(dLinkList *headPtr, dLinkList *tailPtr, int index, int newitem);//插入结点
int DeleteElem(dLinkList *headPtr, dLinkList *tailPtr, int index);//删除结点
void VisitList(dLinkList headPtr, dLinkList tailPtr);//遍力链表
void DestroyList(dLinkList *headPtr, dLinkList *tailPtr);//销毁链表
int main(void)
{
int delitem;
dLinkList newhead = NULL, newtail = NULL;
CreateList(&newhead, &newtail);
VisitList(newhead, newtail);
InsertElem(&newhead, &newtail, 4, 29);
VisitList(newhead, newtail);
delitem = DeleteElem(&newhead, &newtail, 2);
VisitList(newhead, newtail);
printf("delete item = %d\n", delitem);
DestroyList(&newhead, &newtail);
return 0;
}
void CreateList(dLinkList *headPtr, dLinkList *tailPtr)
{
int newitem;
dLinkList newPtr;
printf("Enter one number:\n");
scanf("%d", &newitem);
while (newitem != 0)
{
newPtr = (dLinkList)malloc(sizeof(dListNode));
if (newPtr == NULL)
{
exit(1);
}
newPtr -> item = newitem;
newPtr -> next = NULL;
newPtr -> prior = NULL;
if (*headPtr == NULL)
{
newPtr -> next = *headPtr;
newPtr -> prior = *headPtr;
*headPtr = newPtr;
}
else
{
(*tailPtr) -> next = newPtr;
newPtr -> prior = *tailPtr;
}
newPtr -> next = NULL;
*tailPtr = newPtr;
printf("Enter one number:\n");
scanf("%d", &newitem);
}
}
void VisitList(dLinkList headPtr, dLinkList tailPtr)//逆序
{
while (tailPtr != NULL)
{
printf("%d ", tailPtr -> item);
tailPtr = tailPtr -> prior;
}
printf("\n");
}
void DestroyList(dLinkList *headPtr, dLinkList *tailPtr)
{
dLinkList temp;
while (*headPtr != NULL)
{
temp = *headPtr;
*headPtr = (*headPtr) -> next;
free(temp);
}
*headPtr = NULL;
*tailPtr = NULL;
}
void InsertElem(dLinkList *headPtr, dLinkList *tailPtr, int index, int newitem)
{
int pos = 0;
dLinkList newPtr, cur = NULL;
cur = *headPtr;
newPtr = (dLinkList)malloc(sizeof(dListNode));
if (newPtr == NULL)
{
exit(1);
}
newPtr -> item = newitem;
while (cur && pos < index)
{
cur = cur -> next;
pos ++;
}
newPtr -> next = cur;
newPtr -> prior = cur -> prior;
cur -> prior -> next = newPtr;
cur -> prior = newPtr;
}
int DeleteElem(dLinkList *headPtr, dLinkList *tailPtr, int index)
{
int pos = 0, delitem;
dLinkList cur;
cur = *headPtr;
while (cur && pos < index)
{
cur = cur -> next;
pos ++;
}
delitem = cur -> item;
cur -> prior -> next = cur -> next;
cur -> next -> prior = cur -> prior;
free(cur);
cur = NULL;
return delitem;
}
[此贴子已经被作者于2006-6-1 21:23:46编辑过]