使用C在指定数目的节点后插入链表 [英] linked list insertion after specified number of nodes using C

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

问题描述

例如,如果用户输入以下字符串:
我爱C
然后输入:
非常
然后输入整数1,程序将输出:
我非常喜欢C
但是我的输出丢失了很多"部分,只剩下"I vlove c"
问题是insert()函数,我很努力地让它像这样工作.
任何人都有一些建议.


========原始代码============

For example, if the user enters the following string:
I love C
and then enters:
very much
and then enters the integer 1, the program would output:
I very much love C
But my output lost the "very much" part, left only with "I vlove c"
The problem is the insert() function, I tired my best to get it work like this.
Anyone got some suggestions.


========original code============

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

#define MAX 256

typedef struct list
{
    char data;
    struct list *next;
} List;


List *create(char *str);
List *insert(List *head, int node, char *str);

int getNode(int length);
void display(List *head);
void removeReturn(char *str);

int main(void)
{
    List *head = NULL;
    char str[MAX];
    int node, length;

    printf("Enter the first string: ");
    fgets(str, MAX, stdin);
    removeReturn(str);
    length = strlen(str);
    head = create(str);
  
    printf("Enter the second string: ");
    fgets(str, MAX, stdin);
    removeReturn(str);
    
    node = getNode(length);
    
    printf("The list before insertion:\n");
    display(head);
    
    head = insert(head, node, str);
    
    printf("The list after insertion:\n");
    display(head);
    
    system("pause");
    return 0;
}

int getNode(int length)
{
    int node = -1;
    
    while(node == -1 || node > length)
    {
        printf("Enter the value of insertion point (0 to %d): ", length);
        scanf("%d", &node);
    }
    
    return node;
}

List *create(char *str)
{
    int i;
    List *head = NULL;
    List *current;
    
    for(i = strlen(str); i >= 0 ; i--)
    {
        current = (List *)malloc(sizeof(List));
        current->data = str[i];
        current->next = head;
        head = current;
    }
    
    return head;
}

List *insert(List *head, int node, char *str)
{
    int i;
    List *head2, *toBeInserted;
    
    head2 = (List *)malloc(sizeof(List));
    head2 = head;
    for(i = 0; i < node; i++)
        head2 = head2->next;
        
    toBeInserted = (List *)malloc(sizeof(List));
    toBeInserted = create(str);

    toBeInserted->next = head2->next;
    head2->next = toBeInserted;

    return head;
}

void display(List *head)
{
    while(head)
    {
        printf("%c", head->data);
        head = head->next;
    }
    printf("\n");
}

void removeReturn(char *str)
{
    str[strlen(str)-1] = '\0';
}

推荐答案

在函数create()中,您malloc()一个列表项,其data字段是单个char变量,并且您只需将输入字符串的每个字符复制到该变量中,即可覆盖除最后一个字符以外的所有字符.将其更改为char*,使用malloc()为字符串分配足够的空间,并使用strcpy()将源字符串复制到其中.
我本来建议您切换到C ++和STL,但这可能是学校的任务,所以我现在就把它保留下来.
In function create() you malloc() a list item whose data field is a single char variable, and you are just copying each character of the input string into that variable, overwriting all except the last. Change it to a char*, use malloc() to allocate sufficient space for the string, and strcpy() to copy the source string into it.

I was going to suggest you switch to C++ and STL, but this may be a school assignment, so I''ll leave it for now.


insert函数中的以下代码
The following code in your insert function
head2 = (List *)malloc(sizeof(List));
head2 = head;




and

toBeInserted = (List *)malloc(sizeof(List));
toBeInserted = create(str);


内存泄漏 [


are memory leaks[^], the first line in both instances is not needed.
You are allocating memory that is not release and to which the pointer is overwritten.

You also need to to free the memory allocated for the list at the end of your program.

In your current code you are losing text because of the

toBeInserted->next = head2->next;

语句,您正在丢失文本.变量toBeInserted是包含很多"的列表的开头,您需要在此语句中使用列表的最后一个节点.

提示:列表中的每个节点都应包含一个单词,而不仅仅是输入的字符串中的一个字符.否则,您将无法使用1将第二个列表添加到第一个列表中.

statement in your insert function. the variable toBeInserted is the head of the list containing "very much", you need to use the last node of the list in this statement.

Hint: Each node in your list should contain a word and not just one character from the string entered. Otherwise you cannot be using 1 to add the second list to the first one.


您忘了使用malloc()strcpy()将字符串复制到您的data元素中项目清单.您要做的只是将指向原始字符串的指针存储在str中(在主循环中),然后在下一次调用fgets()时将其覆盖.尝试使用调试器逐步检查代码,以查看程序运行时变量的确切变化.
You forgot about using malloc() and strcpy() to copy your strings into the data element of your list item. All you are doing is storing a pointer to the original string in str (in your main loop) which you then overwrite with the next call to fgets(). Try stepping through your code with the debugger to see exactly what happens to your variables as your program runs.


这篇关于使用C在指定数目的节点后插入链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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