| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 694 人关注过本帖
标题:链表的问题
只看楼主 加入收藏
sunmingchun
Rank: 4
来 自:安徽-滁州
等 级:业余侠客
帖 子:198
专家分:277
注 册:2010-4-2
结帖率:84%
收藏
已结贴  问题点数:20 回复次数:8 
链表的问题
这是我写的链表,编译没错误就是有警告请教各位大虾帮我看看!还有怎么运行的时候只能输入两个数据,然后就不能输入呢!不明白。请教各位了。
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN  sizeof(struct student)
struct student
{
    int number;
    char name[10];
    char sex;
    int age;
    struct student *next;
};
int n;
struct student *creat()
{
    struct student *p1,*p2,*head;
    n=0;
    p1=p2=(struct student *)malloc(LEN);
    scanf("%d,%s,%c,%d",&p1->number,p1->name,&p1->sex,&p1->age);
    head=NULL;
    while(p1->age!=0)
    {
        n=n+1;
        if(n==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=(struct student *)malloc(LEN);
        scanf("%d,%s,%c,%d",&p1->number,p1->name,&p1->sex,&p1->age);
    }
    p2->next=NULL;
    return head;
}
struct student *del(struct student *head,int age)
{
    struct student *p1,*p2;
    if(head==NULL)
    {
        printf("list null\n");
        return head;
    }
    p1=head;
    while(age!=p1->age && p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if(age==p1->age)
    {
        if(p1==head)
            head=p1->next;
        else
            p2->next=p1->next;
        n=n-1;
    }
    else
        printf("%d not been found:\n",age);
    return head;
}
struct student *insert(struct student *head,struct student *stu)
{
    struct student *p1,*p2,*p0;
    p0=stu;
    p1=head;
    if(head==NULL)
    {
        head=p0;
    p0->next=NULL;
    }
    else
    {
        while(p1->number<p0->number && p1->next!=NULL)
        {
            p2=p1;
            p1=p1->next;
        }
        if(p0->number==p1->number)
        {
            if(head==p1)
                head=p0;
            else
            {
                p2->next=p0;
            p0->next=p1;
            }
        }
        else
        {
            p1->next=p0;
            p0->next=NULL;
        }
        n=n+1;
    }
    return head;
}
void main()
{
    struct student *head,*stu;
    int age;
    head=creat();
    print(head);
    scanf("%d",&age);
    while(age!=0)
    {
        head=del(head,age);
        print(head);
        printf(" input the deleted age:\n");
        scanf("%d",&age);
    }
    printf("input the insert nunber:\n");
    stu=(struct student*)malloc(LEN);
    scanf("%d,%s,%c,%d",&stu->number,stu->name,&stu->sex,&stu->age);
    while(stu->age!=0)
    {
        head=insert(head,stu);
        print(head);
        stu=(struct student *)malloc(LEN);
        scanf("%d,%s,%c,%d",&stu->number,stu->name,&stu->sex,&stu->age);
    }
}
昨天我从新按照书上的程序又改了一遍可还是又错误啊!
我都看不懂了!希望各位大虾指教啊!
 

 
 



[ 本帖最后由 sunmingchun 于 2010-6-8 18:30 编辑 ]
搜索更多相关主题的帖子: next include number 
2010-06-06 20:48
hexu2010
Rank: 2
等 级:论坛游民
帖 子:29
专家分:35
注 册:2010-2-13
收藏
得分:7 
好长呀.........
2010-06-07 16:58
韩明海
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:253
专家分:749
注 册:2010-4-3
收藏
得分:7 
printf(head);你再那本书上学的?

struct student *creat()
{
    struct student *p1,*p2,*head;
    n=0;
    p1=p2=(struct student *)malloc(LEN);
    scanf("%d,%s,%c,%d",&p1->number,p1->name,&p1->sex,&p1->age);
    head=NULL;
printf("%d\n",p1->number);    看看你输入的是啥东西
printf("%s\n",p1->name);        好好考虑一下吧
printf("%c\n",p1->sex);
printf("%d\n",p1->age);
    while(p1->age!=0)
    {
        n=n+1;
        if(n==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=(struct student *)malloc(LEN);
        scanf("%d,%s,%c,%d",&p1->number,p1->name,&p1->sex,&p1->age);
    }
    p2->next=NULL;
    return head;
}
2010-06-07 20:10
sunmingchun
Rank: 4
来 自:安徽-滁州
等 级:业余侠客
帖 子:198
专家分:277
注 册:2010-4-2
收藏
得分:0 
3楼  我是按照书上写的啊 怎么不对吗?
2010-06-07 20:49
韩明海
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:253
专家分:749
注 册:2010-4-3
收藏
得分:0 
对不对,你按我说的加几个输出语句,看看你输入的是不是你想要的不就清楚了么。调试程序要学会用输出语句
2010-06-08 18:32
sunmingchun
Rank: 4
来 自:安徽-滁州
等 级:业余侠客
帖 子:198
专家分:277
注 册:2010-4-2
收藏
得分:0 
我的程序都输入不了内容啊!
2010-06-08 22:42
韩明海
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:253
专家分:749
注 册:2010-4-3
收藏
得分:0 
哎,不跟你绕弯子了,有scanf就可以输入,只是你的scanf用的不对,看来你真是不明白我得意思,不是输入不了,有scanf怎么可能没有输入呢,把各种输入函数研究一下吧,
struct student
{
    int number;
    char name[10];
    char sex;
    int age;
    struct student *next;
};
#define LEN  sizeof(struct student)
main()
{
    struct student *p1;
    p1=(struct student *)malloc(LEN);
    scanf("%d,%s,%c,%d",&p1->number,p1->name,&p1->sex,&p1->age);
 printf("%d\n",p1->number);   
printf("%s\n",p1->name);        
printf("%c\n",p1->sex);
printf("%d\n",p1->age);
}输入一组数据,看看和输出一样不
2010-06-09 20:18
韩明海
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:253
专家分:749
注 册:2010-4-3
收藏
得分:0 
写个“好长啊”就给七分?如果还不明白,等着别人来吧,我对得起你的七分了
2010-06-09 20:20
BlueGuy
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:29
帖 子:4476
专家分:4055
注 册:2009-4-18
收藏
得分:0 
回复 8楼 韩明海
程序代码:
一:类的定义风格
代码1:不良的写法
class Employee
{ 
    public Employee(String n, double s, int year, int month, int day)
    { 
    name = n;
    salary = s;
    GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
    // GregorianCalendar uses 0 for January
    hireDay = calendar.getTime();
    }

