| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1296 人关注过本帖
标题:C语言双向链表
只看楼主 加入收藏
tingting555
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2017-3-27
结帖率:0
收藏
已结贴  问题点数:20 回复次数:13 
C语言双向链表
    while(strcmp(head->nodeData.studentID,id)<0&&head->next!=NULL)
        head=head->next;
        if(strcmp(head->nodeData.studentID,id)==0)
为什么进不去这个循环
2017-03-27 12:14
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:5 
那肯定是条件未满足
2017-03-27 13:50
tingting555
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2017-3-27
收藏
得分:0 
回复 2楼 yangfrancis
LINK *DeleteNode(LINK *head)  //删除某一节    !!!!!!!!!!!!!!!!!!!!!!1
{
    char ID[20];
    LINK *p=head;
    fflush(stdin);
    printf("请输入所要删除学生的学号");
       gets(ID);
   while(p->next!=NULL&&strcmp(p->nodeData.studentID,ID)!=0)
   p=p->next;
       if(strcmp(ID,p->nodeData.studentID)==0)
       {
   if(p==head)
        {
            head=head->next;
            head->last=NULL;
        }
    else if(p->next==NULL)
                p->last->next=NULL;
    else
        {
            p->last->next=p->next;
            p->next->last=p->last;
        }
            free(p);
            printf("删除成功") ;
            }     
  writefile(head);
  return head;
}
2017-03-27 19:02
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
收藏
得分:5 
嗯哼,当我什么都没说。

[此贴子已经被作者于2017-3-27 19:38编辑过]


09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
2017-03-27 19:06
tingting555
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2017-3-27
收藏
得分:0 
回复 4楼 renkejun1942
我其他功能的while(head!=NULL)可以用;
这里我用p指向了head,就用不了,是因为head为空?
为什么我其他功能又可以用
2017-03-27 19:26
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:5 
回复 5楼 tingting555
代码量不是特别大的话  最好贴出全码来

断章取义 有些事就说不准

DO IT YOURSELF !
2017-03-27 19:28
tingting555
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2017-3-27
收藏
得分:0 
回复 6楼 wp231957
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define num 10  //学生人数
typedef struct date  // 日期结构体
 {
 short year;
 unsigned short month;
 unsigned short day;
 }DATE;
typedef struct fenshu
 {
 int chinese;
 int math;
 int C;
 int english;
 }FEN;
typedef struct student // 学生信息结构体
 {
 char name[10];
 char studentID[20]; // 学号
 unsigned short gender; // 性别
 DATE birthday;
 char address[100];
 FEN Score; // 4门课程分数
}STUDENT;
typedef struct link  // 双向链表结构体
 {
 STUDENT nodeData; // 数据
 struct link *last;  // 父节点指针
 struct link *next;  // 子节点指针
 }LINK;
