插入节点不值链表结束 [英] Insert node not values in end of linked lists

查看:90
本文介绍了插入节点不值链表结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有一些链表已经上市,我想每个人名单的第一个节点存储到另一个列表,这样我可以记得这个列表显示原始列表。我认为,我们必须使用两种不同的结构。我公司在保留原有列表和使用各个列表的第一个节点阵列显示它们已经成功,但是我想创建单独的列表的链接列表来实现相同的。它有预期的输出,但正如我说我想用链表来代替节点的数组。

Lets say I have some linked lists already available and I want to store first node of each individual list into another list so that I can recall this list to display the original lists. I think we have to use two different structures. I am already successful in retaining original lists and displaying them using Array of first nodes of individual lists but I want to create a linked list of individual list to implement same. It has the expected output but as I said I want to use linked list instead of array of nodes.

现在这就是我正在试图解决的问题,以取代链表的阵列第一节点的链表,我越来越崩溃每当我试图调试code。请帮我。

Now this is how I am trying to solve the problem to replace the array of linked lists to linked list of first nodes, I am getting crashes whenever I try to debug code. Please help me.

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

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

typedef struct node Node;
Node* insertValue(Node * list, int value);
void display(Node*);

struct list_of_nodes {
    Node *list;
    struct list_of_nodes *next;
};

 typedef struct list_of_nodes ListNode;

 ListNode* insertNode(ListNode* head,Node* node);

int main()
{
ListNode *head=NULL;
Node *globalList = NULL, *lists[100];
int nbrOfLists, listNo, nbrOfVal, valNo, val,i=0,k;

CHECKER:
printf("\n\n Enter the number of lists (1 to 100):");
scanf("%d", &nbrOfLists);

if(nbrOfLists <= 0 || nbrOfLists > 100) //handling exceptional cases
    {
        printf("\n \n Number of Lists should be between 1 to 100");  // since array of node pointers contains 100 elements
        goto CHECKER;
    }


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++)   // to enter values in each individual list
    {
        printf("Enter node value %d:", valNo+1);
        scanf("%d", &val);

        // Here we insert the value in both lists
        lists[listNo]= insertValue(lists[listNo], val);  // original list has to be retained so storing in array lists
        globalList = insertValue(globalList, val);   // inserting node in combined list. This prevents an extra loop and merges the list elements into one.
    }

    head=insertNode(head,lists[listNo]); // CRASHING HERE 

    printf("\n  The list %d is: ",listNo+1);
    display(lists[listNo]);  // display each list after input
}

printf("\n\n\n THE FINAL LIST IS: ");
display(globalList);  //display combined list

printf("\n\n THE LISTS WERE: ");

while(i<nbrOfLists){   //original lists displayed
    k=i+1;
    printf("\n\n The list %d is: ",k);
    display(lists[i]);
    i++;
}

printf("\n\n");
return 0;
}

ListNode* insertNode(ListNode* head, Node* node){
ListNode *newNode, *m;
newNode = malloc(sizeof(ListNode));
newNode->list=node;

if(newNode == NULL)
{
   newNode->next=NULL;  // inserting first node
   return newNode;
}

m = head;
while(m->next)  // checking for right position in ordered list for new node
{
  m = m->next;
}
newNode->next = m->next;  // inserting new node
m->next = newNode;
return head;

}
Node* insertValue(Node * list, int value)   // function to insert node in ordered manner into list
{
Node *newNode, *m;
newNode = malloc(sizeof(Node));
newNode->number=value;

if(list == NULL)
{
   newNode->next=NULL;  // inserting first node
   return newNode;
}

if(value < list->number)
{
   newNode->next = list;  // inserting in end
   return newNode;
}

m = list;
while(m->next)  // checking for right position in ordered list for new node
{
   if(value < m->next->number)
       break;
   m = m->next;
}
newNode->next = m->next;  // inserting new node
m->next = newNode;
return list;
}

void display(Node*nodex){ // display node values in list

printf("%d ->",nodex->number);
nodex=nodex->next;

   if(nodex)
        return display(nodex);
   else
        return 0;
  }

下面是code这表明预期的结果,但与节点阵列:

Here is the code which shows expected results but with Array of nodes:

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

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

typedef struct node Node;
Node* insertValue(Node *list, int value);
void display(Node*);

