#include<iostream>
#include<stdio.h>
#include<string.h>
#include<conio.h>//conio是Console Input/Output(控制台输入输出)的简写,其中定义了通过控制台进行数据输入和数据输出的函数,主要是 一些用户通过按键盘产生的对应操作
using namespace std;
typedef struct student
{
int data;
struct student *next;
}node;
node *creat() //创建链表
{
node *head,*p,*s;//定义头指针
int x,cycle=1;
head=(node*)malloc(sizeof(node));
//开辟一个大小为结构体长度的内存区,由于malloc带回的是不指向任何类型数据的指针(void*类型),而head是指向结构体类型的,所以用强制类型转换,(node*),其中*不可省略,否则就转换成结构体类型的了,而不是指针类型。
p=head;//p是当前指针
while(cycle) //循环输入值
{
printf("\nplease input the data:");
scanf("%d",&x);
if(x!=0) //不为0时
{
s=(node*)malloc(sizeof(node)); //开辟空间,放入数据,s负责开辟新结点
s->data=x;
printf("\n%d",s->data);
p->next=s; //p(当前结点衔接上去,连接新结点)连接动作
p=s;//当前结点指向新结点
}
else cycle=0;
}
head=head->next;
p->next=NULL;
printf("\n yyy %d",head->data);
return(head);//返回头指针
}
// 测长
int length(node *head)
{
int n=0;
node *p;
p=head;
while(p!=NULL)
{
p=p->next;
n++;
}
return n;
}
链表打印 ///
void print(node *head)
{
node *p;
p=head;
while(p!=NULL)
{
printf("%d",p->data);
p=p->next;
}
}