链接的链接插入功能

时间:2015-02-23 15:48:59

标签: c linked-list

您好我正在尝试创建n个列表。每个列表都是带有新实例的有序链表。但是,我还需要一个组合的有序链表。以下是相同的代码。当谈到每个独立的清单时,我没有任何问题。但是,当我试图将每个节点插入到全局声明的链表节点中时,我收到错误(输入因一个节点值的迭代而崩溃。)请查看该函数并调用突出显示以清楚我在哪里我正好得到错误。请记下全球声明。提前致谢。

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct node{
    int number;
    struct node*next;
};

typedef struct node NODE;

NODE*createlist(int),*rel_strt,*new_entry[100];
NODE *start1=NULL,*rear1,*m1,*temp11;
NODE *insert_in_end(NODE*node1);

int i,num,n2,j,v,u;

int main() {   
    v=0;u=0;
    printf("\n Enter the number of lists:");
    scanf("%d",&n2);
    for(j=0;j<n2;j++) {
        printf("\n\n Enter the number of inputs to the list %d: \n ",j+1);
       scanf("%d",&num);
       new_entry[v]=createlist(num);
       display(new_entry[v]);
        v++;
    }
    printf("\nThe final List is:");
    display(start1);
    return 0;
}

NODE*insert_in_end(NODE *node11) {  //problem in this function

    int inf1=node11->number;

    if(start1==NULL) {
        start1=rear1=node11;
    } else {   
        m1=start1;
        if(m1->number>inf1) {
            node11->next=m1;
            start1=node11;
        } else {
           while(m1->next) {
                if(m1->next->number < inf1)
                    m1=m1->next;
                else
                    break;
            }
            temp11=m1->next;
            m1->next=node11;
            node11->next=temp11;
        }
    }
    return start1;
}

void display(NODE*nodex) {
    while(nodex!=NULL) {
        printf("%d ->",nodex->number);
        nodex=nodex->next;
    }
}

NODE *createlist(int n1) {

    NODE *node1,*start,*rear,*m,*temp1;
    start=NULL;

    for(i=0;i<n1;i++) {
        node1=(NODE*) malloc(sizeof(NODE));
        int inf;
        printf("Enter node value %d:",i+1);
        scanf("%d",&inf);
        node1->number=inf;
        node1->next=NULL;
        insert_in_end(node1);  // error during second fuction call check function definition

        if(start==NULL) {
            start=rear=node1;
        } else {
            m=start;
            if(m->number>inf) {
                node1->next=m;
                start=node1;
            } else {
                while(m->next) {
                    if(m->next->number < inf)
                        m=m->next;
                    else
                        break;
                }
                temp1=m->next;
                m->next=node1;
                node1->next=temp1;
            }
        }
    }
    return start;
}

1 个答案:

答案 0 :(得分:0)

我建议你定义一个函数insertValue,它会让你更方便。它还将使您的代码更具可读性。 该功能如下,您应该看到它与您拥有的非常接近。

// Returns the new start of the list after inserting value in increasing order
Node* insertValue(Node * list, int value)
{
   Node *newNode, *m;
   newNode = malloc(sizeof(NODE));
   newNode->number = value;   // added this
   newNode->next = NULL;      // added this
   if(list == NULL)
       return newNode;
   if(value < list->number)
   {
       newNode->next = list;
       return newNode;
   }
   m = list;
   while(m->next)
   {
       if(value < m->next->number)
           break;
       m = m->next;
   }
   newNode->next = m->next;
   m->next = newNode;
   return list;
}

您的主要功能现在应该迭代读取不同短名单的数字,如下所示。如您所见,变量名称比您的更明确。它们被限制在最小值,并且没有全局变量。

int main(int argc, char* argv[])
{
    NODE *globalList = NULL, *lists[100];
    int nbrOfLists, listNo, nbrOfVal, valNo, val;

    printf("\n Enter the number of lists:");
    scanf("%d", &nbrOfLists);
    if(nbrOfLists < 0 || nbrOfLists > 100)
        return -1;
    for(listNo = 0; listNo < nbrOfLists; listNo++) 
    {
        printf("\n\n Enter the number of inputs to the list %d: \n ",listNo+1);
        scanf("%d", &nbrOfVal);
        lists[listNo] = NULL;
        for(valNo = 0; valNo < nbrOfVal; valNo++)
        {
            printf("Enter node value %d:", valNo+1);
            scanf("%d", &val);

            // Here we insert the value in both lists
            lists[listNo] = insertValue(lists[listNo], val);
            globalList = insertValue(globalList, val);
        }
        display(lists[listNo]);
    }
    display(globalList);
    return 0;
}

您的显示功能正确。我在下面提供,但它和你的一样。您遇到的问题是因为我忘记了在malloc之后初始化节点的字段。

void display(NODE * list) 
{
    while(list) 
    {
        printf("%d ->", list->number);
        list = list->next;
    }
}