bergdorf goodman大促:这个数据结构提怎么做?

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/29 02:51:14
试编写一算法,对单链表实现就地逆置,即L=( a1, a2,...,ai-1,ai,ai+1,...,an),逆置为L=( an,..., ai+1, ai ,ai-1,..., a2,a1)
好的话,有加分!!!!

定义一个节点结构:
struct node
{
int date;//可能使用其它类型或结构
node *next;
};

假设你的链表已经存在,为L
node *curnode = L;//当前节点,指向开始
node *temp=curnode->next;//临时节点
curnode->next=NULL;
L=curnode;
//先将指针指向队尾
while(temp != NULL)
{
curnode=temp;
temp =curnode->next;
curnode->next=L;
L=curnode;
}

楼上给出的答案不错