1.链表不带头结点(同任务08-1)。
如果没有x,则使之成为第一个结点。
函数返回值为链表的首节点的地址。
typedef struct Node
{
int data;
struct Node * next;
} Node;
Node * insert(Node * list, int x, int e) {
// TODO:
}
c语言实现
1.链表不带头结点(同任务08-1)。
如果没有x,则使之成为第一个结点。
函数返回值为链表的首节点的地址。
typedef struct Node
{
int data;
struct Node * next;
} Node;
Node * insert(Node * list, int x, int e) {
// TODO:
}
c语言实现
供参考:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node* next;
} Node;
Node* insert(Node* list, int x, int e) {
// TODO:
Node* pre = NULL, * p = list;
Node* newp = (Node*)malloc(sizeof(Node));
newp->data = e;
newp->next = NULL;
while (p && p->data != x) {
pre = p;
p = p->next;
}
if (!p || p == list) {
newp->next = list;
list = newp;
}
else {
newp->next = pre->next;
pre->next = newp;
}
return list;
}