void push_tail(struct LinkList* list, struct Node* temp) { temp->pro = list->head; temp->next = list->tail; list->tail->pro = temp; list->head->next = temp; list->head = temp; }这是双向链表的头插法还是尾插法
时间: 2024-03-18 16:43:21 浏览: 18
这是双向链表的尾插法。在这个函数中,我们将新节点temp插入到链表的尾部,即temp成为新的尾节点。具体来说,我们将temp的前驱指针pro指向当前链表的头节点list->head,将temp的后继指针next指向当前链表的尾节点list->tail,然后将当前链表的尾节点的前驱指针list->tail->pro指向temp,将当前链表的头节点的后继指针list->head->next指向temp,最后更新链表的头节点为temp,完成插入操作。
相关问题
void push_tail(struct LinkList* list, struct Node* temp) { temp->pro = list->head; temp->next = list->tail; list->tail->pro = temp; list->head->next = temp; list->head = temp; }优化一下这个代码,使用双向链表的头插法
void push_head(struct LinkList* list, struct Node* temp) {
temp->next = list->head->next;
temp->pro = list->head;
list->head->next->pro = temp;
list->head->next = temp;
list->tail = (list->tail == list->head) ? temp : list->tail;
}
这是使用双向链表头插法的代码。需要注意的是,当链表为空时,头尾节点都需要指向新插入的节点。因此,需要判断一下链表是否为空。
查找错误并举出、修改#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> typedef struct num { float a; //系数 int b; //指数 struct num *next; }*num; struct LinkList // 链表类型 { num head;// 分别指向线性链表中的头结点和最后一个结点 感觉不需要tail int len; // 指示线性链表中数据元素的个数 }; struct LinkList *init(struct LinkList *list)//创建空链表 { list = (struct LinkList *)malloc(sizeof(struct LinkList)); list->len = 0; list->head = (struct num*)malloc(sizeof(struct num));//list->tail = list->head->next = NULL;//list->tail->next = return list; }; void compare(struct LinkList *list, float a, int b)//比较指数 { int i = 0; struct num*p = list->head; for (i; i <= list->len; i++) { if (b > p->b) p = p->next; else if (b = p->b){ p->b += b; break; } else{ add(list, i, a, b);//插入 break; } } if (i>list->len) add(list, i, a, b);//添加到最后一个 }; void add(struct LinkList *list, int index, float a,int b)//添加新的指数项 { struct num*p = list->head, *s; int i; for (i = -1; i<index - 1; i++) { p = p->next; } s = (struct num *)malloc(sizeof(struct num)); s->a = a; s->b = b; s->next = p->next; p->next = s; list->len++; //if (index == list->len) 感觉不需要尾结点 // list->tail = s; }; int main(){ //指数升序查找 struct LinkList *lista, *listb ; lista = init(lista); listb = init(listb); int n,b; float a; scanf("%d", &n); for (int i = 0; i < n; i++)//lista { scanf("%f%d", &a,&b); compare(lista, a, b); } scanf("%d", &n); for (int i = 0; i < n; i++)//listb { scanf("%f%d", &a, &b); compare(listb, a, b); } return 0; }
在这段代码中,有两个问题:
1. 在定义结构体 `num` 时,没有为 `next` 成员指定类型,应该将 `*num` 改为 `struct num *`。
2. 在函数 `init` 中,没有为头结点的 `next` 成员赋值,应该为其赋值为 `NULL`。
下面是修改后的代码:
```c
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
typedef struct num {
float a; //系数
int b; //指数
struct num* next;
} *num;
struct LinkList // 链表类型
{
num head; // 分别指向线性链表中的头结点和最后一个结点
int len; // 指示线性链表中数据元素的个数
};
struct LinkList* init(struct LinkList* list) //创建空链表
{
list = (struct LinkList*)malloc(sizeof(struct LinkList));
list->len = 0;
list->head = (num)malloc(sizeof(struct num));
list->head->next = NULL;
return list;
};
void compare(struct LinkList* list, float a, int b) //比较指数
{
int i = 0;
num p = list->head;
for (i; i <= list->len; i++)
{
if (b > p->b)
p = p->next;
else if (b == p->b) {
p->a += a;
break;
}
else {
add(list, i, a, b); //插入
break;
}
}
if (i > list->len)
add(list, i, a, b); //添加到最后一个
};
void add(struct LinkList* list, int index, float a, int b) //添加新的指数项
{
num p = list->head, *s;
int i;
for (i = -1; i < index - 1; i++)
{
p = p->next;
}
s = (num)malloc(sizeof(struct num));
s->a = a;
s->b = b;
s->next = p->next;
p->next = s;
list->len++;
};
int main()
{
//指数升序查找
struct LinkList* lista, * listb;
lista = init(lista);
listb = init(listb);
int n, b;
float a;
scanf("%d", &n);
for (int i = 0; i < n; i++) //lista
{
scanf("%f%d", &a, &b);
compare(lista, a, b);
}
scanf("%d", &n);
for (int i = 0; i < n; i++) //listb
{
scanf("%f%d", &a, &b);
compare(listb, a, b);
}
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)