链接列表按最高标记排序名称和标记 [英] Linked list sort name and mark according the highest mark

查看:89
本文介绍了链接列表按最高标记排序名称和标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个程序,即插入名称和标记,而不是使用最高标记对其进行排序。当我运行它时,所有内容都会发现,例如当我创建数据和双显示时。但是,当我选择3-Sort时,它将永远循环直到无限。希望有人可以帮助我。非常感谢。谢谢,代码下面:



// ......... .................................................. ................

I have create 1 program which is to insert name and mark than sort it with highest mark.When i run it everything find such as when i 1-create data and 2-display.But,when i choose 3-Sort, it will loop forever until infinity.Hope someone can help me.Very appreciate that.Thanks,below the code:

//...........................................................................

#include <iostream>
#include <cstdlib>
#include <string.h>
#include <iomanip>
#include <windows.h>
using namespace std;
struct node
    {
        int mark;
        char name[10];
        struct node *next;
    } *person=NULL,*head=NULL,*temp=NULL,*curr=NULL,*head1=NULL,*current=NULL,*o=NULL,*next=NULL;

void SortList();
void Insert();

void Menu()
{
    cout<<"1.Insert data."<<endl;
    cout<<"2.Display data."<<endl;
    cout<<"3.Sort data based on highest mark."<<endl;
    cout<<"4.Exit."<<endl;
    cout<<"Enter Your choice: ";
}

void Insert()
{
    person =new node;
    char name[10];
    cout<<endl;
    cout<<"Enter Your name: ";
    cin>>person->name;
    cout<<"Enter Your mark: ";
    cin>>person->mark;
    cout<<endl;

    if (head == NULL)
    {
        head = person;
        person->next = NULL;
    }
    else
    {
        person->next = head;
        head = person;
    }
}

void Print()
{
    temp = head;
    int i = 1;

    cout<<"-----LIST CONTENTS-----"<<endl<<endl;
    cout<<"No"<<setw(10)<<"Name"<<setw(10)<<"Marks"<<endl;

    if (temp == NULL)
   cout<<"List is empty!!";
    else
    {
        while (temp != NULL)
        {
            cout<<i<<setw(10)<<temp->name<<setw(10)<<temp->mark<<endl;
            temp = temp->next;
            i++;
        }

    }
    cout<<endl;
}

void SortedInsert()
{
    temp = head;
    person = o;
    if (head == NULL)
    {
        head = person;
        person->next = NULL;
    }
    else
    {
        if (person->mark > temp->mark)
        {
             person->next = temp;
             head = person;

        }
        else
        {
            while ((temp->next != NULL) && (temp->next->mark > person->mark))
            {
                temp = temp-> next;
            }

                person->next=temp->next;
                temp->next = person;

            }
        }
    }


void SortList()
{

    current = head;
    if (current == NULL)
    cout<<"List empty"<<endl;
    else
    {
        while (current != NULL)
        {
            next = current->next;
            SortedInsert();
            current=next;
        }

    }


    cout<<"Sorted list: "<<endl;

}
int main (int argc, const char * argv[])
{

    int choice;
    cout<<"===This Program to insert name and mark==="<<endl;
    cout<<endl;
    do
    {
        Menu();
        cin>>choice;
        switch (choice)
        {
            case 1:
                Insert();
                break;
            case 2:
                Print();
                break;
            case 3:
                SortList();
                Print();
                break;
            default:
               cout<<"\nWRONG CHOICE";
        }
    } while (choice != 0);

    return 0;
}

推荐答案

我想知道这不会崩溃:你初始化全局变量 o NULL 并且永远不会改变它。在 SortedInsert()中,您将其分配给 person ,然后取消引用 person (当时是 NULL 指针)。



在附注中, function SortList()使用和修改全局变量当前 next ,你没有将变量传递给 SortedInsert ,而 SortedInsert 也不会访问这两个变量!这些功能如何可以互动?



帮自己一个忙,删除所有这些全局变量!使用局部变量代替函数参数。它将使控制流更清晰,你将更好地了解哪个变量访问了什么。
I wonder how this doesn''t crash: You initialize the global variable o to NULL and never change it. In SortedInsert() you assign that to person, and then you dereference person (which at that point is a NULL pointer).

On a side note, the function SortList() uses and modifies the global variables current and next, you pass no variables to SortedInsert, and SortedInsert accesses neither of these two variables! How can these functions possibly interact?

Do yourself a favor and remove all those global variables! Use local variables instead and function arguments. it will make the flow of control much clearer, and you''ll get a better view of which variable accesses what.


void SortedInsert()
{
    temp = head;
    person = o; // where is o assigned a valid value
                // seems like you are using an uninitialized variable
    if (head == NULL)
    {
        head = person;
        person->next = NULL;
    }
    else
    {
        if (person->mark > temp->mark)
        {
             person->next = temp;
             head = person;

        }
        else
        {
            while ((temp->next != NULL) && (temp->next->mark > person->mark))
            {
                temp = temp-> next;
            }

                person->next=temp->next;
                temp->next = person;

            }
        }
    }



但是当你打电话给<时,我很惊讶你的程序没有崩溃code> SortList()功能。

你应该得到一个段错误。


But seriously I am amazed that your program does not crash when you call the SortList() function.
You should be getting a seg fault.


在我看来你复制了别人的代码,但事后并没有把它与你自己的想法混在一起。要修复它,你应该首先设计一个明确的策略,SortList和SortedInsert应该做什么;这些行中的一些东西:



SortList:将从head指向的列表中删除单个元素,并将该元素输入head1指向的新列表(您已经宣布它,但到目前为止从未使用它)。处理完整个列表头后,列表head1现在包含已排序的列表。最后它将head1分配到头部。



SortedInsert:将查看SortList选择(和删除)的当前元素,并将其列在一个地方的head1中,以便list head1始终按顺序排列。



然后相应地修改SortList和SortedInsert。



提示:关注斯特凡的建议并摆脱所有这些全局变量。例如,SortList可以通过全局指针将参数插入的current元素传递给SertedInsert。



完成后如果你的代码仍然有问题,通过改善问题粘贴你的新代码告诉我们你做了什么。



祝你好运!
Looks to me that you copied someone else''s code, but didn''t mixed it up with your own thoughts afterwards. To fix it, you should first devise a clear strategy what SortList and SortedInsert should do; somethings along these lines:

SortList: Will remove a single element from the list pointed to by head and enter that element into a new list pointed to by head1 (you already declared it, but never used it so far). After the entire list head has been processed, list head1 now contains the sorted list. Finally it will assign head1 to head.

SortedInsert: Will look at the current element selected (and removed) by SortList and it to list head1 in a place, so that list head1 is always in sorted order.

Then modify SortList and SortedInsert accordingly.

Hint: Follow Stefan''s advice and get rid of all those global variables. For exanple, SortList could pass the "current" element that is to be inserted by a parameter to SertedInsert instead via a global pointer.

After you have done that and if you then still have problems with your code, show us what you have done by pasting your new code via "Improve Question".

Good Luck!


这篇关于链接列表按最高标记排序名称和标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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