按字母顺序排序链表 [英] sort linked list on alphabetically

查看:153
本文介绍了按字母顺序排序链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么,当我在另一个程序中排序时,它们会被排序,但是当我排序时,我在第98行出错。



  struct  book {
char name [ 100 ];
char 作者[ 50 ];
int 页面;
int 价格;
int yt;
};

struct 元素{
book x;
元素*下一个;
};

void add(book x,element ** head){
element * t = new 元素;
t-> x = x;
t-> next = * head;
* head = t;
}

void addLast(book x,element ** head,element ** tail){
element * t = 元素;
t-> x = x;
t-> next = NULL;
if (* head){
(* tail) - > next = t;
} 其他 {
* head = t;
}
* tail = t;
}



void print(element * head){
cout< < ------------------------ << ENDL;
元素* t =头;
while (t!= NULL){
cout<<叔> x.name<< |<< t-> x.author<< |
<< t-> x.pages<< |<< t-> x.price<< |<< t-> x.yt<< ENDL;
t = t-> next;
}
cout<< endl<< ------------------------ << ENDL;
}

void printN(element * head, int y ){
cout<< ------------------------ << ENDL;
元素* t =头;
while (t!= NULL){
if (t-> x.yt< Y)>
cout<<叔> x.name<< |<< t-> x.author<< |
<< t-> x.pages<< |<< t-> x.price<< |<< t-> x.yt<< ENDL;
t = t-> next;
}
cout<< endl<< ------------------------ << ENDL;
}

void printS(element * head, char * f){
cout<< ------------------------ << ENDL;
元素* t =头;
while (t!= NULL){
if (!strcmp(t - > x.name,f))
cout<<叔> x.name<< |<< t-> x.author<< |
<< t-> x.pages<< |<< t-> x.price<< |<< t-> x.yt<< ENDL;
t = t-> next;
}
cout<< endl<< ------------------------ << ENDL;
}



void print(book b){
cout<< ; b.name<< |<< b.author<< |
<< b.pages<< |<< b.price<< |<< b.yt<< ENDL;
}












