从中间C ++插入链接列表问题

时间:2015-05-18 09:21:14

标签: c++ linked-list

我是链接列表的新手,现在我遇到了如何将节点添加到列表中间的问题。例如,如果我在下面显示了一个名单,当我逐个添加数据时,就像下面的序列一样:

1.andrew
2.eric
3.madness
4.gerik

我想要我的数据" gerik "在" 疯狂"当它显示出来的地方。我能够对" eric "的数据进行排序。但是" eric "我不知道。我想要我的输出如下:

1.andrew
2.eric
3.gerik
4.madness

以下是我的示例代码,请通过给我建议或代码示例帮助我:

#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>

using namespace std;

struct node
{
    char f_name[20];
    char l_name[20];
    char u_id[10];
    node *next;
};

node *head;
node *curr;

//prototype
void display();
void add();
void search_name();
void insert_data(node *tempnode);
void insert_before_head(node *tempnode);
void menu(char choice);
char pause;

//function start...
void search_name()
{
    char name[20];
    curr = head;
    cin.ignore(30,'\n');
    cout<<"Key In Last Name :"<<endl;
    cin.get(name, 20);
    cin.ignore(30,'\n');

    while((curr->next != NULL) && (strcmp(curr->l_name, name) != 0))
    {
        curr = curr->next;
    }
    if(curr != NULL)
    {
        cout<<"Record Found !"<<endl;
        cout<<"First Name"<<setw(16)<<"Last Name"<<setw(16)<<"User ID"<<endl;
        cout<<"--------------------------------------------------------------"<<endl;
        cout<<curr->f_name<<setw(20)<<curr->l_name<<setw(16)<<curr->u_id<<endl<<endl;

    }
    else
    {
        cout<<"No Match !"<<endl;
        cout<<"Press 'Enter' To Continue"<<endl;
        cin.get(pause = getch());
        system("cls");
    }
};
void display()
{
    curr = head;
    if(head != NULL)
    {
        cout<<"First Name"<<setw(16)<<"Last Name"<<setw(16)<<"User ID"<<endl;
        cout<<"--------------------------------------------------------------"<<endl;
        while(curr != NULL)
        {
            cout<<curr->f_name<<setw(20)<<curr->l_name<<setw(16)<<curr->u_id<<endl;
            curr = curr->next;
        }
    }
    else
    {
        cout<<"No Data. File storage Empty!"<<endl;
    }
};
void add()
{
    node *temp;
    temp = new node;
    cin.ignore(30, '\n');
    cout<<"Key In First Name:"<<endl;
    cin.get(temp->f_name, 20);
    cin.ignore(30, '\n');
    cout<<"Key In Last Name:"<<endl;
    cin.get(temp->l_name, 20);
    cin.ignore(30, '\n');
    cout<<"Key In Your ID:"<<endl;
    cin.get(temp->u_id, 10);
    insert_data(temp);
};

void insert_data(node *tempnode)
{
    node *temp;

    if(head == NULL)
    {
        node *temp;
        temp = new node;
        temp = head;
        tempnode->next = NULL;
        head = tempnode;
    }
    else if(strcmp(tempnode->l_name, head->l_name) < 0)
    {
            insert_before_head(tempnode);
    }
    else
    {
        temp = new node;
        curr = head;

        while(curr->next != NULL)
        {
            curr = curr->next;
        }
            temp = tempnode;
            curr->next = tempnode;
            tempnode->next = NULL;
    }

};
void insert_before_head(node *tempnode)
{
    node *temp;

    if(head != NULL)
    {
       temp = new node;
       temp = tempnode;
       tempnode->next = head;
       head = tempnode;
    }
};

void menu(int choice)
{
    switch (choice)
    {
    case 1 :
        add();
        break;
    case 2:
        display();
        break;
    case 3:
        search_name();
        break;
    case 4:
        cout<<"Exit Program !"<<endl;
        break;
    default :
        cout<<"Error! Program Terminate !"<<endl;
    }
};

int main()
{
    int choice;
    node *temp;
    head = NULL;
    curr = NULL;

    cout << "Data Stack Head And Any Position !" << endl;
    system("cls");
    do{
            cout<<"1. Add Data."<<endl;
            cout<<"2. Show Data. "<<endl;
            cout<<"3. Search Last Name "<<endl;
            cout<<"4. Exit. "<<endl;
            cin >>choice;
            menu(choice);

       }while(choice != 4);

 return 0;
}

2 个答案:

答案 0 :(得分:1)

要对链接列表进行排序,您需要使用具有合并排序的分而治之策略。

为了在中间插入,您需要创建2个节点Node slow和Node fast。首先,Node slow是head.next,Node fast是head.next.next,你继续通过slow = slow.next和fast = fast.next.next来移动那些2,直到你用Node快速结束。如果你考虑一下,fast的移动速度会比Node慢两倍,所以最后Node慢速将在中间。然后在该节点之后插入。

答案 1 :(得分:0)

我们想在列表newNode中插入 让节点X成为节点,之后应插入newNode以保留排序顺序。

您可以在我的示例中重写insert_data(...)功能。
我拿了代码,由WhozCraug吸收并重写它更明显。

void insert_data(node *newNode)
{
    node **pointerToTheNextPtr = &head;
    // find position to insert new node
    while (*pointerToTheNextPtr && strcmp((*pointerToTheNextPtr)->l_name, newNode->l_name) < 0)
        pointerToTheNextPtr = &((*pointerToTheNextPtr)->next);

    // here pointerToTheNextPtr stores the pointer to the X->next field

    // insert new node between X and *(X->next)
    newNode->next = *pointerToTheNextPtr; // set newNode->next = X->next
    *pointerToTheNextPtr = newNode; // set X->next = newNode 
}