鱼C论坛

 找回密码
 立即注册
查看: 2221|回复: 0

[技术交流] 结构与共同体04链表程序,勿喷啊,不好之处见谅!

[复制链接]
发表于 2012-7-27 16:30:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
/******************************************
**************这是一个C链表程序***********/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct student
{
        int num;
        int score;
        struct student *next;
};

void main ()
{
        struct student *creat();//链表函数非常特别哦!
        void print();

        struct student *head;
        head=NULL;

        head=creat(head);//创建单链表!
        print(head);
}
int n=0;
struct student *creat(struct student *head)//头指针,好重要啊!
{
        struct student *p1,*p2;//继续申请结构指针变量,不要吝啬啊,呵呵!
        p1=p2=(struct student *)malloc(sizeof(struct student));
        printf("Please input the num: ");
        scanf("%d",&p1->num);
        printf("Please input the score: ");
        scanf("%d",&p1->score);

        p1->next=NULL;//p1前锋,p2后跟!

        while(p1->num!=0)
        {        n++;
                if(head==NULL)
                {
                        head=p1;//空表赋头指针!也是第一次,膜拜ing!
                }
                else
                {
                        p2->next=p1;//小弟也很重要,节点指针哦!
                }
                p2=p1;//第一次从此成为p1小弟!(结构指针变量地址!)

                p1=(struct student *)malloc(sizeof(struct student));//此乃格式,向申请分配节点!

                printf("Please input the num: ");
                scanf("%d",&p1->num);
                printf("Please input the score: ");
                scanf("%d",&p1->score);//前锋啊,膜拜!p2:小弟也!
        }
        p2->next=NULL;
        return head;
}
void print(struct student *head)//头指针,好重要啊!
{
        struct student *temp;//创建临时结构指针变量!
        temp=head;
        printf("the all record is %d",n);
        while(temp!=NULL)
        {       
                printf("%d %d\n",temp->num,temp->score);

                temp=temp->next;//本函数内有节点指针(地址)指向下一个函数首个地址!
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 18:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表