c - 从输入中读取多个值类型以在 c 中生成链表

标签 c string linked-list

我需要根据用户输入的信息创建一个链接列表。用户将立即放入所有信息,并且将包含一个命令,并且可能后跟一个要加载到列表中的字符串或一个 int 值(到引用节点)。即输入:ins Monkey OR prv 1 8(在这些情况下,前 3 个字符是命令,第一个需要插入“monkey”,第二个需要打印值在 1 到 8 之间的节点)

我已经为此工作了数周,但无法弄清楚如何让我的程序处理输入的第二部分。我将所有输入存储到一个数组中,然后根据字符串值将其解析为各种数组,但我什至不知道如何将字符串的结构值设置为命令后放入的字符串。

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

//self referring struct (keystone to LL)
struct Node
{
    char symbol [11];// each Node contains a array 'symbol'
    int count; // each Node contains a symbol counter
    struct Node *next; // points to the next node (this is the self referential aspect)
    };// end struct







int main()
{
    //function prototypes to be used by main
    void insert(struct Node**, struct Node **,char y[]);
    //char delSym(NodePtr *xPtr, char y[]);
    //char forceDel(NodePtr *xPtr, int z);
    void printList(struct Node*);

    struct Node *head, *tail;
     head = tail= NULL;

    //Declare variables needed for input and output
    char input[15];
    char cmd [4];
    char info [11];
    char str [11];
    char *x= info;
    int val=0;

    //possible command strings
    char ins[4]={'i','n','s'};
    char del[4]={'d','e','l'};
    char fde[4]={'f','d','e'};
    char pst[4]={'p','s','t'};
    char prl[4]={'p','r','l'};
    char pcr[4]={'p','c','r'};
    char ppr[4]={'p','p','r'};
    char psu[4]={'p','s','u'};
    char end[4]={'e','n','d'};


    // Prompt user for command and corresponding input
    puts("Please enter a command with corresponding value(s) where necessary");
    fgets(input,15, stdin);

    //Read the command
    memcpy(cmd, &input[0], 4 );// I am getting the command twice followed by the info????
    memcpy(info, &input[4], 11 );

    printf("%s\n", cmd);// put in to check memcpy
    printf("%s\n", info);



//While command is not 'end':
    while (memcmp(end,cmd,3) != 0)
    {
         // Read value(s) for the command, in necessary
         if (memcmp(ins,cmd, 3)==0)
         {
             insert(&head, &tail, info);
         }


.....


    void insert(struct Node **h, struct Node **t, char y[])
{
   struct Node *temp; // New node
   struct Node *previous; // Previous node
   struct Node *nodePtr; // To walk the LL

    //Allocates a new node and store the appropriate data
    if((temp =(struct Node *)maaloc(sizeof(struct Node)))==NULL)
        {
            printf("Node allocation failed.\n");
            exit(1);
        }
    temp->symbol = y;
    temp->count = count++;



    if(!*h)// If the list is empty make newNode the first node
    {
        *h = temp;
        temp->next = NULL;
    }
    else// Insert node if list is not empty
    {
        nodePtr = *h;

        previousNode = NULL;

        while(nodePtr !=NULL)
            {

            if(memcmp(nodePtr->symbol, temp->y, 11)!=0)
            {


                while(nodePtr != NULL && nodePtr->count < temp->count)
                    {
                        previousNode = nodePtr;
                        nodePtr = nodePtr ->next;
                    }
                if (previousNode == NULL)
                {
                    *h = temp;
                    temp->next = nodePtr;

                }
                else
                {
                    previousNode->next  = temp;
                    temp->next = nodePtr;
                }
            }
            else
            {
                nodePtr->count=count++;
            }
            nodePtr= nodePtr->next;
            }
    }

最佳答案

您描述的特定问题可以追溯到这一行:

memcpy(cmd, &input[0], 4 );

此代码将输入的前 4 个字符复制到 cmd[] 中。推测第四个字符是空格,所以 cmd不是字符串,即 cmd不是以 null 结尾的字符数组。因此,printf("%s\n", cmd)是未定义的行为,可能会打印字符直到 0cmd 结尾之后的某个地方遇到.

您可以更改有问题的行,使其仅从输入中复制三个字符:

memcpy(cmd, &input[0], 3);

请注意cmd仍然不是字符串,因为最终元素尚未初始化为 \0然而。您现在可以执行 cmd[3] = '\0' 。初始化cmd可能会更好在声明时:

char cmd[4] = {0};

另请注意 fgets()保留换行符(只要输入缓冲区足够大),因此 info可能包含 \n之前\0您可能希望删除它。

“可能的命令字符串”实际上都不是字符串,它们是字符数组,并且每个字符串中仅初始化了前三个字节。

一个修复方法是添加空终止符:

char ins[4]={'i', 'n', 's', '\0'};

最好从字符串文字初始化,并让编译器确定数组的大小:

char ins[] = "ins";

关于c - 从输入中读取多个值类型以在 c 中生成链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43168534/

相关文章:

c++ - 错误: 'ALIGN' undeclared (first use in this function) with ALIGN defined into macro

c++ - 如何在 hiredis 中使用 SADD 命令?

c - C中链表函数的 append (添加到尾部)打印不正确(不打印最后 append 的元素)

java - Java中无法分配LinkedList头节点以供将来引用

C语言与队列/链表

c++ - 为什么 stdarg.h 有一个宏 « __va_size »?

java - 创建 MIPS 汇编器

javascript - 在什么情况下需要在 JavaScript 中将变量显式转换为字符串?

c - 空字符是否在字符串末尾附加两次

c 替代字符串枚举