void deletememory(LINK *head)    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
{
    LINK *p=NULL;
    while(head!=NULL)
    {
        p=head->next;
        free(head);
        head=p;
    }
}
 void change(STUDENT *p1,STUDENT *p2)
{
    int i;
    strcpy(p1->name,p2->name);
    strcpy(p1->studentID,p2->studentID);
    p1->gender=p2->gender;
    p1->birthday.year=p2->birthday.year;
    p1->birthday.month=p2->birthday.month;
    p1->birthday.day=p2->birthday.day;
    strcpy(p1->address,p2->address);
    p1->Score.C=p2->Score.C;
    p1->Score.chinese=p2->Score.chinese;
    p1->Score.english=p2->Score.english;
    p1->Score.math=p2->Score.math;
}
 void exchange(STUDENT *p1,STUDENT *p2)            // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
{
    STUDENT n;
    change(&n,p1);
    change(p1,p2);
    change(p2,&n);
}
void writefile(LINK *head)        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
{
    int i;
    FILE *p;
    if ((p = fopen("neww.txt","a+")) == NULL)                       //
    {
        printf("写数据文件失败。");
        exit(0);
    }
    while(head!=NULL)   
    {
        fwrite(&(head->nodeData),sizeof(STUDENT),1,p);
        head=head->next;
    }
    fclose(p);
    printf("写数据文件成功");
    printf("\n");
    getch();
}
LINK *AppendNode(LINK *head, STUDENT *b) //b指向的数据作为一个节点数据加到双向链表尾
{
    int i;
    LINK *s = NULL,*p=head;
    s = (struct link*)malloc(sizeof(struct link));  //新建节点
    if(s == NULL)
 {
    printf("No enough memory to allocate\n");
    exit(0);
 }
    if(head == NULL)
 {
    head=s;
    head->last=NULL;
 }
    else
 {
    while(p->next!=NULL)
    p=p->next;
    p->next=s;
    s->last=p;
}
    strcpy(s->nodeData.name,b->name);
    strcpy(s->nodeData.studentID,b->studentID);
    s->nodeData.gender=b->gender;
    s->nodeData.birthday.year=b->birthday.year;
    s->nodeData.birthday.month=b->birthday.month;
    s->nodeData.birthday.day=b->birthday.day;
    strcpy(s->nodeData.address,b->address);
        s->nodeData.Score.C=b->Score.C;
        s->nodeData.Score.chinese=b->Score.chinese;
        s->nodeData.Score.english=b->Score.english;
        s->nodeData.Score.math=b->Score.math;
    s->next=NULL;
    return head;
}
LINK *sort(LINK *head) //节点排序
{
   LINK *p;
   while(head!=NULL)
   {
       p=head->next;
       while(p!=NULL)
       {
   if(strcmp(head->nodeData.name,p->nodeData.name)>0)     //按学号排序
 {
    exchange(&(head->nodeData),&(p->nodeData));
 }
   p=p->next;
}
   head=head->next;
}
  return head;
}
LINK *input(LINK *head)   //输入
{
    STUDENT b;
    char ch='y';
    while(ch=='y'||ch=='Y')
    {
        fflush(stdin);
   printf("输入姓名\n");
   gets(b.name) ;
   printf("输入学号\n");
   gets(b.studentID);
   printf("输入地址\n");
    gets(b.address);
   printf("输入出生年月日\n");
   scanf("%d%d%d",&b.birthday.year,&b.birthday.month,&b.birthday.day);
   printf("输入Chinese,math,C,english成绩\n");
   fflush(stdin);
   scanf("%d%d%d%d",&b.Score.chinese,&b.Score.math,&b.Score.C,&b.Score.english);
   head=AppendNode(head,&b);
   printf("\n 继续输入学生信息?(y/n)");
    ch=getch();
    printf("\n");
}
   writefile(head);
   return head;
}
LINK *DeleteNode(LINK *head)  //删除某一节    !!!!!!!!!!!!!!!!!!!!!!1
{
    char name[20];
    LINK *p=head;
    fflush(stdin);
    printf("请输入所要删除学生的");
       gets(name);
   while(p->next!=NULL&&strcmp(p->nodeData.name,name)!=0)
   p=p->next;
       if(strcmp(name,p->nodeData.name)==0)
       {
   if(p==head)
        {
            head=head->next;
            head->last=NULL;
        }
    else if(p->next==NULL)
                p->last->next=NULL;
    else
        {
            p->last->next=p->next;
            p->next->last=p->last;
        }
            free(p);
            printf("删除成功") ;
            }     
  writefile(head);
  return head;
}
void search(LINK *head)       //查找学生信息    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
{
    char name[30];
    fflush(stdin);
    printf("输入所查询学生的姓名");
    gets(name);
    while(head->next!=NULL&&strcmp(head->nodeData.name,name)!=0)
    head=head->next;
        if(strcmp(head->nodeData.name,name)==0)
        {
        printf("%s",head->nodeData.name);
            printf("%s",head->nodeData.studentID);
            printf("%s,%s,%d\n",head->nodeData.name,head->nodeData.studentID,head->nodeData.gender);
            printf("%d,%d,%d,%d\n",head->nodeData.Score.C,head->nodeData.Score.chinese,head->nodeData.Score.english,head->nodeData.Score.math);
            printf("%d,%d,%d\n",head->nodeData.birthday.day,head->nodeData.birthday.month,head->nodeData.birthday.year);
            printf("%s\n",head->nodeData.address);
        }
        else
        printf("无此人");
}
LINK *reviseinput(LINK *head)         //修改信息的输入 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
{
    LINK *p=head;
    char id[20],con;
    fflush(stdin);
    printf("输入学生id");
    gets(id);
    while(strcmp(head->nodeData.studentID,id)<0&&head->next!=NULL)
    head=head->next;
    if(strcmp(head->nodeData.studentID,id)==0)
    {
        printf("%s,%s",head->nodeData.studentID,head->nodeData.name);
    }
        printf("是否修改此人信息(y/n)");
        con=getch();
    if(con=='y')
    {
            printf("输入姓名");
            gets(head->nodeData.name);
            fflush(stdin);
            printf("输入四门分数C,chinese,english,math");
            scanf("%d%d%d%d",&head->nodeData.Score.C,&head->nodeData.Score.chinese,&head->nodeData.Score.english,&head->nodeData.Score.math);
    }
        writefile(p);
        return head;
}
int INPUT()    //主界面  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
{
   int n;
   char ch='8';
   while(ch<'0'||ch>'7')
   {
   printf(" 1查询成绩\n");
   printf(" 2输入\n");
   printf(" 3删除\n");
   printf(" 4修改\n");
   printf(" 5排序\n");
   printf (" 6统计\n");
   printf("0退出系统\n");
   ch=getch();
   }
   n=(int)(ch-'0');
   return n;
}
LINK *count(LINK *head)        //统计  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
{
   LINK *b;
   double average=0,pass=0,i=0,j=0,k=0,l=0,n=0;    //及格率,方差 ijkl为统计人数
   int m=0,o=0,q,p;   
   double countc=0,countmath=0,countchinese=0,countenglish=0,nn=0;
   double change=0;
   char NAME[num][20];
   double score[num][4],countadd[num];
   double temp;
   char swap[1][20];
   for(b=head;b!=NULL;b=b->next)    //计算每门成绩平均分 ,一个人四科总分
   {
       countc=countc+b->nodeData.Score.C;
       countmath=countmath+b->nodeData.Score.math;
       countchinese=countchinese+b->nodeData.Score.chinese;
       countenglish=countenglish+b->nodeData.Score.english;
       countadd[o]=b->nodeData.Score.C+b->nodeData.Score.chinese+b->nodeData.Score.english+b->nodeData.Score.math;     
       if(b->nodeData.Score.C>60)
       i++;
       if(b->nodeData.Score.math>60)
       j++;
       if(b->nodeData.Score.chinese>60)
       k++;
       if(b->nodeData.Score.english>60)
       l++;
       score[m][0]=b->nodeData.Score.C;
       score[m][1]=b->nodeData.Score.chinese;
       score[m][2]=b->nodeData.Score.english;
       score[m][3]=b->nodeData.Score.math;
       strcpy(NAME[m],b->nodeData.name);
    m++;
       o++;
       n++;
       nn++;
   }
   printf("%lf,%lf,%lf,%lf\n",i/nn,j/nn,k/nn,l/nn);
   for(p=0;p<nn-1;p++)     //总分
   {
   for(q=p+1;q<nn;q++)
   {
       if(countadd[q]>countadd[p])
       {
           change=countadd[q];
           countadd[p]=countadd[q];
           countadd[q]=change;
           strcpy(swap[1],NAME[q]);
           strcpy(NAME[q],NAME[p]);
           strcpy(NAME[p],swap[1]);
       }
   }
}
  for(p=0;p<nn;p++)
  {
       printf("总分第%d名为",(p+1));
       puts(NAME[p]);
   }
      for(p=0;i<nn;i++)     //C分
   for(q=1;j<nn;j++)
   {
       if(score[q][0]>score[p][0])
       {
           temp=score[q][0];
           score[p][0]=score[q][0];
           score[q][0]=temp;
           strcpy(swap[1],NAME[q]);
           strcpy(NAME[q],NAME[p]);
           strcpy(NAME[p],swap[1]);
       }
   }
   for(p=0;p<nn;p++)
   {
   printf("C第%d名为",(p+1));
   puts(NAME[p]) ;
}
 for(p=0;i<nn;i++)     //C分
   for(q=1;j<nn;j++)
   {
       if(score[q][1]>score[p][1])
       {
           temp=score[q][1];
           score[p][1]=score[q][1];
           score[q][1]=temp;
           strcpy(swap[1],NAME[q]);
           strcpy(NAME[q],NAME[p]);
           strcpy(NAME[p],swap[1]);
       }
   }
   for(p=0;p<nn;p++)
   {
   printf("语文第%d名为",(p+1));
   puts(NAME[p]) ;
}
 for(p=0;i<nn;i++)     //C分
   for(q=1;j<nn;j++)
   {
       if(score[q][2]>score[p][2])
       {
           temp=score[q][2];
           score[p][2]=score[q][2];
           score[q][2]=temp;
           strcpy(swap[1],NAME[q]);
           strcpy(NAME[q],NAME[p]);
           strcpy(NAME[p],swap[1]);
       }
   }
   for(p=0;p<nn;p++)
   {
   printf("英语第%d名为",(p+1));
   puts(NAME[p]) ;
}
 for(p=0;i<nn;i++)     //C分
   for(q=1;j<nn;j++)
   {
       if(score[q][3]>score[p][3])
       {
           temp=score[q][3];
           score[p][3]=score[q][3];
           score[q][3]=temp;
           strcpy(swap[1],NAME[q]);
           strcpy(NAME[q],NAME[p]);
           strcpy(NAME[p],swap[1]);
       }
   }
   for(p=0;p<nn;p++)
   {
   printf("数学第%d名为",(p+1));
   puts(NAME[p]) ;
}
   getch();
    return head;
}
LINK *ReadFile()   //读文件  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1       ~~~~~~~~~~~~~~~~~~~~~~~~
{
    int i;
    FILE *p;
    LINK *head=NULL;
    STUDENT s;
    p = fopen("neww。txt","r");
    if (p == NULL)      //
    {
        printf("读数据文件失败");
        return head;
    }
    fgetc(p);
    if(feof(p))
    return head;
    rewind(p);
    while(!feof(p))   
    {
        fread(&s,sizeof(STUDENT),1,p);
        head=AppendNode(head,&s);
    }
    fclose(p);
    printf("读取数据文件成功");
    while(head->next!=NULL)
        head=head->next;
    head=head->last;
    deletememory(head->next);
    head->next=NULL;
    while(head->last!=NULL)
        head=head->last;
    return head;                           
}
void sortname(LINK *head)     //排序功能  名字
{
    LINK *p;
    while(head!=NULL)
    {
        p=head->next;
        while(p!=NULL)
        {
                if(strcmp(head->nodeData.name,p->nodeData.name)>0)
                    exchange(&(head->nodeData),&(p->nodeData));
            p=p->next;
        }
        head=head->next;
    }
}
void sortid(LINK *head)     //排序功能  学号
{
    LINK *p;
    while(head!=NULL)
    {
        p=head->next;
        while(p!=NULL)
        {
            if(strcmp(head->nodeData.studentID,p->nodeData.studentID)>0)
            exchange(&(head->nodeData),&(p->nodeData));
            p=p->next;
        }
        head=head->next;
    }
}
void DisplayLink(LINK *head)
{
    if(head==NULL)
    {
        printf("没有记录!按任意键继续\n");
    }
    while(head!=NULL)
    {
    printf("名字%s\n",head->nodeData.name);
    printf("学号%s\n", head->nodeData.studentID);
    printf("性别%d\n",head->nodeData.gender);
    printf("出生年%d,月%d,日%d\n", head->nodeData.birthday.year, head->nodeData.birthday.month,head->nodeData.birthday.day);
    printf("地址%s\n", head->nodeData.address);
    printf("成绩C%d    语文%d    英语%d    数学%d",head->nodeData.Score.C,head->nodeData.Score.chinese,head->nodeData.Score.english,head->nodeData.Score.math);
    printf("\n");
        head=head->next;
    }
    printf("已经显示完全部记录");
    getch();
}
int check()
{
    char mi[7]={};
    int i;
    char pass[10];
    printf("输入6位密码:");
    for(i=0;i<6;i++)
    {
        pass[i]=getch();
    }
    pass[6]='\0';
    if(strcmp(pass,mi)!=0)
    {
        printf(" 密码错误 ");
        getch();
        return 0;
    }
    printf("\n\n\t 密码正确");
    getch();
    return 1;
}
void main()   // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
{   
   int i=1,n;
   char ch,chch,*q=NULL;
   LINK *head = NULL;
  // n=check();
   //if(n==1)
   head=ReadFile();
   while(i!=0)
   {
       i=INPUT();
       switch(i)
       {
          case 1:
               printf("查询成绩");
               search(head);
               
               break;
           case 2:
               printf("输入信息\n");
               head=input(head);
               break;
           case 3:
               printf("删除");
               head=DeleteNode(head);
               break;
           case 4:
               printf("修改");
               reviseinput(head);
               break;
           case 5:
               printf("排序");
               printf("按姓名排序输入1.按学号排序输入2") ;
               printf("\n");
               ch=getch();
               if(ch==1)
               sortname(head);
               else
               sortid(head);
               break;
          case 6:
           printf("统计");
           head=count(head);
           break;
        case 0:
        printf("是否退出(y/n)");
        chch=getch();
        if(chch=='y')
        i=0;
        else
        i=1;
    }
   }
   sort(head);
   DisplayLink(head);
   //deletememory(head);
}
现在读文件不行,是怎么了,一读文件,直接说失败~~
txt和程序都是在同一个文件里的


