我觉得链表没问题,是不是指针有什么问题……
#include<iostream>
#include<string.h>
using namespace std;
class song{
int id;
char* name;
public:
char* get(){return name;}
song(){};
song(int a,char* b){
id=a;
name=new char[strlen(b)+1];
strcpy_s(name,5,b);
}
};
struct list{
song ge;
list* next;
};
void insert(list*head,list*charu){
if(head==NULL)
{
head=charu;
charu->next=NULL;
return ;
}
if(strcmp(head->ge.get(),charu->ge.get())>=0)
{
charu->next=head;
head=charu;
return;
}
list*p,*q;
p=q=head;
while(p->next!=NULL&&strcmp(p->ge.get(),charu->ge.get())<0)
{
q=p;
p=p->next;
}
if(p->next==NULL)
{
if(strcmp(p->ge.get(),charu->ge.get())<0)
{
charu->next=NULL;
p->next=charu;
return;
}
else {
charu->next=p;
q->next=charu;
return;
}
}
else {
charu->next=p;
q->next=charu;
return;
}
}
int main(){
song aa(5,"aaa"),bb(10,"bbbb"),cc(2,"aabb");
list *head=NULL,*p=new list;
p->ge=aa;
insert(head,p);
p->ge=bb;
p->next=NULL;
insert(head,p);
p->ge=cc;
insert(head,p);
while(head!=NULL)
{
cout<<head->ge.get()<<" ";
head=head->next;
}
return 0;
}