我想实现将一个链表中的最大值移动到表尾,打算通过以下的分步运行来实现:
1:找出最大值并返回(应该是成功了)
2:删除数据域为该最大值的(应该是成功了)
3:创建一个新结点,将最大值赋值给其数据域,再插入到表尾(失败)
#include
#include
using namespace std;
#include
typedef int Status;
typedef int ElemType;
typedef struct LNode {//定义链表
ElemType Data;
//Elemtype* next;
struct LNode *next;
}LNode,*LinkList;
int Inilist(LinkList &L){//初始化链表//int 型
L = new LNode;
L->next = NULL;
return OK;
}
void Create_List(LinkList &L,ElemType n){
L = new LNode;
L -> next = NULL;
LNode *R = L;
for(int i = 1; i <= n; i++){
LNode *S;
Inilist(S);
cin >> S->Data;
S->next = NULL;
R->next = S;
R = S;
}
}
ElemType Find_MAX(LinkList &L){//找对了
LNode *p = L->next;
ElemType MAX = p->Data;
while(p){//就是用一个指针来查找
if(p->Data >= MAX)
MAX = p -> Data;
p = p -> next;
}
//找到最大值
return MAX;
}
ElemType DeleteMax(LinkList &L,ElemType Max){
LNode *P = L->next;
LNode *Target = L;
if(Max = L->Data){
Target ->next = P->next;//如果第一个就是最大值
free(P);//释放目标数据
P = Target ->next;
}
while(P){
if(P->Data == Max){
Target ->next = P->next;//不是的话就往后挪一个,先赋值
free(P);//释放目标数据
P = Target ->next;
}
else{
Target = P;//还不是就再往后挪一个位置继续找
P = P->next;//为下一次循环做准备
}
}
}
int InsertMax(LinkList &L,ElemType Max){//用尾插法插入
/*
LNode *p = L;
while(p){
p = p->next;
}
LNode *NewNode;
NewNode = new LNode;
NewNode->Data = Max;
NewNode -> next = p -> next;
p->next = NewNode;
*/
LNode *p = L;
while(p){
p = p->next;
}
LNode *NewNode;
NewNode = new LNode;
NewNode->Data = Max;
NewNode->next = NULL;
p->next = NewNode;
p = NewNode;
}
void PrintList(LinkList &L)
{
LNode *p = L->next;
while(p != NULL){
cout << p->Data << " ";
p = p->next;
}
cout << endl;
}
int main() {
int n = 0;
LinkList LA;
Inilist(LA);
cin >> n;
Create_List(LA, n);
//cout << Find_MAX(LA) << endl;
DeleteMax(LA,Find_MAX(LA));
InsertMax(LA,Find_MAX(LA));
PrintList(LA);
return 0;
}
while(p){
p = p->next;
}
此时p已经是null了
应该是
while (p->next)
修改如下,供参考:
#include<stdio.h>
#include<iostream>
using namespace std;
#include<stdlib.h>
typedef int Status;
typedef int ElemType;
typedef struct LNode {//定义链表
ElemType Data;
//Elemtype* next;
struct LNode* next;
}LNode, * LinkList;
int Inilist(LinkList& L) {//初始化链表//int 型
L = new LNode;
L->next = NULL;
return 0; //OK;
}
void Create_List(LinkList& L, ElemType n) {
L = new LNode;
L->next = NULL;
LNode* R = L;
for (int i = 1; i <= n; i++) {
LNode* S;
Inilist(S);
cin >> S->Data;
S->next = NULL;
R->next = S;
R = S;
}
}
ElemType Find_MAX(LinkList& L) {//找对了
LNode* p = L->next;
ElemType MAX = p->Data;
while (p) {//就是用一个指针来查找
if (p->Data >= MAX)
MAX = p->Data;
p = p->next;
}
//找到最大值
return MAX;
}
ElemType DeleteMax(LinkList& L, ElemType Max) {
LNode* P = L->next;
LNode* Target = L;
//if (Max = L->Data) { // 修改 以下几行多余
// Target->next = P->next;//如果第一个就是最大值
// free(P);//释放目标数据
// P = Target->next;
//}
while (P) {
if (P->Data == Max) {
Target->next = P->next;//不是的话就往后挪一个,先赋值
free(P);//释放目标数据
P = Target->next;
}
else {
Target = P;//还不是就再往后挪一个位置继续找
P = P->next;//为下一次循环做准备
}
}
return 0;
}
int InsertMax(LinkList& L, ElemType Max) {//用尾插法插入
/*
LNode *p = L;
while(p){
p = p->next;
}
LNode *NewNode;
NewNode = new LNode;
NewNode->Data = Max;
NewNode -> next = p -> next;
p->next = NewNode;
*/
LNode* p = L;
while (p->next) { //while (p) //修改
p = p->next;
}
LNode* NewNode;
NewNode = new LNode;
NewNode->Data = Max;
NewNode->next = NULL;
p->next = NewNode;
p = NewNode;
return 0;
}
void PrintList(LinkList& L)
{
LNode* p = L->next;
while (p != NULL) {
cout << p->Data << " ";
p = p->next;
}
cout << endl;
}
int main() {
int n = 0, max; //修改
LinkList LA;
Inilist(LA);
cin >> n;
Create_List(LA, n);
max = Find_MAX(LA); //修改
//cout << Find_MAX(LA) << endl;
DeleteMax(LA, max); //DeleteMax(LA, Find_MAX(LA)); //修改
InsertMax(LA, max); //InsertMax(LA, Find_MAX(LA)); //修改
PrintList(LA);
return 0;
}
1.表格
2.折线图