2017-03-28 20:13
marlow
Rank: 6Rank: 6
等 级:侠之大者
威 望:2
帖 子:125
专家分:419
注 册:2016-7-18
收藏
得分:5 
    p = fopen("neww。txt","r");
    是一点,不是句号吧

一切都在学习、尝试、摸索中
2017-03-28 22:53
tingting555
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2017-3-27
收藏
得分:0 
回复 8楼 marlow
是的,已经改了
2017-03-29 13:00
tingting555
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2017-3-27
收藏
得分:0 
回复 8楼 marlow
LINK *input(LINK *head)&nbsp;&nbsp;&nbsp;//输入
{
&nbsp;&nbsp;&nbsp; STUDENT b;
&nbsp;&nbsp;&nbsp; char ch='y';
&nbsp;&nbsp;&nbsp; while(ch=='y'||ch=='Y')
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fflush(stdin);
&nbsp;&nbsp;&nbsp;printf("输入姓名\n");
&nbsp;&nbsp;&nbsp;gets(b.name) ;
&nbsp;&nbsp;&nbsp;printf("输入学号\n");
&nbsp;&nbsp;&nbsp;gets(b.studentID);
&nbsp;&nbsp;&nbsp;printf("输入地址\n");
&nbsp;&nbsp;&nbsp; gets(b.address);
&nbsp;&nbsp;&nbsp;printf("输入出生年月日\n");
&nbsp;&nbsp;&nbsp;scanf("%d%d%d",&b.birthday.year,&b.birthday.month,&b.birthday.day);
&nbsp;&nbsp;&nbsp;printf("输入Chinese,math,C,english成绩\n");
&nbsp;&nbsp;&nbsp;fflush(stdin);
&nbsp;&nbsp;&nbsp;scanf("%d%d%d%d",&b.Score.chinese,&b.Score.math,&b.Score.C,&b.Score.english);
&nbsp;&nbsp;&nbsp;head=AppendNode(head,&b);
&nbsp;&nbsp;&nbsp;printf("\n 继续输入学生信息?(y/n)");
&nbsp;&nbsp;&nbsp; ch=getch();
&nbsp;&nbsp;&nbsp; printf("\n");
但是我这里,可以输入地址,但我输出不了~~~printf("%s",b.address);为啥
2017-03-29 13:02
快速回复:C语言双向链表
数据加载中...
 
   



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

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