鱼C论坛

 找回密码
 立即注册
查看: 2679|回复: 6

动态指针输入和输出

[复制链接]
发表于 2012-7-22 12:30:47 | 显示全部楼层 |阅读模式
1鱼币
#include "stdio.h"
#include "malloc.h"
#define Null 0
#define LEN sizeof(struct student)
int n;
struct student            //定义结构
{
int num;
float score;
struct student *next;
};
struct student * creat(void) //定义一个指针函数 结果返还为地址
{
struct student *head,*p1,*p2; //定义一个结构指针 为head p1 p2
n=0;
p1=p2=(struct student *) malloc(LEN);//开辟新结点 使p1 p2指向新开辟结点的头地址
printf("please input two numbers:");
scanf("%ld%lf",&p1->num,&p1->score);
head=NULL;                          //将头指针置0或者清空
while(p1->num!=NULL)                 //判断条件,当p1->num不为0(NULL)时执行循环
{
  n=n+1;                           
  if(n==1)
  {
   head=p1;                  //为1时 使head指针指向p1
  }
  else p2->next=p1;              //把p2->next指向p1
  p2=p1;
  p1=(struct student *) malloc(LEN);   //再次开辟一个新结点
  scanf("%ld,%lf",&p1->num,&p2->score);
}
  p2->next=NULL;             //p2的指针 指向null
  return(head);             //把head 的地址返回去
}
void print(struct student *head)  
{   struct student *p;   //定义一个结构指针p
   printf("These is NUM%d records are:\n ",n);   
   p=head;  //把头地址给予p
  
    while(p!=NULL)  //当p不等于0时 输出 num 和score
    {
     printf("%ld %lf\n",p->num,p->score);
     p=p->next;  //指向下一个结点
    }
}
void main()
{
struct student *head;  
head=creat();
print(head);
}
结果是错的  我不明白 我错在哪里 我觉得语句都好像是对的啊  求大侠指点下,还有就是 我注释的有错误吗?我是这么理解的,有错误请指出 感激涕零啊

最佳答案

查看完整内容

你主要的错误应该就是在输入整形的时候 输入格式写错了 整形是 %d 长整形才是 %ld

点评

下次发代码 记得用格式发 不会的话 看主题上面有提示  发表于 2012-7-22 19:03
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-7-22 12:30:48 | 显示全部楼层
你主要的错误应该就是在输入整形的时候 输入格式写错了 整形是 %d  长整形才是 %ld  
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-7-22 12:46:39 | 显示全部楼层
我自己发现一处 float没改为double 但是程序还是不对
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-7-22 12:50:55 | 显示全部楼层
  scanf("%ld,%lf",&p1->num,&p2->score);
