Tag: linked list

结构中的“警告:空声明中无用的存储类说明符”

typedef struct item { char *text; int count; struct item *next; }; 所以我有这个结构与上面定义的节点,但我得到下面的错误,我无法弄清楚什么是错的。 警告:空声明中无用的存储类说明符};

复制链接列表并返回指向新列表的指针

我目前的结构是: typedef char AirportCode[4]; typedef struct node{ AirportCode airport; struct node *next; }Node; 关于函数应该如何启动的想法是这样的: Node *copy(Node *list) { int count = 0; while (list != NULL){ count++; list = list->next; } } 现在我们知道原始列表有多长,但我之所以难以理解是因为我不知道如何为每个我们必须复制到第二个列表的单个节点分别分配内存。

插入单链表C

我在C中链接列表fucntion前面的插入有问题 #define arrSIZE = 100; struct listNode { char data[arrSIZE]; struct listNode *nextPtr; }; typedef struct listNode ListNode; void insertHead(ListNode *sPtr, char value[arrSIZE]){ ListNode *newPtr = (ListNode *)malloc(sizeof(ListNode)); strncpy(newPtr->data, value, arrSIZE); if(sPtr ==NULL){ newPtr->nextPtr=NULL; sPtr = newPtr; }else{ newPtr->nextPtr=sPtr; sPtr =newPtr; } }

将Node插入到第一位的C编程中

这是我正在编写的程序的一个function,以便更熟悉节点。 我不确定它是否正确,但实质上是检查Node是否为Null,如果是,那么它将信息添加到代码字段并将指针设置为NULL。 否则,它会创建一个新节点并将信息插入代码字段,然后指向现有的第一个节点。 我不确定如何更改指向原始第一个节点的标头指向新节点。 代码是 typedef struct node { LibraryCode location; struct node *next; } Node; void insertFirstNode(LibraryCode code, Node **listPtr) { Node *n=*listPtr; Node *first; if(n==NULL){ n=malloc(sizeof(Node)); n->location=code; n->next=NULL; } else { Node *first; first->location=code; first->next=n; } }

通过引用传递链接列表时,在第n个位置添加元素

我是新链接列表这是我在LL中插入元素后的第二个问题。现在我试图在第n个位置插入元素。 我这样做: (1)首先在终端上取用户的大小。 (2)第二次从用户连续读取输入直到大小。 (3)我在LL的开头添加了在终端读取的元素。 (4)我打印出LL直到形成。 直到这里一切正常 (5)之后我尝试在LL的第n个位置添加,但是它给出了3个错误我已经在我的代码中的注释中解释了。 还请告诉我,我在第n个位置添加元素的逻辑是否正确? 注意: 我有义务仅在函数调用中传递List节点作为引用(并在函数定义中取消引用它们) 下面是我的完整代码,指出评论中的错误。 #include #include #include #include struct node { int freq; struct node * next; }; typedef struct node node; ///////////////////////////// Function definitions //////////////////////////////////////// insert_beginning(int size, node * * head) { node * temp; temp = (node * ) malloc(sizeof(node)); temp -> freq = size; […]

创建链接列表数组时出现Valgrind错误(对于哈希表链接)

作为概述,我正在尝试在C中创建一个类似战舰的游戏,船只被放置在一个场地上。 这是我得到的错误: ==11147== Invalid write of size 8 ==11147== at 0x400786: MakeField (battleship.c:34) ==11147== Address 0x8 is not stack’d, malloc’d or (recently) free’d 这是相关的代码: struct piece{ int x; int y; int direction; int length; char name; }; struct node{ struct piece boat; struct node *next; }; struct field{ int numBoats; struct node *array[numRows]; }; struct […]

C编程错误,打印链表,在运行时执行代码崩溃

我正在研究链表和指针。 这是一个包含推送function的简单代码。 在推送我的元素并尝试打印第一个成员后,执行的代码在运行时崩溃。 但是,当将相同的指针传递给print_list函数并在print_list中应用printf函数时,它可以正常工作。 但是当在main函数中直接使用它并应用printf函数时,它会崩溃。 #include #include typedef struct list{ int order; struct list *next; }list; void push(struct list **arg,int i); int main() { struct list **ptr=NULL; for(int i=0;iorder); //Here run time error return 0; } void push(struct list **arg,int i){ struct list *temp; temp= malloc(sizeof(list)); temp->order=i; temp->next=*arg; *arg=temp; } void print_list(list ** head) { […]

c中链表中的升序

我试图通过链接和地址而不是值来改变链表中的升序 struct node { char name[30]; int percent; struct node *link; }; int main { clrscr(); randomize(); struct node *st; st=NULL; for(int i=0;ipercent display(st); AscMarks(&st); //Changing the order of links and addresses to arrange them in ascending order printf(“\nAscending order list…\n”); display(st); getch(); return 0; } /*Adds a node at the end of a linked […]

优先级队列C.

我正在尝试使用队列数组创建priority queue ,数组的每个索引都是优先级。 我尝试了以下解决方案, 队列数据类型包含数组llist, Queue *q_create(int size) { struct queue *p; struct q_head *h; int i; if ((p = (struct queue *)malloc(sizeof(struct queue))) != NULL) { p->size = size; for (i = 0; i llist[i]); h->head = NULL; h->tail = NULL; } } return p; } 我h = &(p->llist[i]);条线感到困惑: h = &(p->llist[i]); 我在想llist[i] = […]

涉及链接列表和结构的示例程序不起作用

所以,这是我教授提供的示例程序。 它的function是搜索树的链表并返回天气的结果,它发现用户输入的树。 但是,无论我输入什么,它总是返回false。 它出什么问题了? #include #include #define NL 20 typedef struct tree { char tree_name [NL]; struct tree* next; }tree; int checktree (tree *p, char name[]) { int found = 0; while (p != NULL) { if (strcmp(p -> tree_name, name) == 0) found = 1; p = p -> next; } return (found); } […]