一个用链表实现排序 但是不能选择是否插入 (新手求调试)
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
struct node{
int date;
struct node *next;};
struct node* initsqlist(struct node*);
void showsqlist(struct node*);
struct node*insertnode(struct node*,int);
int main()
{
cout<<"please input the data:\n";
int temp;
struct node *head=NULL,*p,*q;
while(cin>>temp)
{ q= (struct node *)malloc(sizeof(struct node));
if(head==NULL)
head=q;
else
p->next=q;
q->date=temp;
p=q;
}
q->next=NULL;
head=initsqlist(head);
showsqlist(head);
cout<<"do you want to insert num?(\"yes\"or\"no\")\n";
cout<<"put yes or no";
string a;
cin>>a;
cin.clear();
if(a=="yes"){int temp;
cout <<"plese input number:\n";
cin>>temp;
head=insertnode(head,temp);
}
showsqlist(head);
}
struct node* initsqlist(node*ihead)
{
struct node*p,*q;
p=ihead;
while(p)
{
q=p;
while(q)
{if(q->date<q->date)
swap(q->date,q->date);
q=q->next;
}
p=p->next;
}
return ihead;
}
struct node*insertnode(node*ihead,int m)
{
struct node*p,*q;
q=p=ihead;
node*temp=(struct node*) malloc(sizeof(struct node));
temp->date=m;
if(m>q->date&&q->next!=NULL)
{
p=q;
q=q->next;
}
if(ihead==q)
{
temp->next=ihead;
ihead=temp;
}
else if(q->next==NULL)
{
q->next=temp;
temp->next=NULL;
}
else {
p->next=temp;
temp->next=q;
}
return ihead;
}
void swap(int n,int m)
{
int temp;
temp=n;
n=m;
m=temp;
}
void showsqlist(node*ihead)
{
struct node*head;
head=ihead;
while(head)
{
cout<<head->date;
cout<<endl;
head=head->next;
}
}
[解决办法]
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
struct node{
int data;
struct node *next;
};
struct node* initsqlist(struct node*);
void showsqlist(struct node*);
struct node*insertnode(struct node*,int);
int main()
{
cout<<"please input the data:\n";
int temp;
struct node *head=NULL,*p,*q;
//p 下面既然有用到它的地方,这里注意给p分配个空间
p = (struct node*)malloc(sizeof(struct node));
cin>>temp;
while(temp != -1)//这里做了修改
{
q = (struct node*)malloc(sizeof(struct node));
if(head==NULL)
{
head = q;
}
//之前这里的else去了
p->next=q;
q->data=temp;
p=q;
cin>>temp;//循环输入data
}
q->next=NULL;
head=initsqlist(head);
showsqlist(head);
cout<<"do you want to insert num?(\"yes\"or\"no\")\n";
cout<<"put yes or no"<<endl;
cin.clear();//放在这里
char a[10];//用字符串数组来代替string类型,我习惯用指针和数组
cin>>a;
if(strcmp(a,"yes") == 0)//这样来比较字符串
{
int temp;
cout <<"plese input number:";
cin.clear();//这里想加也可以
cin>>temp;
head=insertnode(head,temp);
cout<<"\nNow,the new list data is :\n";
showsqlist(head);//将添加新节点后的链表数据再次输出
/*PS:感觉你的insertnode(head,temp)有点问题,默认情况下是插到
链表最后一个元素的末尾,也就是
{
endElement->data = temp;
endElement->next = NULL;
}看了你写的,晕晕。
*/
}
//showsqlist(head);这里这句不用要了
return 0;
}
struct node* initsqlist(node*ihead)
{
struct node*p,*q;
p=ihead;
while(p)
{
q=p;
while(q)
{
if(q->data<q->data)
{
swap(q->data,q->data);//数据交换
}
q=q->next;
}
p=p->next;
}
return ihead;
}
struct node*insertnode(node*ihead,int m)
{
struct node*p,*q;
q=p=ihead;
node*temp=(struct node*) malloc(sizeof(struct node));
temp->data=m;
if(m > q->data && q->next!=NULL)
{
p=q;
q=q->next;
}
if(ihead==q)
{
temp->next=ihead;
ihead=temp;
}
else if(q->next==NULL)
{
q->next=temp;
temp->next=NULL;
}
else
{
p->next=temp;
temp->next=q;
}
return ihead;
}
void swap(int n,int m)
{
int temp;
temp=n;
n=m;
m=temp;
}
void showsqlist(node*ihead)
{
struct node*head;
head=ihead;
while(head)
{
cout<<head->data;
cout<<endl;
head=head->next;
}
}