单链表的建立/测长/打印

来源:互联网 发布:淘宝 苹果证书 编辑:程序博客网 时间:2024/05/18 13:30
#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)
{
printf("\n please input the data:");
scanf("%d", &x);
if (x != 0)
{
s = (node*)malloc(sizeof(node));
s->data = x;
printf("\n%d", s->data);
p->next = s;
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; int n;
n = length(head);
printf("\n Now,These %d records are:\n", n);
p = head;
while (p != NULL)
{
printf("\n uuu  %d  ", p->data);
p = p->next;
}

}
0 0
原创粉丝点击