#include <iostream>
#include <string>
#include <conio.h>
#include <cstdlib>
using namespace std;
typedef struct student
{
int data;
student *next;
}node;
node *creat()
{
node *head,*p,*s;
int x=1,cycle=1;
head=(node *)malloc(sizeof(node));
p=head;
while(cycle)
{
cin>>x;
if(x!=0)
{
s=(node *)malloc(sizeof(node));
s->data=x;
cout<<x;
p->next=s;
p=s;
}
else
cycle=0;
}
head=head->next;
p->next=NULL;
return (head);
}
int length(node *head)
{
int n=0;
node *p;
p=head;
while(p!=NULL)
{
n++;
p=p->next;
}
return n;
}
void print(node *head)
{
node *p;
int n;
n=length(head);
p=head;
if(head!=NULL)
{
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
}
}
int main()
{
node * A;
A=creat();
length(A);
print(A);
return 0;
}
编程实现一个单链表实现创建、测长以及打印
最新推荐文章于 2024-01-12 23:28:18 发布