我从这个网站上读到了代码:http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-using-C-C,但它给了我分段错误,但我还是不太明白。
*我将其修改为我的结构
struct Node
{
int type;
char cmd[256];
struct Node *next;
};
struct Node *head = NULL;
void insert(int val, char arr[])
{
struct Node *temp1 = (struct Node*)malloc(sizeof(struct Node));
struct Node *temp2 = (struct Node*)malloc(sizeof(struct Node));
temp1 = head;
while(temp1->next != NULL)
temp1 = temp1->next;
temp2->type = val;
strcpy(temp2->cmd, arr);
temp2->next = NULL;
temp1->next = temp2;
}
此代码有什么问题?
好的,这个问题已经解决了。 Thx Guyz'^'!你知道怎么把charcters“(ASCII 34)放到printf字符串中?(例如,如果我做printf(”打印这个“句子”“);它会给我一个错误的句子,剪切我铸造另一组“”在一个“”里面。很多。
答案 0 :(得分:3)
首先,您未能在初始插入时设置头指针。这可以通过简单的头部检查来完成,但如果插入环路设置正确则不需要。其次,你是在泄漏记忆。这不是Java。覆盖保存动态分配地址的指针就像将内存扔出窗口一样好。
这是一种方法,可以在插入代码中隐藏if (head == NULL)
特殊情况。与流行的观点相反,如果您这样做,则不需要这种特殊情况:
void insert(int val, char arr[])
{
struct Node **pp = &head;
while (*pp)
pp = &(*pp)->next;
*pp = malloc(sizeof(**pp));
(*pp)->next = NULL;
(*pp)->type = val;
strcpy((*pp)->cmd, arr);
}
在进行任何插入操作之前,请确保head
初始化为NULL,通过查看更新后的帖子看起来您的工作正常。
答案 1 :(得分:1)
您需要在运行第一个插入之前初始化head
:
/* This should go in main or some init function, before the first insert */
head = (struct Node *)malloc(sizeof(struct Node));
head->next = NULL;
答案 2 :(得分:1)
试试这个,它会纠正内存泄漏并检查头是否有效。 如果仍然存在分段错误,则应运行调试器以确切知道发生了什么。
void insert(int val, char arr[])
{
struct Node *temp2 = malloc(sizeof(struct Node));
temp2->type = val;
strcpy(temp2->cmd, arr);
temp2->next = NULL;
if (head == NULL) {
//list is empty, head must points on the created node
head = temp2;
}
else {
struct Node *temp1 = head;
while(temp1->next != NULL)
temp1 = temp1->next;
temp1->next = temp2;
}
}
编辑:现在,即使head
为空,此函数也应处理任何情况。 (当列表为空时)
答案 3 :(得分:0)
看到你引用的链接,还有一个测试文件,http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=24684,它会告诉你为什么会出现这个错误,当从后面插入时,它会首先检查头是否为null并分配空间第一个元素。
块引用
1 #include<iostream>
2
3 using namespace std;
4
5 typedef struct node
6 {
7 int data; // will store information
8 node *next; // the reference to the next node
9 };
10
11
12 int main()
13 {
14 node *head = NULL; //empty linked list
15 int info = 0, node_number = 0, counter = 0;
16 char ch;
17
18 do{
19 cout<<"\n\n";
20 cout<<"0.Quit\n";
21 cout<<"1.Insert at first\n";
22 cout<<"2.Traverse\n";
23 cout<<"3.Insert at last\n";
24 cout<<"4.Insert after specified number of node\n";
25 cout<<"5.Delete at first node\n";
26 cout<<"6.Delete at last node\n";
27 cout<<"7.Delete specified number of node\n";
28 cout<<"8.Sort nodes\n";
29
30 cout<<"Enter your choice: ";
31 cin>>ch;
32
33 switch(ch)
34 {
35
36 case '0': break;
37
38 case '1': ....
..... case '3':{
**// check linked list is empty**
if(head==NULL)
{
cout<<"ENTER ANY NUMBER:";
cin>>info; // take input data
cout<<"Input data: "<<info;
node *temp; // create a temporary node
temp = (node*)malloc(sizeof(node)); // allocate space for node
temp->data = info; // store data(first field)
temp->next = NULL; // second field will be null
head = temp; // transfer the address of 'temp' to 'head'
counter++;
}
else
{
cout<<"ENTER ANY NUMBER:";
cin>>info; // take input data
cout<<"Input data: "<<info;
node *temp1; // create a temporary node
temp1=(node*)malloc(sizeof(node)); // allocate space for node
temp1 = head; // transfer the address of 'head' to 'temp'
while(temp1->next!=NULL) // go to the last node
temp1 = temp1->next; //tranfer the address of 'temp->next' to 'temp'
node *temp; // create a temporary node
temp = (node*)malloc(sizeof(node));// allocate space for node
temp->data = info; // store data(first field)
temp->next = NULL; // second field will be null(last node)
temp1->next = temp; // 'temp' node will be the last node
break;
}
}