【C语言】动态链表和静态链表的创建

动态链表和静态链表

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct wep{
	//char name[16];
	int pwt;
	float price;
	struct wep *next;
};

/** 创建动态链表 **/
struct wep * create(){
	struct wep *p1,*p2,*head;
	int n = 0;
	p1 = p2 = (struct wep*)malloc(sizeof(struct wep));
	scanf("%d,%f",&p1->pwt,&p1->price);
	head = NULL;
	while(p1->pwt != 0){
		n++;
		if(n==1) head=p1;
		else p2->next = p1;
		p2 = p1;
		p1 = (struct wep*)malloc(sizeof(struct wep));
		scanf("%d,%f",&p1->pwt,&p1->price);
	}
	p2->next = NULL;
	return head;
}
int main(void)
{
	/*struct wep w_1 = {"shutdo",20,100.84};
	struct wep warr[2] = {{"sh_1",30,122.99},{"sh_2",50,220.88}};
	struct wep *w;
	struct wep *w_arr;
	w_arr = warr;
	w = &w_1;
	printf("%s -- %d  ---- %.2f \n",w_1.name,w_1.pwt,w_1.price);
	printf("%s -- %d  ---- %.2f \n",(*w).name,(*w).pwt,(*w).price);
	printf("%s -- %d  ---- %.2f \n",w->name,w->pwt,w->price);
	printf("%s -- %d  ---- %.2f \n",w_arr->name,w_arr->pwt,w_arr->price);
	printf("%p\n",w_arr);
	w_arr++;
	printf("%p\n",w_arr);
	printf("%s -- %d  ---- %.2f \n",w_arr->name,w_arr->pwt,w_arr->price);
	printf("%u\n",sizeof(struct wep));*/

	/**静态单向链表start***/
	/*struct wep a,b,c,*head;
	//a.name = {"tom"};
	a.pwt = 18;
	a.price = 19.8;
	//b.name = {"jim"};
	b.pwt = 20;
	b.price = 122.88;
	//c.name = {"lily"};
	c.pwt = 77;
	c.price = 19.99;
	head = &a;
	a.next = &b;
	b.next = &c;
	c.next = NULL;
	struct wep *p;
	p=head;
	while(p!=NULL){
		printf("-- %d -- %0.2f\n",(*p).pwt,(*p).price);
		p = p->next;
	}*/
	/**静态单向链表end***/

	struct wep *p;
	p = create();
	printf("%d-----%f\n",p->pwt,p->price);
	return EXIT_SUCCESS;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

深入沟通_it6688668

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值