插入链接列表

时间:2013-12-30 23:41:54

标签: c linked-list

在构建链接列表时,我在插入包含字符串中所有字符的完整句子时遇到了一点困难。

我希望能够插入如下字符串:word_#_2003_#_definition 但是,当我在我的main()方法中运行我的代码时,它会继续重复这些选择,因为它永远不会停止让我输入选项。希望很清楚。

这是我的struct

struct node
{
    char data[100];
    struct node *previous;  // Points to the previous node
    struct node *next;   // Points out to the next node
} *head, *last;

这是我插入节点的功能:

void insert_beginning(char words[99])
{
    struct node *var, *temp;
    var=(struct node *)malloc(sizeof(struct node)); //explination about the (node *)
    strncpy(var->data, words,99);

    if (head==NULL)
    {
        head=var;
        head->previous=NULL;
        head->next=NULL;
        last=head;
    }
    else
    {
        temp=var;
        temp->previous=NULL;
        temp->next=head;
        head->previous=temp;
        head=temp;
    }
}

这是我的main()方法:

int main()
{
    char loc[99];
    char words[99];
    int i, dat;

    head=NULL;

    printf("Select the choice of operation on link list");
    printf("\n1.) Insert At Begning\n2.) Insert At End\n3.) Insert At Middle");
    printf("\n4.) Delete From End\n5.) Reverse The Link List\n6.) Display List\n7.)Exit");

    while(1)
    {
        printf("\n\n Enter the choice of operation you want to do ");
        scanf("%d",&i);

        switch(i)
        {
            case 1:
            {
                printf("Enter a word you want to insert in the 1st node ");
                scanf(" %s",words);

                insert_beginning(words);
                display();
                break;
            }

关于如何做的任何想法?

2 个答案:

答案 0 :(得分:1)

代码非常值得怀疑:

  • 永远不会检查返回代码。你必须检查返回代码, 特别是如果你使用scanf
  • 您需要使用scanf清空整个缓冲区,否则您将继续 阅读下一个命令的旧内容
  • 更好的选择是使用sscanf
  • 您必须初始化变量,例如最后一次,但还有其他变量 未初始化的变量案例
  • 您的数据类型未定义,这将产生安全问题 有时是99个字符,有时是100个字符。
  • insert_beginning不应该返回void,内存分配可能会失败

答案 1 :(得分:1)

main()中的代码可能看起来更像:

int main()
{
    char loc[99];
    char words[99];
    int i, dat;

    head = NULL;

    printf("Select the choice of operation on link list");
    printf("\n1.) Insert At Beginning\n2.) Insert At End\n3.) Insert At Middle");
    printf("\n4.) Delete From End\n5.) Reverse The Link List\n6.) Display List\n7.) Exit\n");

    while(1)
    {
        printf("\nEnter the choice of operation you want to do: ");
        if (scanf("%d", &i) != 1)
        {
            fprintf(stderr, "Failed to read a number: exiting\n");
            return 1;
        }

        switch(i)
        {
            case 1:
            {
                printf("Enter a word you want to insert in the 1st node: ");
                if (scanf("%98s", words) != 1)
                {
                    fprintf(stderr, "Failed to read words; exiting\n");
                    return 1;
                }

                insert_beginning(words);
                display();
                break;
            }
            ...
         }
         ...
      }
      ...
   }
   return 0;
}

正如评论中所讨论的那样,您没有从scanf()检查返回状态,因此您不知道它何时失败,并且当它无法读取数字时,它会保留参数({{ 1}})单独,所以你再次回到相同的选项,并冲洗并重复。

基本调试技术(未显示):

  1. 打印您从输入中获得的值。在i错误检查后打印i的值。在scanf()错误检查后打印words的值。

  2. 使用调试器逐步完成。

  3. 创建一个转储(打印)关键数据结构的函数:

    scanf()
  4. 调试时广泛使用结构转储器。以后在修改代码时保留它以供使用。如果做得好,它会非常有帮助。