int main()
{

    Node *globalList = NULL, *lists[100];
    int nbrOfLists, listNo, nbrOfVal, valNo, val, i = 0, k;

CHECKER:
    printf("\n\n Enter the number of lists (1 to 100):");
    scanf("%d", &nbrOfLists);

    if(nbrOfLists <= 0 || nbrOfLists > 100) //handling exceptional cases
    {
        printf("\n \n Number of Lists should be between 1 to 100");  // since array of node pointers contains 100 elements
        goto CHECKER;
    }

    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++)   // to enter values in each individual list
        {
            printf("Enter node value %d:", valNo + 1);
            scanf("%d", &val);

            // Here we insert the value in both lists
            lists[listNo] = insertValue(lists[listNo], val);  // original list has to be retained so storing in array lists
            globalList = insertValue(globalList, val);   // inserting node in combined list. This prevents an extra loop and merges the list elements into one.
        }

        printf("\n  The list %d is: ", listNo + 1);
        display(lists[listNo]);  // display each list after input
    }

    printf("\n\n\n THE FINAL LIST IS: ");
    display(globalList);  //display combined list

    printf("\n\n THE LISTS WERE: ");

    while(i < nbrOfLists){   //original lists displayed
        k = i + 1;
        printf("\n\n The list %d is: ", k);
        display(lists[i]);
        i++;
    }

    printf("\n\n");
    return 0;
}

Node* insertValue(Node *list, int value)   // function to insert node in    ordered manner into list
{
    Node *newNode, *m;
    newNode = malloc(sizeof(Node));
    newNode->number = value;

    if(list == NULL)
    {
        newNode->next = NULL;  // inserting first node
        return newNode;
    }

    if(value < list->number)
    {
        newNode->next = list;  // inserting in end
        return newNode;
    }

    m = list;
    while(m->next)  // checking for right position in ordered list for new node
    {
        if(value < m->next->number)
            break;
        m = m->next;
    }

    newNode->next = m->next;  // inserting new node
    m->next = newNode;
    return list;
}

void display(Node *nodex){ // display node values in list

    printf("%d ->", nodex->number);
    nodex = nodex->next;

    if(nodex)
        return display(nodex);
    else
        return 0;
}

请让我知道,如果你不明白的问题。

Please let me know if you do not understood the problem.

推荐答案

在<相当的讨论之后href=\"http://chat.stackoverflow.com/rooms/75599/discussion-between-jonathan-leffler-and-abhishek-singh\">chat,最后我用这个code这是密切相关的最后一个版本中的问题:

After considerable discussion in chat, I ended up using this code which is closely related to the last version in the question:

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

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

typedef struct node Node;
Node *insertValue(Node *list, int value);
void display(Node *);

struct list_of_nodes
{
    Node *list;
    struct list_of_nodes *next;
};

typedef struct list_of_nodes ListNode;

ListNode *insertNode(ListNode *head, Node *node);

int main(void)
{
    ListNode *head = NULL;
    Node *globalList = NULL, *lists[100];
    int nbrOfLists, listNo, nbrOfVal, valNo, val, i = 0, k;

CHECKER:
    printf("\n\n Enter the number of lists (1 to 100):");
    scanf("%d", &nbrOfLists);

    if (nbrOfLists <= 0 || nbrOfLists > 100)
    {
        printf("\n \n Number of Lists should be between 1 to 100");
        goto CHECKER;
    }

    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);

            lists[listNo] = insertValue(lists[listNo], val);
            globalList = insertValue(globalList, val);
        }

        head = insertNode(head, lists[listNo]);

        printf("\n  The list %d is: ", listNo + 1);
        display(lists[listNo]);
    }

    printf("\n\n\n THE FINAL LIST IS: ");
    display(globalList);

    printf("\n\n THE LISTS WERE: ");

    while (i < nbrOfLists)
    {
        k = i + 1;
        printf("\n\n The list %d is: ", k);
        display(lists[i]);
        i++;
    }

    printf("\n\n");
    return 0;
}

ListNode *insertNode(ListNode *head, Node *node)
{
    ListNode *newNode, *m;
    newNode = malloc(sizeof(ListNode));
    newNode->list = node;
    newNode->next = NULL;

    if (newNode == NULL)
    {
        fprintf(stderr, "Out of memory in %s\n", __func__);
        exit(1);
    }

    if (head == NULL)
        return newNode;

    m = head;
    while (m->next)
    {
        m = m->next;
    }
    newNode->next = m->next;
    m->next = newNode;
    return head;
}

Node *insertValue(Node *list, int value)
{
    Node *newNode, *m;
    newNode = malloc(sizeof(Node));
    newNode->number = value;
    newNode->next = NULL;

    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;
}