    private String name;
    public String getName()
    { 
    return name;
    }
   
    private double salary;
    public double getSalary()
    { 
    return salary;
    }

    private Date hireDay;
    public Date getHireDay()
    { 
    return hireDay;
    }
    public void raiseSalary(double byPercent)
    { 
    double raise = salary * byPercent / 100;
    salary += raise;
    }

}

代码2:良好的写法
class Employee
{  

    private String name;
    private double salary;
    private Date hireDay;

    public Employee(String n, double s, int year, int month, int day)
    { 
    name = n;
    salary = s;
    GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
    // GregorianCalendar uses 0 for January
    hireDay = calendar.getTime();
    }

    public String getName()
    { 
    return name;
    }

    public double getSalary()
    { 
    return salary;
    }

    public Date getHireDay()
    { 
    return hireDay;
    }

    public void raiseSalary(double byPercent)
    { 
    double raise = salary * byPercent / 100;
    salary += raise;
    }
}

评:代码1 成员变量布局混乱,想到哪写到哪,很难整体把握类的功能。

二:实例域的类型
代码1:不良的写法
class Employee
{ 
    public String name;
    public double salary;
    public Date hireDay;
    public Employee(String n, double s, int year, int month, int day)
    { 
    name = n;
    salary = s;
    GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
    // GregorianCalendar uses 0 for January
    hireDay = calendar.getTime();
    }

    public String getName()
    { 
    return name;
    }

    public double getSalary()
    { 
    return salary;
    }

    public Date getHireDay()
    { 
    return hireDay;
    }

    public void raiseSalary(double byPercent)
    { 
    double raise = salary * byPercent / 100;
    salary += raise;
    }
}

代码2:良好的写法
class Employee
{ 
   private String name;
   private double salary;
   private Date hireDay;

   public Employee(String n, double s, int year, int month, int day)
   { 
      name = n;
      salary = s;
      GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
         // GregorianCalendar uses 0 for January
      hireDay = calendar.getTime();
   }

   public String getName()
   { 
      return name;
   }

   public double getSalary()
   { 
      return salary;
   }

   public Date getHireDay()
   { 
      return hireDay;
   }

   public void raiseSalary(double byPercent)
   { 
      double raise = salary * byPercent / 100;
      salary += raise;
   }

}

评:代码1 用public标记实例域,是一种极不提倡的做法。public 数据域允许程序的任何方法
    对其进行读取和修改。这就完全破坏了封装。

三:   
代码1:不好的写法
class Employee
{ 
   private String name;
   private double salary;
   private Date hireDay;

   public Employee(String n, double s, int year, int month, int day)
   { 
      name = n;
      salary = s;
      GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
         // GregorianCalendar uses 0 for January
      hireDay = calendar.getTime();
   }

   public String getName()
   { 
      return name;
   }

   public double getSalary()
   { 
      return salary;
   }

