我试图使用链接列表在C中编写一个队列(String Version)程序.
这是结构:
struct strqueue;
typedef struct strqueue *StrQueue;
struct node {
char *item;
struct node *next;
};
struct strqueue {
struct node *front;//first element
struct node *back;//last element in the list
int length;
};
Run Code Online (Sandbox Code Playgroud)
我首先创建一个新的StrQueue
StrQueue create_StrQueue(void) {
StrQueue q = malloc(sizeof (struct strqueue));
q->front = NULL;
q->back = NULL;
q->length = 0;
return q;
}
Run Code Online (Sandbox Code Playgroud)
制作str的副本并将其放在队列的末尾
void push(StrQueue sq, const char *str) {
struct node *new = malloc(sizeof(struct node));
new->item = NULL;
strcpy(new->item,str);//invalid write size of 1 ? …Run Code Online (Sandbox Code Playgroud) 比方说,我有一个名为"tests"的文件,它包含
a
b
c
d
Run Code Online (Sandbox Code Playgroud)
我试图逐行读取这个文件,它应该输出
a
b
c
d
Run Code Online (Sandbox Code Playgroud)
我创建了一个名为"read"的bash脚本,并尝试使用for循环读取此文件
#!/bin/bash
for i in ${1}; do //for the ith line of the first argument, do...
echo $i // prints ith line
done
Run Code Online (Sandbox Code Playgroud)
我执行它
./read tests
Run Code Online (Sandbox Code Playgroud)
但它给了我
tests
Run Code Online (Sandbox Code Playgroud)
有谁知道发生了什么?为什么打印"测试"而不是"测试"的内容?提前致谢.
如果我们想要使用get in c,我们将执行以下操作:
int main(void) {
char str[100];
while (gets(str)) {
printf("%s\n",str);
}
}
Run Code Online (Sandbox Code Playgroud)
我们必须首先知道str的长度(即100),然后使用gets.是否可以在不知道c中数组长度的情况下使用gets?