#include <stdio.h>
int data;
struct Node*next;
Lnode *r,*p,*head;
head = (Lnode *)malloc(sizeof(Lnode));//指针类型,head是一个指针
if(head == NULL){
printf("申请空间失败。");
return NULL;
}
int data;
r=head;
for(int i = 0;i < e; i++){ //给链表赋值
scanf("%d",&data);
p=(Lnode *)malloc(sizeof(Lnode));
if(p==NULL){
printf("no memory available\n");
}
else{
p->data= data;
p->next= NULL;
r->next= p;
r= p;
}
}
return head;
Lnode *p,*s;
p = head;
int insertdata;
while(p->next->data!= e){
p=p->next;
}
s = (Lnode *)malloc(sizeof(Lnode));
printf("please input the data you want insert:");
scanf("%d",&insertdata);
s->data=insertdata;
s->next=p->next;
p->next=s;
printf("insert succeed\n");
return head;
Lnode *p,*s;
p=head;
while(p->next->data==e){
s=p->next;
p->next=s->next;
free(s);
}
printf("delete succeed\n");
return head;
Lnode *p;
int num=0;
p=head->next;
while(p->data!= e){
num++;
p=p->next;
}
return num;
Lnode *p;
p=head->next;
while(p->next!=NULL){
printf("%d",p->data);
p=p->next;
}
printf("\n");
{
LinkList L;
int length;
int insertpos;
int deletedata;
int finddata;
int pos;
printf("please input the length of the link:\n");
scanf("%d",&length);
L = init_List(length);
int choice;
printf("1--Insert\n2--Delete\n3--Find\n4--Output\n");
printf("请输入你的选择:\n");
scanf("%d",&choice);
while(choice!= 0){ //利用while进行循环操作,switch进行选择不同的操作
switch(choice){
case 1://插入
printf("Insert:\n");
printf("please input you pos you want to insert:\n");
scanf("%d",&insertpos);
L=Insert(L,insertpos);
break;
case 2://删除
printf("Delete:\n");
printf("please input the data youwant to delete:\n");
scanf("%d",&deletedata);
Delete(L,deletedata);
break;
case 3://定位
printf("Find\n");
printf("please input the data you want to find:\n");
scanf("%d",&finddata);
printf("the position is:\n");
pos=Find(L,finddata);
printf("%d",pos);
break;
case 4:
printf("Travel:\n");
Travertal(L);
break;
}
scanf("%d",&choice);
}
if(choice==0){
printf("end\n");
}
return 0;
#include <stdlib.h>
int data;
struct Node*next;
}Lnode,*LinkList;
Lnode *r,*p,*head;
head = (Lnode *)malloc(sizeof(Lnode));//指针类型,head是一个指针
if(head == NULL){
printf("申请空间失败。");
return NULL;
}
int data;
r=head;
for(int i = 0;i < e; i++){ //给链表赋值
scanf("%d",&data);
p=(Lnode *)malloc(sizeof(Lnode));
if(p==NULL){
printf("no memory available\n");
}
else{
p->data= data;
p->next= NULL;
r->next= p;
r= p;
}
}
return head;
}
Lnode *p,*s;
p = head;
int insertdata;
while(p->next->data!= e){
p=p->next;
}
s = (Lnode *)malloc(sizeof(Lnode));
printf("please input the data you want insert:");
scanf("%d",&insertdata);
s->data=insertdata;
s->next=p->next;
p->next=s;
printf("insert succeed\n");
return head;
}
Lnode *p,*s;
p=head;
while(p->next->data==e){
s=p->next;
p->next=s->next;
free(s);
}
printf("delete succeed\n");
return head;
}
Lnode *p;
int num=0;
p=head->next;
while(p->data!= e){
num++;
p=p->next;
}
return num;
}
Lnode *p;
p=head->next;
while(p->next!=NULL){
printf("%d",p->data);
p=p->next;
}
printf("\n");
}
{
LinkList L;
int length;
int insertpos;
int deletedata;
int finddata;
int pos;
printf("please input the length of the link:\n");
scanf("%d",&length);
L = init_List(length);
int choice;
printf("1--Insert\n2--Delete\n3--Find\n4--Output\n");
printf("请输入你的选择:\n");
scanf("%d",&choice);
while(choice!= 0){ //利用while进行循环操作,switch进行选择不同的操作
switch(choice){
case 1://插入
printf("Insert:\n");
printf("please input you pos you want to insert:\n");
scanf("%d",&insertpos);
L=Insert(L,insertpos);
break;
case 2://删除
printf("Delete:\n");
printf("please input the data youwant to delete:\n");
scanf("%d",&deletedata);
Delete(L,deletedata);
break;
case 3://定位
printf("Find\n");
printf("please input the data you want to find:\n");
scanf("%d",&finddata);
printf("the position is:\n");
pos=Find(L,finddata);
printf("%d",pos);
break;
case 4:
printf("Travel:\n");
Travertal(L);
break;
}
scanf("%d",&choice);
}
if(choice==0){
printf("end\n");
}
return 0;
}
错误点:
1.在定义节点时:
typedef struct Node{
int data;
struct Node*next;
}Lnode,*LinkList;
struct Node *next,是定义一个指向节点的指针,所以在*前或者说是在struct之后要有 Node ,才能说明next这个指针是指向节点的
2. s = (Lnode *)malloc(sizeof(Lnode));
在申请空间的时候,s为一个指向节点的指针,所以说申请的空间大小为一个节点的大小,而s是个指针,所以必须强制转化为指向节点的指针类型
模糊点:有待明确
在主函数中定义一个指向节点的指针L,也就是相当于链表头,每次调用的函数中都各自定义一个头节点,函数结束是返回到主函数中,改变主函数中的L,以此传递链表在操作后的结果。
不知道这种方法是不是最简洁易懂的,需要在以后的编写中慢慢摸索。