C 从后面插入:链表
我读了这个网站的代码:,但它给了我分段错误,我不太明白 *我把它修改成我的结构C 从后面插入:链表,c,insert,linked-list,C,Insert,Linked List,我读了这个网站的代码:,但它给了我分段错误,我不太明白 *我把它修改成我的结构 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)); st
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(“打印此“句子”);它会在句子中给我错误,剪切我在一个字符串中铸造了另一组“”。Thx一束。在运行第一次插入之前,您需要初始化
head
:
/* This should go in main or some init function, before the first insert */
head = (struct Node *)malloc(sizeof(struct Node));
head->next = NULL;
尝试此操作,它将纠正内存泄漏并检查磁头是否有效。 如果仍然存在分段错误,则应运行调试器以准确了解发生了什么
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;
}
}
EDIT:现在,该函数应该处理任何情况,即使
head
为空。(当列表为空时)首先,您在初始插入时无法设置头指针。这可以通过简单的头检查来完成,但如果插入循环设置正确,则不需要这样做。其次,您正在泄漏内存。这不是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#包括
2.
3使用名称空间标准;
4.
5类型定义结构节点
6 {
7 int data;//将存储信息
8 node*next;//对下一个节点的引用
9 };
10
11
12 int main()
13 {
14节点*head=NULL;//空链表
15内部信息=0,节点编号=0,计数器=0;
16个字符ch;
17
18做{
19.seg故障是由哪条线路引起的?您不需要为temp1
分配内存,否则会出现内存泄漏。struct Node*temp1=head
可以。您能告诉我们struct Node
的内容吗?(以及cmd
成员是什么)我只是看了链接页面的内容,它绝对糟糕。不要使用它。head
定义在哪里?你在任何地方初始化它吗?当我从前面插入时,我把head保持在我的问题中,它工作得很好,有什么原因需要像这样从后面插入吗?@LarsChung因为而(temp1->next!=NULL)
检查(尝试取消引用空指针).我怀疑你的前端插入没有尝试访问head->next
哦,是的,它有点固定,但仍然有一些逻辑错误,我想,我得到了一个额外的节点…我输入了3个字符串,但它给了我4个节点…@LarsChung,这是因为你不需要设置初始节点。如果初始head指针为NULL,你可以这样做已正确选中。@LarsChung,检查我的答案,当列表为空且不添加额外节点时,它将处理这种情况。好的,此问题已解决。Thx!您是否知道如何将字符“(ASCII 34)放入printf字符串中?(例如,如果我执行printf(“打印此“句子”);它将在句子中给我错误,剪切我铸造了另一组字符)在“.”中,@LarsChung是的,只需添加转义字符:printf(“打印这句话”);
nvm,我知道了,我可以做%c并将ascii值放在那里。thx无论如何,“^”@LarsChung,如果你想添加任何ascii字符,不要使用%c,你可以用\x(十六进制ascii值)
:printf(“\x58\x59\x5A”)来完成
将打印XYZ
在操作列表方面类似,我只能看到输入/输出函数调用的不同,它很容易转换。您最好编写函数来处理两种情况:AddToHead()和AddToBack(),而不仅仅是insert()
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;
}
}