这段也错了 多加了个,所以输出的时候没注意 。。。但是 还是有点问题:Q
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2012-7-22 15:48:16 | 显示全部楼层
这是不是链表的啊,要是的话。应该先建表,之后是每输入一个数据,动态分配内存,然后连接在一起,最后返回的是表头指针。我有个程序你自己看下吧,感觉好乱。要是我改的话,基本上就是全改了。我还是喜欢用书上教的那个方法比较好。可能吧我还没学好吧。以前上的数据结构。过段时间还真的在看看,都给忘没了。
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct node
{
        int data;
        struct node *pNext;

}NODE,*PNODE;
PNODE create_list(void)//创建链表
{
        int len,val;
        int i;
        printf("请输入单链表的个数:len=");
        scanf("%d",&len);
        printf("\n");
        PNODE pHead=(PNODE)malloc(sizeof(NODE));
        if(NULL==pHead)
        {
                printf("分配内存失败!程序终止\n");
                exit(-1);
        }
        PNODE pTail=pHead;
        pTail->pNext=NULL;
        for(i=0;i<len;i++)
        {
                printf("请输入第%d个单链表的值:",i+1);
                scanf("%d",&val);
                printf("\n");
                PNODE pNew=(PNODE)malloc(sizeof(NODE));
                if(NULL==pHead)
                {
                printf("分配内存失败!程序终止\n");
                exit(-1);
       
                }
                pNew->data=val;
                pTail->pNext=pNew;
                pNew->pNext=NULL;
                pTail=pNew;
        }
        return pHead;
}
bool is_empty_list(PNODE pHead)//判断链表是否为空
{
        if(NULL==pHead->pNext)
                return true;
        else
                return false;

}
void traverse_list(PNODE pHead)//遍历链表
{
        PNODE p=pHead->pNext;
        printf("输出链表的值:\n");
        while(NULL!=p)
        {
                printf("%5d\n",p->data);
                p=p->pNext;
       
        }
        return;       
}
int length_list(PNODE pHead)
{
        int len=0;
        PNODE p=pHead->pNext;
        while(NULL!=p)
        {
                p=p->pNext;
                len++;
        }
        return len;
}
int main(void)
{
        int len=0;
        PNODE pHead=NULL;
    pHead=create_list();
    traverse_list(pHead);
        if(is_empty_list(pHead))
                printf("链表为空!\n");
        else
                printf("链表不为空!\n");
       
        len=length_list(pHead);
        printf("链表的长度是%d\n",len);
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-7-22 16:30:01 | 显示全部楼层
全部都是粗心惹的祸。。都弄清楚了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2012-7-22 19:14:30 | 显示全部楼层
  1. #include "stdio.h"

  2. #include "malloc.h"

  3. #define Null 0

  4. #define LEN sizeof(struct student)

  5. int n=0;

  6. struct student            //定义结构
  7. {
  8.         int num;
  9.         float score;
  10.         struct student *next;
  11. };
  12. struct student * creat(void) //定义一个指针函数 结果返还为地址
  13. {
  14.         struct student *head,*p1,*p2; //定义一个结构指针 为head p1 p2
  15.         p1=p2=(struct student *) malloc(LEN);//开辟新结点 使p1 p2指向新开辟结点的头地址
  16.         printf("please input numbers:");
  17.         scanf("%d",&p1->num);                //整形的输入格式为 %d
  18.         printf("please input score:");
  19.         scanf("%f",&p1->score);               
  20.         head=NULL;                          //将头指针置0或者清空
  21.         while(p1->num)                 //这里是判断当你输入的学号不为0时  这里不能用NULL 因为 p1->num 是一个整形的整数 (也可以直接p1->num)
  22.         {
  23.                 printf("\n\n");
  24.                 n=n+1;                           
  25.                 if(n==1)
  26.                 {
  27.                         head=p1;                  //为1时 使head指针指向p1
  28.                 }
  29.                 else p2->next=p1;              //把p2->next指向p1
  30.                 p2=p1;
  31.                 p1=(struct student *) malloc(LEN);   //再次开辟一个新结点
  32.                 printf("please input numbers:");
  33.                 scanf("%d",&p1->num);                //整形的输入格式为 %d
  34.                 printf("please input score:");
  35.                 scanf("%f",&p1->score);                 
  36.         }
  37.         p2->next=NULL;             //p2的指针 指向null
  38.         return(head);             //把head 的地址返回去
  39. }
  40. void print(struct student *head)  
  41. {
  42.         struct student *p;   //定义一个结构指针p
  43.         printf("These is NUM%d records are:\n ",n);   
  44.         p=head;  //把头地址给予p
  45.         printf("num\tscore\n");
  46.         while(p)  //当p不等于0时 输出 num 和score 当表达式为真时 就被执行 所以可以直接p 就可以咯
  47.         {
  48.                 printf("%d\t%5.2f\n",p->num,p->score);
  49.                 p=p->next;  //指向下一个结点
  50.         }
  51. }
  52. void main()
  53. {
  54.         struct student *head;  
  55.         head=creat();
  56.         print(head);
  57. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-5-28 02:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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