数据结构
12月5日 周二
问题:
单链表构建不懂还在学习中
懵懂 以后翻回来看
代码
/*
链式存储:逻辑上相邻物理上不一定相邻
sizeof 检测某个类型所占字节数
typedef 给类型 起别名
malloc 申请字节
free(p) p=null
*/
#include "stdio.h"
struct node
{
int data;
struct node *next;
};
void main()
{
struct node *Head,*p,*s;
int t=5,e,i;//节点的数量
//开辟一个空间
Head=(struct node *)malloc(sizeof(struct node));//强转 申请内存 计算字节 类型
s=Head;
for(i=1;i<=t;i++)
{
printf("请输入第%d个元素:",i);
scanf("%d",&e);
p=(struct node *)malloc(sizeof(struct node));
p->data=e;
p->next=NULL;
s->next=p;//把第一个的指针域链到头指针上去
s=p;//把第一个元素的地址赋值给s
}
printf("输出链表中的元素:\n");
s=Head->next;
for(i=1;i<=t;i++)
{
printf("输出%d\n",s->data);
s=s->next;
}
}
新知识:
感悟:
栈的应用
递归函数:调用自己
一句话:整理好心情再次出发。
12月5日 周二
问题:
单链表构建不懂还在学习中
懵懂 以后翻回来看
代码
/*
链式存储:逻辑上相邻物理上不一定相邻
sizeof 检测某个类型所占字节数
typedef 给类型 起别名
malloc 申请字节
free(p) p=null
*/
#include "stdio.h"
struct node
{
int data;
struct node *next;
};
void main()
{
struct node *Head,*p,*s;
int t=5,e,i;//节点的数量
//开辟一个空间
Head=(struct node *)malloc(sizeof(struct node));//强转 申请内存 计算字节 类型
s=Head;
for(i=1;i<=t;i++)
{
printf("请输入第%d个元素:",i);
scanf("%d",&e);
p=(struct node *)malloc(sizeof(struct node));
p->data=e;
p->next=NULL;
s->next=p;//把第一个的指针域链到头指针上去
s=p;//把第一个元素的地址赋值给s
}
printf("输出链表中的元素:\n");
s=Head->next;
for(i=1;i<=t;i++)
{
printf("输出%d\n",s->data);
s=s->next;
}
}
新知识:
感悟:
栈的应用
递归函数:调用自己
一句话:整理好心情再次出发。