链表(初学的)

对于初学者来说数据结构刚接触肯定是感觉还是有一定难度的(当然这不包括天才,我们这些凡夫俗子 不能与之相比我们只能一步步踏踏实实走) 废话不说了 看一下代码:

#include<stdio.h>
#include<stdlib.h>


struct stu{
int data;
struct stu *next;
};
struct stu * creat ()
{
struct stu *head , *current , *pnew;
int i;
if((pnew = (struct stu * )malloc(sizeof(struct stu))) == NULL)
{
printf("空间分配失败!");
exit(0);
}


head=NULL;
int n;
int a;
printf("输入所要加入的节点数\n");
scanf("%d" , &n);
for(i=0;i<n;i++)
{

if((current = (struct stu * )malloc(sizeof(struct stu))) == NULL)
{
printf("空间分配失败!");
exit(0);
}
printf("添加数据\n");
scanf("%d" , &a );

if(NULL == head)
{
printf("ok!");
pnew->data = a ;
pnew->next = head;
head = pnew ;//吧pnew的地址交给 HEAD 即 把首节点的地址交给头指针
printf("%d\n" , pnew->data);
}else{
pnew->next = current ;   //把新分配的节点地址交给上一个节点的指针
current->data = a ;
current->next = NULL; //把新分配的节点中指针 置空
pnew = current ;   // 个人理解 就相当于 把新的节点交给上一个  current 想到与中间变量 实现循环连接
}
}

return head;
}


void show(struct stu * head )
{
struct stu * p;
if(head != NULL)
{
p = head;     //把 头指针赋给
// printf("%3d\n" , head->data );
do{
printf("%3d\n" , p->data);
p=p->next ;
}while(p!=NULL);
}
}
void main()
{
struct stu *head;

head=creat();  //创建链表

show(head);//显示链表

}

如果你不是天才,如果你不承认你是笨蛋 , 那么一分钟能学会的技术,五分钟能学会的技术,就不能叫技术,那只是骗人的把戏,要相信费死劲一个月学会的东西绝对比你一天学会的东西厉害,我的座右铭,持之以恒;坚持到底,永不放弃;

转载于:https://my.oschina.net/u/992853/blog/119027

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值