void display(Node *nodex)
{
    printf("%d ->", nodex->number);
    nodex = nodex->next;
    if (nodex)
        display(nodex);
}

通过一个简单的数据文件( ll7.data

With a sample data file (ll7.data):

3
6 26 22 83 96 89 69
10 87 33 5 36 85 34 0 25 57 99
5 49 44 27 75 82

我编 ll7.c 使用上面:

$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror ll7.c -o ll7
$

和运行它在 的valgrind 其中指出,$ C $ç泄漏像筛子(因为没有在望免费),但在其他方面给了它一个健康清洁提单。

And ran it under valgrind which noted that the code leaks like a sieve (because there isn't a free in sight), but otherwise gave it a clean bill of health.

$ valgrind --suppressions=suppressions ./ll7 < ll7.data
==7696== Memcheck, a memory error detector
==7696== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==7696== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==7696== Command: ./ll7
==7696== 
--7696-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option
--7696-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 2 times)
--7696-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 4 times)


 Enter the number of lists (1 to 100):

 Enter the number of inputs to the list 1: 
 Enter node value 1:Enter node value 2:Enter node value 3:Enter node value 4:Enter node value 5:Enter node value 6:
  The list 1 is: 22 ->26 ->69 ->83 ->89 ->96 ->

 Enter the number of inputs to the list 2: 
 Enter node value 1:Enter node value 2:Enter node value 3:Enter node value 4:Enter node value 5:Enter node value 6:Enter node value 7:Enter node value 8:Enter node value 9:Enter node value 10:
  The list 2 is: 0 ->5 ->25 ->33 ->34 ->36 ->57 ->85 ->87 ->99 ->

 Enter the number of inputs to the list 3: 
 Enter node value 1:Enter node value 2:Enter node value 3:Enter node value 4:Enter node value 5:
  The list 3 is: 27 ->44 ->49 ->75 ->82 ->


 THE FINAL LIST IS: 0 ->5 ->22 ->25 ->26 ->27 ->33 ->34 ->36 ->44 ->49 ->57 ->69 ->75 ->82 ->83 ->85 ->87 ->89 ->96 ->99 ->

 THE LISTS WERE: 

 The list 1 is: 22 ->26 ->69 ->83 ->89 ->96 ->

 The list 2 is: 0 ->5 ->25 ->33 ->34 ->36 ->57 ->85 ->87 ->99 ->

 The list 3 is: 27 ->44 ->49 ->75 ->82 ->

==7696== 
==7696== HEAP SUMMARY:
==7696==     in use at exit: 43,752 bytes in 471 blocks
==7696==   total heap usage: 551 allocs, 80 frees, 49,880 bytes allocated
==7696== 
==7696== LEAK SUMMARY:
==7696==    definitely lost: 32 bytes in 2 blocks
==7696==    indirectly lost: 688 bytes in 43 blocks
==7696==      possibly lost: 0 bytes in 0 blocks
==7696==    still reachable: 29,998 bytes in 310 blocks
==7696==         suppressed: 13,034 bytes in 116 blocks
==7696== Rerun with --leak-check=full to see details of leaked memory
==7696== 
==7696== For counts of detected and suppressed errors, rerun with: -v
==7696== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
$

该提示输出不真想当输入来自文件,这就是为什么数字是没有见过了。当你在终端上键入,终端驱动程序呼应您键入的内容到屏幕上。当数据来自一个文件,你不会看到的字符,因为他们正在阅读。

The prompting output isn't really wanted when the input comes from a file, and that's why the numbers aren't seen, too. When you type at the terminal, the terminal driver echoes what you type to the screen. When the data comes from a file, you don't see the characters as they're read.

该燮pressions文件列表和SUP presses各种从Mac OS X 10.10.3优胜美地运行系统泄漏。这就是为什么有在使用这么多的内存,太多;运行时系统使用了大量的内存。

The suppressions file lists and suppresses a variety of leaks from the Mac OS X 10.10.3 Yosemite runtime system. That's why there's so much memory in use, too; the runtime system uses a lot of memory.

是我所有的code,有好多是会受到不同的做了很多。有很多可能/应该做的应该添加错误检查和重构(refactorization)(特别是提取功能'),但这些变化并没有到preserve取得了一定的假象,以张贴的问题code

Were it all my code, there'd be a lot that would be done differently. There is a lot of error checking that should be added and refactorization (especially 'extract function') that could/should be done, but those changes were not made to preserve some semblance to the code posted in the question.

这篇关于插入节点不值链表结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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