void bS(元素**头){
bool 更改;
元素* t;
执行
change = false ;
for (t = * head; t-> next; t = t-> next)
if (strcmp(t-> x.name,t-> next-> x.name> 0))
{
strcpy( char temp,t-> x.name);
strcpy(t-> x.name,t-> next-> x.name);
strcpy(t-> next-> x.name,temp);
change = true ;
}

while (更改);
}
int main(){

element * head = NULL;
元素* tail = NULL;

book b;
strcpy(b.name, C);
strcpy(b.author, D);
b.pages = 100 ;
b.price = 1500 ;
b.yt = 1980 ;
addLast(b,& head,& tail);

strcpy(b.name, C);
strcpy(b.author, M);
b.pages = 300 ;
b.price = 2500 ;
b.yt = 1982 ;
addLast(b,& head,& tail);

strcpy(b.name, K);
strcpy(b.author, H);
b.pages = 120 ;
b.price = 1000 ;
b.yt = 1975 ;
addLast(b,& head,& tail);

print(head);
int y = 1982 ;
char f [] = D ;

printN(head,y);
printS(head,f);
bS(& head);
cout<< \ n排序后:\ n;
print(head);

解决方案

使用调试器。

设置断点第98行 - 无论哪个 - 并运行您的代码。当执行到达断点时,调试器将停止运行,并等待你告诉它该做什么。

查看变量,并确保所有指针都有效,并导致有效地方。几乎可以肯定,那里有一个坏或未初始化的指针。



我们不能为你做这个 - 我们无法访问你的数据与你一样完全运行 - 这是一项宝贵的技能。这是值得学习的:它在现实世界和IT中都很有用。并且它很多,在处理这样简短的代码时比在处理实际项目时更容易学习!



所以试试看。它应该为您提供有关其失败原因的信息 - 并且您可以使用它来确定代码中导致其失败的错误的位置:它可能距离导致您的程序的行很远失败!

Why, when I sort of numbers in another program they are sorted , but when I sort of string , I have an error in line 98 .

struct book {
    char name[100];
    char author[50];
    int pages;
    int price;
	int yt;
};

struct element {
    book x;
    element *next;
};

void add(book x, element **head){
    element * t = new element;
    t->x = x;
    t->next = *head;
    *head = t;
}

void addLast(book x, element ** head, element **tail) {
    element * t = new element;
    t->x = x;
    t->next = NULL;
    if (*head){
        (*tail)->next = t;
    } else {
        *head = t;
    }
    *tail = t;
}



void print(element *head) {
    cout << "------------------------" << endl;
    element * t = head;
    while (t != NULL) {
        cout << t->x.name<< " | " << t->x.author << " | "
        << t->x.pages << " | " << t->x.price <<"|"<<t->x.yt<< endl;
        t = t->next;
    }
    cout << endl << "------------------------" << endl;
}

void printN(element *head,int y) {
    cout << "------------------------" << endl;
    element * t = head;
    while (t != NULL) {
		if(t->x.yt<y)>
        cout << t->x.name<< " | " << t->x.author << " | "
        << t->x.pages << " | " << t->x.price <<"|"<<t->x.yt<< endl;
        t = t->next;
    }
    cout << endl << "------------------------" << endl;
}

void printS(element *head,char *f) {
    cout << "------------------------" << endl;
    element * t = head;
    while (t != NULL) {
		if(!strcmp(t->x.name,f))
        cout << t->x.name<< " | " << t->x.author << " | "
        << t->x.pages << " | " << t->x.price <<"|"<<t->x.yt<< endl;
        t = t->next;
    }
    cout << endl << "------------------------" << endl;
}

 

void print(book b) {
    cout << b.name<< " | " << b.author << " | "
    << b.pages << " | " << b.price <<"|"<<b.yt<< endl;
}












void bS(element**head){
	bool change;
	element *t;
	do
		change=false;
		for(t=*head;t->next;t=t->next)
			if(strcmp(t->x.name,t->next->x.name>0))
			{
				strcpy(char temp,t->x.name);
				strcpy(t->x.name,t->next->x.name);
				strcpy(t->next->x.name,temp);
				change=true;
			}
		
while(change);
}
int main() {

    element *head = NULL;
    element *tail = NULL;

    book b;
    strcpy(b.name, "C");
    strcpy(b.author, "D");
    b.pages = 100;
    b.price = 1500;
	b.yt=1980;
    addLast(b, &head, &tail);

    strcpy(b.name, "C");
    strcpy(b.author, "M");
    b.pages = 300;
    b.price = 2500;
	b.yt=1982;
    addLast(b, &head, &tail);

    strcpy(b.name, "K");
    strcpy(b.author, "H");
    b.pages = 120;
    b.price = 1000;
	b.yt=1975;
    addLast(b, &head, &tail);

    print(head);
	int y=1982;
	char f[]="D";
	
	printN(head,y);
	printS(head,f);
	bS(&head);
	 cout << "\nAfter sort :\n";
   print(head);

解决方案

Use the debugger.
Put a breakpoint on "line 98" - whichever that is - and run your code. When execution reaches the breakpoint, the debugger will stop running, and will wait for you to tell it what to do.
Look at the variables, and make sure that all pointers are valid, and lead to valid places. Almost certainly, you have a "bad" or uninitialized pointer in there.

We can''t do this for you - we don''t have access to your data to run this exactly as you do - and it''s a valuable skill. It''s well worth learning: it''s useful in the real world as well as in IT. And it''s a lot, lot easier to learn when dealing with short, simple code like this than it is when you are dealing with real-world projects!

So give it a try. It should give you information on why it''s failing - and you can use that to work out where the error in your code that causes it to fail there may be: it could be a long way from the line that causes your program to fail!


这篇关于按字母顺序排序链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