链表的详细介绍与操作说明
1.链表的使用与介绍
2.静态链表的介绍和应用
3.动态链表的介绍
3-1.建立和输出链表
3-2.查找链表节点
3-3.删除链表节点
3-4.插入链表节点
链表![链表的定义](https://i-blog.csdnimg.cn/blog_migrate/8d35464dfa2e7a2c69fbe91618875c4e.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b47604ad146b85ef53ff1c6e948426e6.png)
静态链表
/*一维数组动态分配
int n;
int *a=(int *)malloc(sizeof(int)*n);
/*二维数组动态分配
int r,c;
int **a=(int **)malloc(sizeof(int*)*r);
for(int i=0;i<r;i++)
a[i]=(int *)malloc(sizeof(int)*c);
/*结构体类型的动态分配
struct good *s=(struct good*)malloc(sizeof(struct good));*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct good
{
char name[10];
float price;
struct good *next;
};
struct good *Creat_list()
{
struct good *g1,*g2,*head=NULL;
g1=g2=(struct good *)malloc(sizeof(struct good));
scanf("%s %f",&(*g1).name,&(*g1).price);
(*g1).next=NULL;
int n=0;
while(strcmp((*g1).name,"end")!=0)
{
n+=1;
if(n==1){head=g1;}
else{(*g2).next=g1;}
g2=g1;
g1=(struct good *)malloc(sizeof(struct good));
scanf("%s %f",&(*g1).name,&(*g1).price);
(*g1).next=NULL;
}
free(g1);
return head;
}
void Display_list(struct good *head)
{
struct good *p;
p=head;
while(p!=NULL)
{
printf("%s,%.2f\n",(*p).name,(*p).price);
p=(*p).next;
}
}
good *head,*p;
char s[100];
printf("请输入商品名称,价格:\n");
head=Creat_list();
printf("\n商品信息:\n");
Display_list(head);
return 0;
}