繁体   English   中英

链表的递归升序和递减降序函数

[英]Recursive Ascending and Recursive Descending Order Functions for a Linked List

这是一项家庭作业,我已经编写了大部分代码,我不知道的唯一事情是我必须具有一个递归函数来对链表进行升序排序,并需要一个递归函数来对链表进行排序。链表以降序排列。 我很迷路。

这是我的整个代码。

using namespace std;

struct ListNode;
typedef ListNode* ListPtr;

struct ListNode
{
    int number;
    ListPtr next;

    ListNode(int value, ListPtr ptr = NULL)
    {
        number = value;
        next = ptr;
    }
};

char Menu();
void Add(ListPtr &, int);
void Delete(ListPtr &, int);
void Ascend(ListPtr &);
void Descend(ListPtr &);
void Print(ListPtr &);
void DeleteList(ListPtr &);

int main()
{
    ListPtr head = NULL;
    char answer;
    int input;

    answer = Menu();
    while(answer != 'Q')
    {
        if(answer == 'A')
        {
            cout << "Please enter in an integer: ";
            cin >> input;
            Add(head, input);
        }
        else if(answer == 'D')
        {
            cin >> input;
            Delete(head, input);
        }
        else if(answer == 'P')
        {
            Ascend(head);
        }
        else if(answer == 'O')
        {
            Descend(head);
        }
        else if(answer == 'N')
        {
            Print(head);
        }
        else
        {
            cout << "Incorrect input, please try again.\n";
        }

        answer = Menu();
    }

    DeleteList(head);
    return 0;
}

char Menu()
{
    char uinput;

    cout << "Please enter in one of the following:\n";
    cout << "A: Add an item to the end of the list.\n";
    cout << "D: Delete an item from the list.\n";
    cout << "P: Print the list in ascending order.\n";
    cout << "O: Print the list in descending order.\n";
    cout << "N: Display the number of items in the list.\n";
    cout << "Q: Quit.\n";

    return toupper(uinput);
}

void Add(ListPtr &start, int item)
{
    if(start->number > item || start == NULL)
        start = new ListNode(item, start);
    else
        Add(start->next, item);
}

void Delete(ListPtr &start, int item)
{
    if(start != NULL)
    {
        if(start->number == item)
            ListPtr cur = start;
        start = start->next;
        delete cur;
    }
    else
    {
        Delete(start->next, item);
    }
}

void Ascend(ListPtr &start)
{

} 

void Descend(ListPtr &start)
{

}

void Print(ListPtr &start)
{
    ListPtr cur = start;
    int count = 0;

    if(cur == NULL)
    {
        cout << "The list is empty.\n";
    }
    else
    {
        if(cur != NULL)
        {
            if(count % 10 == 0)
                cout << endl;
            cout << setw(5) << cur->number;
            cur = cur->next;
            count++;
        }
    }
    cout << endl;
}


void DeleteList(ListPtr &start)
{
    if(start != NULL)
    {
        DeleteList(start->next);
        cout << "Deleting item " << start->number << endl;
        delete start;
    }
}

对于递归部分,一种方法是将列表递归地分为左右两个列表,直到列表大小减小为1(或零)为止,在这种情况下,递归函数仅返回列表,否则在代码中合并返回的左列表和右列表并返回合并列表的递归函数。

您是否了解了如何将链接列表分为两个列表? 通常,这是使用两个指针完成的。 我不确定您教过什么以及应该弄清楚什么。 这是什么级别的编程课?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM