码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-07-31 21:59:13      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

#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;
}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:

原文地址:http://blog.csdn.net/wangfengfan1/article/details/47175539

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!