对链接列表中的名称进行排序

时间:2017-03-19 06:50:49

标签: c++ sorting linked-list doubly-linked-list

(这是一个家庭作业问题)我无法实现链表的名称排序。我有权重(int)排序现在我只想按升序排序名称。

#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;

int c = 0;
struct node
{
    char name[20];
    int weight;

    node *next;
    node *prev;


};
node*head = NULL;
node *tail = NULL;
node *start_ptr = NULL;

这里我创建节点并在排序位置插入列表。我想为名字做同样的事情。名称被赋予权重(例如,人的权重)。它必须处于排序位置,因此当调用print函数时,它按排序顺序打印列表。

void create(int x,char name)
{

    node*p = NULL;
    node*r = NULL;
    node*np = NULL;
        np = new node;
    np->weight = x;
    np->next = NULL;
    np->prev = NULL;
    if (c == 0)
    {
        tail = np;
        head = np;
        p = head;
        p->next = NULL;
        p->prev = NULL;
        c++;
    }
    else
    {
        p = head;
        r = p;
        if (np->weight < p->weight)
        {
            np->next = p;
            p->prev = np;
            np->prev = NULL;
            head = np;
            p = head;
            do
            {
                p = p->next;
            } while (p->next != NULL);
            tail = p;
        }
        else if (np->weight > p->weight)
        {
            while (p != NULL && np->weight > p->weight)
            {
                r = p;
                p = p->next;
                if (p == NULL)
                {
                    r->next = np;
                    np->prev = r;
                    np->next = NULL;
                    tail = np;
                    break;
                }
                else if (np->weight < p->weight)
                {
                    r->next = np;
                    np->prev = r;
                    np->next = p;
                    p->prev = np;
                    if (p->next != NULL)
                    {
                        do
                        {
                            p = p->next;
                        } while (p->next != NULL);
                    }
                    tail = p;
                    break;
                }
            }
        }
    }
}

void traverse_head()
{
    node *t = head;
    while (t != NULL)
    {
        cout << t->weight << "\t";
        t = t->next;
    }
    cout << endl;
}


void print_node()
{
    node *temp;
    temp = start_ptr;
    if (temp == NULL) cout << "Empty List!" << endl;
    while (temp != NULL)
    {
        if (temp == NULL) cout << "Empty List!" << endl;



        cout << "Names & weights sorted(ascending) by name. :\n";


        cout << "Name   : " << temp->name << endl;
        cout << "Weight : " << temp->weight << endl;


        cout << "Names & weights sorted(ascending) by weight. : \n";

        cout << endl;
        temp = temp->next;
    }
}

int main()
{
    int i = 0, n, x;
    char names[20];
    cout << "Enter the number of people: \n";
    cin >> n;

读取重量和该人姓名的循环

while (i < n)
        {
            cout << "\nEnter Weights: \n";
            cin >> x;
            cout<<"Enter a Name"<<endl;
            cin>>names;
            create(x,*names);
            i++;
        }

        cout << "Output: \n";

        traverse_head();


        system("pause");
        return 0;
    }

0 个答案:

没有答案