【问题标题】:Data Structures - Linked list library in C数据结构 - C 中的链表库
【发布时间】:2023-09-09 00:00:01
【问题描述】:

我是 C 新手,所以我正在寻求帮助,因为我被困住了。 在创建了一个链表库之后,我可以从中添加、删除和打印用户想要的所有节点,我应该在程序中添加一个整数类型的附加函数,如果值不存在则返回 -1链表。如果值确实存在于链表中,它应该返回元素的位置。

例如在这个链表 (a -> b -> c -> d -> NULL) 中,如果我想知道 c 的位置,它应该返回 3,如果我想知道 G 的位置,它应该返回 -1

这是我迄今为止能够制作的程序:

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

struct ListNode
{ 
char data;
struct ListNode *nextNode;
};
typedef struct ListNode node;


int main()
   {
    node *startNodePtr= NULL;
    int choice;
    char value;
    printf("\t\t\tLIST OF CHARACTERS\n");
    do
    {
        printf("\n1.Add New Node \t2.Delete Node \t3.Print Current List \t4.QUIT\n\n");//user friendly interface
        scanf("%d",&choice);
        switch(choice)
        {
            case 1: printf("Enter Character: ");
                scanf("\n%c",&value);
                insertNode(&startNodePtr,value);//calling the function to add a node
                break;

            case 2: printf("Delete Character: ");
                scanf("\n%c",&value);
                deleteNode(&startNodePtr,value);//calling the function to remove a node
                break;

            case 3: printList(startNodePtr);//calling the function to list the nodes
                break;

            case 4: continue;//if we type 4 it won't show the default answer

            default:printf("\t\tINVALID ANSWER! Please type 1. 2. 3. or 4.\n");// in case we type any other character that is not 1, 2, 3 or 4. In this way the program will not crash
                break;

        }

    } while(choice!=4);//keep adding or deleting nodes until we enter 4 which refers to "QUIT"

return 0;
 }

void insertNode(node **sPtr, char add)//function to add a node
   {
    node *newPtr;
    node *curPtr;
    node *prevPtr;
    newPtr=malloc(sizeof(node));
    newPtr->data=add;
    newPtr->nextNode=NULL;
    prevPtr=NULL;
    curPtr=*sPtr;

    while(curPtr!=NULL)
    {
        prevPtr=curPtr;
        curPtr=curPtr->nextNode;
    }
    if (prevPtr==NULL)
    {
        *sPtr=newPtr;
    }
    else
    {
        prevPtr->nextNode=newPtr;
    }
 }
void deleteNode(node **sPtr, char remove)//function to remove a node
    {
    node *curPtr;
    node *prevPtr;
    curPtr=*sPtr;
    prevPtr=NULL;

   if(curPtr->data==remove)
   {
        *sPtr=curPtr->nextNode;
        free(curPtr);
        return;
}
while (curPtr!=NULL)
{
    if (curPtr->data==remove)
       {prevPtr->nextNode=curPtr->nextNode;
        free(curPtr);
        return;}
    else
       {prevPtr=curPtr;
       curPtr=curPtr->nextNode;}
}
 }


void printList(node *sPtr)//function to list the nodes
{
if (sPtr==NULL)
{
    printf("The list is empty!\n");
}
else
{
    while (sPtr!=NULL)
    {
        printf("\n%c-->", sPtr->data);
        sPtr=sPtr->nextNode;
    }

}   printf("NULL\n\n");

}

【问题讨论】:

  • 好的,有什么问题吗?你已经有了迭代列表的函数,所以......我根本看不到问题:(
  • @MartinJames 是的,这听起来很愚蠢,但我不知道如何创建函数。我可以添加一个案例 5. 调用函数 findFunction(&startNoderPtr, integer) 并在结构中添加一个整数变量,然后?。我有点迷茫
  • 尝试研究“C 中遍历链表获取值”。我认为你应该付出更多的努力来尝试解决问题,而不是仅仅说他是我的问题,你能为我做吗。
  • @sebenalern 你说得对,但我不会说我没有付出努力。最初的问题是制作整个程序;正如我所说,我对它完全陌生CI在一月份参加了这门课,一周前我学会了如何做结构和链接库。我真的坐了4个小时头脑风暴,试图弄清楚如何让它工作。然后我就成功了。在那之前我完成了所有的程序,然后我不知道要做什么以及如何进行。我不是等两天再问我的老师,而是问你们中的某个人。这个论坛是为了互相帮助,不是吗?
  • 好吧,问题是你没有告诉我们你尝试了任何东西,而且问题不是很具体,就像我说的那样,因为这是我的问题,所以你能解决它。跨度>

标签: c linked-list structure


【解决方案1】:

你基本上已经解决了,你只需要计算迭代次数,直到找到正确的元素。

int search(struct ListNode *node, char data) {
    int position = 1;
    // List is empty
    if (node == NULL) {
        return -1;
    }
    while (node != NULL) {
        if (node->data == data) {
            return position;
        }
        position++;
        node = node->nextNode;
    }
    // Element was not in the list
    return -1;
}

【讨论】:

  • mmmh,它看起来不错,但由于某种原因它给了我错误。它说“未知类型名称 ListNode”
  • @Leandro ListNode 替换为 struct ListNode
  • @BLUEPIXY 'case 4: search(&startNodePtr, value);休息;' 'int search(ListNode *node, char data)' 如果我以这种方式调用函数,它会给我错误
  • @Leandro case 4: search(startNodePtr, value); break;, int search(struct ListNode *node, char data) :因为没有改变List,所以&amp;是没有必要的。
  • 你说得对,我没注意到。现在可以了,谢谢。但是如果我输入 4 它不会打印出任何值,它只是重复循环,因为没有发生任何事情