#include <cstdio>
#include <deque>
#include <algorithm>
#include <iterator>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;
typedef struct LNode
{
int data;
struct LNode *next;
}LNode;
LNode *CreateLink(int num) // 构造
{
int i = 1;
LNode *head, *tail, *p;
head = (LNode *)malloc(sizeof(LNode));
tail = head;
while (num--)
{
p = (LNode *)malloc(sizeof(LNode));
p->data = i;
tail->next = p;
tail = p;
i++;
}
tail->next = NULL;
return head;
}
LNode *FindLink(LNode *head, int X) //查找
{
assert(head != NULL);
LNode *chick = head->next;
while (chick != NULL)
{
if (chick->data == X)
return chick;
else
chick = chick->next;
}
return NULL;
}
void DeleteLink(LNode *head, int X) //删除
{
assert(head != NULL);
LNode *p;
LNode *q;
p = FindLink(head, X);
q = p->next;
p->data = q->data;
p->next = q->next;
free(q);
}
LNode *inverseLink(LNode *head)
{
assert(head != NULL);
LNode *p, *q, *r;
p = head;
q = p->next;
while (q != NULL)
{
r = q->next;
q->next = p;
p = q;
q = r;
}
head->next = NULL;
head = p;
return head;
}
int main()
{
LNode *head = CreateLink(5);
head=inverseLink(head); // 这里要对head重新进行写,函数里面对head并不保存。
system("pause");
return 0;
}
C/C++ | 27-19 写出程序把一个链表中的接点顺序倒排
最新推荐文章于 2022-10-23 10:50:39 发布