#include"iostream"
#include"stdio.h"
#include"string.h"
#include"conio.h"
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));
p = head;
while (cycle)
{
cout << "\nplease input the data" << endl;
cin >> x;
if (x!=0)
{
s = (node*)malloc(sizeof(node));
s->data = x;
cout << s->data << endl;
p->next = s;
p = p->next;
}
else cycle = 0;
}
head = head->next;
p->next = NULL;
cout << head->data << endl;
return head;
}
int length(node* head)
{
int i = 0;
while (head != NULL)
{
i++;
head = head->next;
}
return i;
}
void print(node *head)
{
while (head!=NULL)
{
cout << head->data << endl;
head = head->next;
}
}
int main()
{
int len;
node* l1 = creat();
len = length(l1);
print(l1);
cout << "length:" << len << endl;
system("pause");
return 0;
}
#include"stdio.h"
#include"string.h"
#include"conio.h"
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));
p = head;
while (cycle)
{
cout << "\nplease input the data" << endl;
cin >> x;
if (x!=0)
{
s = (node*)malloc(sizeof(node));
s->data = x;
cout << s->data << endl;
p->next = s;
p = p->next;
}
else cycle = 0;
}
head = head->next;
p->next = NULL;
cout << head->data << endl;
return head;
}
int length(node* head)
{
int i = 0;
while (head != NULL)
{
i++;
head = head->next;
}
return i;
}
void print(node *head)
{
while (head!=NULL)
{
cout << head->data << endl;
head = head->next;
}
}
int main()
{
int len;
node* l1 = creat();
len = length(l1);
print(l1);
cout << "length:" << len << endl;
system("pause");
return 0;
}