   public Date getHireDay()
   { 
      return hireDay;
   }

   public void raiseSalary(double byPercent)
   { 
      double raise = salary * byPercent / 100;
      salary += raise;
   }

代码2:良好的写法
class Employee
{ 
   private String name;
   private double salary;
   private Date hireDay;

   public Employee(String n, double s, int year, int month, int day)
   { 
      name = n;
      salary = s;
      GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
         // GregorianCalendar uses 0 for January
      hireDay = calendar.getTime();
   }

   public String getName()
   { 
      return name;
   }

   public double getSalary()
   { 
      return salary;
   }

   public Date getHireDay()
   { 
      return hireDay;
   }

   public void raiseSalary(double byPercent)
   { 
      double raise = this.salary * byPercent / 100;
      this.salary += raise;
   }
   
评:代码2 用隐式参数 this 将实例域与局部变量明显的区分开来了,便于代码的阅读。
   
四:静态方法与静态常量
代码1:不良的写法
class Employee
{ 
  ......
  private static int nextId = 1;

 
  public static int getNextId()
  {
      return nextId;
  } 
}
......
Employee harry = new Employee();
int n = harry.getNextId();

代码2:良好的写法
class Employee
{ 
  ......
  private static int nextId = 1;

 
  public static int getNextId()
  {
      return nextId;
  } 
}
......
int n = Employee.getNextId();
评:静态类成员不属于任何独立的类,它属于类。代码2 用harry.getNextId() 代替
    Employee.getNextId(), 这种方式很容易造成混淆,getNextId()方法计算的结果
    与 harry 毫无关系

   
五:默认域的初始化
代码1:不良的写法
class Employee
{

 
   private int id;
   private String name;
   private double salary;
   // the default constructor
   public Employee()
   {
    
   }
   ......
}

代码2:良好的写法
class Employee
{
   private int id;
   private String name;
   private double salary;
   // the default constructor
   public Employee()
   {
       name = "";
       id = 0;
       salary = 0;
   }
   ......
}
评:代码1 在构造器中没有显示的给域赋予明确的初值, 会影响代码的可读性。

六:参数名
代码1:不良的写法

public Employee(String n, double s)
{
    name = n;
    salary = s;
}

代码2:良好的写法
public Employee(String aName, double aSalary)
{
    name = aSalary;
    salary = aName;
}

评:代码1 参数名过短,只能通过阅读代码才能了解参数n和参数s的含义。代码2 给
    每个参数前面加了一个前缀"a", 这样就代码就很清晰了。每一个读者一眼就能够
    看懂参数的含义。

七:阻止继承
代码1:不好的写法
class Employee
{ 
    ......
   private String name;
   private double salary;
   private Date hireDay;

   public String getName()
   { 
      return name;
   }
}  

代码2:良好的写法
class Employee
{ 
   ......
   private String name;
   private double salary;
   private Date hireDay;

   public final String getName()
   { 
      return name;
   }
} 

评:代码2 将类声明为  final, 可以确保它们不会在子类中改变语义。防止多态的滥用。

八:将公共操作和域放在超类
代码1:不良的写法
abstract class Person
{ 
   ......
}

class Employee extends Person
{ 
   private String name;
   ......
}

代码2:良好的写法
abstract class Person
{ 
   private String name;
}

class Employee extends Person
{ 
   ......
}

评:应该尽可能的将公共操作和域放在超类中,这样就可以最大限度的节省代码量。

九:滥用继承
代码1:不良的写法
class Contractor extends Employee
{
    private double hourlyWage;
}


代码2:良好的写法
class  Employee
{
    ......
}

class Contractor
{
    private double hourlyWage;
}

评:除非所有继承的方法都有意义,否则不要使用继承

十:强制类型转换
代码1:不良的写法
class Employee
{ 
   public Employee(String n, double s, int year, int month, int day)
   { 
      name = n;
      salary = s;
      GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
      hireDay = calendar.getTime();
   }
   ......
}

class Manager extends Employee
{ 

 
   ......
   public void setBonus(double b)
   { 
      bonus = b;
   }

   private double bonus;
}

Employee p = (Manager)manager;
p.setBonus();
......

代码2:良好的写法
class Employee
{ 
   public void setBonus(double b)
   { 
      bonus = b;
   }
   ......
}

class Manager extends Employee
{ 

 
   ......
   private double bonus;
}

Employee p = new Employee();
p.setBonus();

评:如果超类对象需要调用子类对象的方法,那么就应该检查一下超类的设计。重新设计
    一下超类,添加相应的方法才是正确的选择



[ 本帖最后由 BlueGuy 于 2010-8-6 08:33 编辑 ]

我就是真命天子,顺我者生,逆我者死!
2010-06-09 21:03
快速回复:链表的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.070731 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved