果然是看代码容易,写起来麻烦,一个普通的链表处理,被一堆指针符号和结构体绕来绕去,写惯脚本语言再来研究这些真麻烦,勉强写完了,刚本来rand()给data赋值,结果编译通过了没问题以为完工,然后测试了一把,没用rand()直接赋值,果然结果是错的(因为rand是不清楚到底给链表赋值的对不对),菜鸟就先不要计较代码质量了,能出来再说,(-__-)b
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
}Node;
Node *createlinklist(int n){
Node *p, *L, *r;
int i;
L = (Node *)malloc(sizeof(Node));
L->next = NULL;
r = L;
for (i = 0; i < n; i++){
p = (Node *)malloc(sizeof(Node));
p->data = i;
r->next = p;
r = r->next;
}
r->next = NULL;
return L;
}
int addnode(Node *n, int pos, int num){
int i;
Node *p, *L;
p = n;
i = 0;
while (i < pos – 1){
p = p->next;
++i;
}
L = (Node *)malloc(sizeof(Node));
L->next = NULL;
L->data = num;
L->next = p->next;
p->next = L;
return 0;
}
int delnode(Node *n, int pos){
int i;
Node *L, *p;
p = n;
i = 0;
while (i < pos – 1){
p = p->next;
++i;
}
L = p->next;
p->next = L->next;
free(L);
return 0;
}
int main(){
int num = 10, j;
Node *node;
node = createlinklist(num);
addnode(node, 6, 100);
delnode(node, 3);
Node *s = node->next;
for (j = 0; j < num; j++){
printf(“No %d data is %d \n”, j, s->data);
s = s->next;
}
return 0;
}