我从这个网站阅读了代码: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;
}
这段代码有什么问题?
好了,这个问题解决了。谢谢盖兹'^'!你知道如何将字符"(ASCII 34)放入printf字符串中吗?(例如,如果我做printf("Print this "句子"); 它会给我句子错误,剪切我在"内投射了另一组"。谢谢一堆。
首先,您未能在初始插入时设置头部指针。这可以通过简单的头部检查来完成,但如果插入环设置正确,则不需要这样做。其次,你正在泄漏内存。这不是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,通过查看更新的帖子,看起来您正在正确执行。
最后,不要在 C 程序中强制转换malloc()
结果。
试试这个,它将纠正内存泄漏并检查磁头是否有效。如果仍然有分段错误,则应运行调试器以确切了解发生了什么。
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
为空。(当列表为空时)
运行第一次插入之前初始化head
:
/* This should go in main or some init function, before the first insert */
head = (struct Node *)malloc(sizeof(struct Node));
head->next = NULL;
看到你引用的链接,还有一个测试文件,http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=24684,它会告诉你为什么会出现这个错误,当从后面插入时,它将首先检查头部是否为空,并为第一个元素分配空间。
块引用
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<<"nn";
20 cout<<"0.Quitn";
21 cout<<"1.Insert at firstn";
22 cout<<"2.Traversen";
23 cout<<"3.Insert at lastn";
24 cout<<"4.Insert after specified number of noden";
25 cout<<"5.Delete at first noden";
26 cout<<"6.Delete at last noden";
27 cout<<"7.Delete specified number of noden";
28 cout<<"8.Sort nodesn";
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;
}
}