# 从链表翻转看指针引用

void reversenode(node *&head)
{
if ( (head == 0) || (head->next == 0) ) return;// 边界检测
node* pNext = 0;
while (pCur != 0)
{
pNext = pCur->next;// 将下一个节点保存下来
pCur->next = pPrev;// 将当前节点的下一节点置为前节点
pPrev = pCur;// 将当前节点保存为前一节点
pCur = pNext;// 将当前节点置为下一节点
}
}

void reverse(node** head)
{
node *cur,*pre,*next;

pre=(node*)null;
next=cur->next;

while(next)
{
cur->next=pre;
pre=cur;
cur=next;
next=cur->next;
}

cur->next=pre;
}


#include <iostream>
using namespace std;

#ifndef null
#define null (void*)0
#endif

typedef struct node
{
struct node* next;
int data;
}node;

{
node *cur,*pre,*next;

pre=(node*)null;
next=cur->next;

while(next)
{
cur->next=pre;
pre=cur;
cur=next;
next=cur->next;
}

cur->next=pre;
}
{
if ( (head == 0) || (head->next == 0) ) return;// 边界检测
node* pNext = 0;
while (pCur != 0)
{
pNext = pCur->next;// 将下一个节点保存下来
pCur->next = pPrev;// 将当前节点的下一节点置为前节点
pPrev = pCur;// 将当前节点保存为前一节点
pCur = pNext;// 将当前节点置为下一节点
}
}
{
node *p1,*p2,*p3;
while(p2)
{
p3=p2->next;
p2->next=p1;
p1=p2;
p2=p3;
}
}
void insert(node* p)
{
}

void del(node* p)
{
node *cur,*next;
cur=p;
next=p->next;

while(next)
{
delete cur;
cur=next;
next=cur->next;
}
delete cur;
}

void print(node* p)
{
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}

int main()
{
for(int i=0;i<10;i++)
{
node* p=new node;
p->next=(node*)null;
p->data=i;

insert(p);
}
cout<<"reverse order:"<<endl;

system("pause");
return 0;
}

• 评论

1

• 下一篇
• 上一篇