dbq谢谢 C++或C都可以,具体样例在图片里 数据结构与算法 C++ 够三十字了吧
4条回答 默认 最新
- 技术专家团-小桥流水 2022-09-15 11:42关注
遍历链表找出最大的节点并记录,最后移动到最后一个位置即可。
运行结果:代码如下:
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; typedef int datatype; struct node { datatype data; node* link; }; //创建链表 struct node* creatlist() { int n; struct node* head = 0, * p, * t=0; while (1) { cin >> n; p = new node; p->data = n; p->link = 0; if (head == 0) { head = p; t = head; } else { t->link = p; t = p; } //判断是否输入结束 char ch = cin.get(); if (ch == '\n') break; } return head; } //显示链表 void showlist(struct node* head) { struct node* p = head; if (p) { cout << p->data; p = p->link; while (p) { cout << " " << p->data; p = p->link; } cout << endl; } } //操作 struct node* move(struct node* head) { struct node* max, * pre=0,*p; if (head->link == 0) return head; //只有1个节点的情况 p = head; max = head; //找出最大节点 while (p) { if (p->data > max->data) { max = p; } p = p->link; } //如果最大节点是头节点 if (max == head) { pre = head->link; //找到尾节点 p = head; while (p->link) p = p->link; //将头节点移动到尾部 p->link = head; head->link = 0; //第二个节点成为新的头节点 head = pre; return head; } else { pre = head; p = pre->link; while (p) { if (p == max) break; pre = p; p = pre->link; } //将max节点从链表中删除 pre->link = max->link; max->link = 0; //移动到链表尾部 p = head; while (p->link) p = p->link; p->link = max; return head; } } int main() { struct node* head = creatlist(); //showlist(head); head = move(head); showlist(head); return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录
悬赏问题
- ¥15 matlab中使用gurobi时报错
- ¥15 WPF 大屏看板表格背景图片设置
- ¥15 这个主板怎么能扩出一两个sata口
- ¥15 不是,这到底错哪儿了😭
- ¥15 2020长安杯与连接网探
- ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
- ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
- ¥16 mybatis的代理对象无法通过@Autowired装填
- ¥15 可见光定位matlab仿真
- ¥15 arduino 四自由度机械臂