#include <iostream>
#include <string>
using namespace std;
typedef struct student
{
int data;
struct student *next;
}node;
node *creat()
{
node *head, *p, *s;
int x, cycle = 1;
head = new node;
p = head;
while(cycle)
{
s = new node;
cout << "Enter a interger: " ;
cin >> x;
if (x != 0)
{
s->data = x;
cout << "node data: " << s->data << endl;
p->next = s;
p = s;
}
else
{
cycle = 0;
cout << "the list is to the end." << endl;
}
}
head = head->next;
p->next = NULL;
return head;
}
int length(node *head)
{
int len = 0;
node *p;
p = head;
if(!head->next)
cout << "The list is empty." << endl;
do{
++len;
p = p->next;
}while (p);
cout << "The length is " << len << endl;
return len;
}
void printl(node *head)
{
node *p = head;
cout << "Print the list:" << endl;
int cnt = 1;
while (p != NULL)
{
cout << "The " << cnt <<" element of the list is " << p->data << endl;
p = p->next;
++cnt;
}
}
int main()
{
node *head;
cout << "The list :" << endl;
head = creat();
int len = length(head);
printl(head);
system("pause");
return 0;
}
#include <string>
using namespace std;
typedef struct student
{
int data;
struct student *next;
}node;
node *creat()
{
node *head, *p, *s;
int x, cycle = 1;
head = new node;
p = head;
while(cycle)
{
s = new node;
cout << "Enter a interger: " ;
cin >> x;
if (x != 0)
{
s->data = x;
cout << "node data: " << s->data << endl;
p->next = s;
p = s;
}
else
{
cycle = 0;
cout << "the list is to the end." << endl;
}
}
head = head->next;
p->next = NULL;
return head;
}
int length(node *head)
{
int len = 0;
node *p;
p = head;
if(!head->next)
cout << "The list is empty." << endl;
do{
++len;
p = p->next;
}while (p);
cout << "The length is " << len << endl;
return len;
}
void printl(node *head)
{
node *p = head;
cout << "Print the list:" << endl;
int cnt = 1;
while (p != NULL)
{
cout << "The " << cnt <<" element of the list is " << p->data << endl;
p = p->next;
++cnt;
}
}
int main()
{
node *head;
cout << "The list :" << endl;
head = creat();
int len = length(head);
printl(head);
system("pause");
return 0;
}