| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2517 人关注过本帖, 1 人收藏
标题:我初学C语言,弄了一个简单的人员出列程序。请高人指点。如有人帖上链表解法 ...
只看楼主 加入收藏
josen0205
Rank: 2
来 自:江苏
等 级:论坛游民
帖 子:307
专家分:52
注 册:2007-5-8
收藏
得分:0 
回复 1# 的帖子
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

typedef struct Node{
    int data;
    struct Node *next;
}*LinkList;

int CreateList(LinkList &L,int m); /* 将m个人1到m编号依次插入链表L中 */
int GetNode(LinkList &L,int n); /* 依次删除输出链表中符合要求的节点 */

int CreateList(LinkList &L,int m)
{
    int i=1;
    LinkList t,p;
    L=(LinkList)malloc(sizeof(struct Node));    
    if(L==NULL)
    {
        printf("动态分配内存失败!\n");
        return -1;
    }
    p=L;
    for(i=1;i<=m;i++) /* 依次为节点分配内存,插入链表 */
    {
        t=(LinkList)malloc(sizeof(struct Node));
        if(t==NULL)
        {
            printf("动态分配内存失败!\n");
            return -1;
        }
        t->data=i;
        p->next=t;
        p=t;
    }
    p->next=L->next; /* 最后一个节点next指针指向第一个节点 */
    return 0;
}

int GetNode(LinkList &L,int n)
{
    LinkList p,s;
    int i=0,j=0;
    p=L->next;
    while(p->next!=p) /* 链表中只有一个节点时终止 */
    {
        i++;
        if(i==n-1) /* 找到报数n的节点,输出节点值并删除节点 */
        {
            j++;
            s=p->next;
            p->next=p->next->next;
            printf("%-6d",s->data);
            if(j%10==0)
                printf("\n");
            i=0;
            free(s);
        }
        p=p->next;
    }
    printf("%-6d",p->data);
    free(p);
    free(L);
    return 0;
}

int main()
{
    int m,n;
    LinkList L;

    printf("Enter the total number of the person[m]:"); /* 总人数 */
    scanf("%d",&m);
    printf("Enter the number of get out[n]:");
    scanf("%d",&n);
    CreateList(L,m);
    printf("The out queue is:\n");
    GetNode(L,n);
    getch();
    printf("\n");
    return 0;
}

只有想不到,没有做不到
2008-02-14 14:26
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
你写的比我规范。。呵呵,帮你顶一个,我的在网吧完成,被烟熏的头大啊。。。
2008-02-14 16:19
koman
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2007-9-12
收藏
得分:0 
//这是我学C时写的个东西 可能有BUG 我也懒得改  看看对你有没帮助
//动态链表创建排序插入删除
//实现语言C
//平台vc++6.0
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct score
{
int num;
char name[15];
float score_1;
float score_2;
float score_3;
float score_av;
};
struct student
{
struct score objscore;
struct student *next;
};
void paixu();
void chuanjian();
void shuchu();
void shanchu();
struct student *head,*pf,*pb;
int i=0;
void main()
{
char yn='y',ch=' ';
do
{
  system("cls");
  printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
  printf("┃动态链表实现排序插入删除:                   T-11 koman ┃\n");
  printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n");
     printf("┃ 请选择您的需要:                                      ┃\n");
  printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n");
  printf("┃1 、创建或加入学员资料            3、排序,输出       ┃\n");
  printf("┃2 、按学号删除学员                0 、退           ┃\n");     
  printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
  do{
   printf("┠请输入数字: ");
   fflush(stdin);
   scanf("%d",&ch);
   if(ch<0||ch>3)
    printf("┠输入错误!\n");
  }while(ch<0||ch>3);
  if(ch==0)
   break;
  if(ch==1)
  {
   chuanjian();
  }
  else if(ch==3)
  {
   paixu();
   shuchu();
  }
  else if(ch==2)
  {
   shanchu();
  }
  printf("┠■ 是否继续操作(Y/N)? ");
  fflush(stdin);
  scanf("%c",&yn);
}while(yn=='Y'||yn=='y');
}
void paixu()   //排序
{

struct student *p,*q;
struct score temp;
p=head;
while(p!=NULL)
{
  q=p->next;
  while(q!=NULL)
  {
   if(p->objscore.score_av < q->objscore.score_av)
   {
    temp=q->objscore;
    q->objscore=p->objscore;
    p->objscore=temp;
   }
   q=q->next;
  }  
  p=p->next;
}
}
void chuanjian() //创建||加入学员资料
{
char yn='y';
printf("┣请输入学员信息: \n");
do
{
  pb=(struct student*) malloc(sizeof(struct student));
  pb->objscore.num=0;
  pb->objscore.score_1=0;
        pb->objscore.score_2=0;
  pb->objscore.score_3=0;
  pb->objscore.score_av=0;
  printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
  printf("┣学号:");
  scanf("%d",&pb->objscore.num);
  printf("┣姓名:");
  fflush(stdin);
  gets(pb->objscore.name);
  printf("┣三门成绩:\n");
  printf("┣成绩1: ");
  scanf("%f",&pb->objscore.score_1);
  printf("┣成绩2: ");
  scanf("%f",&pb->objscore.score_2);
  printf("┣成绩3: ");
  scanf("%f",&pb->objscore.score_3);
  pb->objscore.score_av=(pb->objscore.score_3+pb->objscore.score_3+pb->objscore.score_3)/3;
  if(i==0)
  {
   pf=head=pb;
  }
  else
  {
   pf->next=pb;
  }
  pf=pb;
  pb->next=NULL;
  i++;
  printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
  printf("┣是否继续(y/n)?");
  fflush(stdin);
  yn=getchar();
}while(yn=='y'||yn=='Y');
}
void shuchu() //输出学员资料
{
struct student *p;
printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
printf("┣你输入的结果排序后输出:\n");
p=head;
if(i==1)
{
  printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
  printf("┣学号: %d\n",head->objscore.num);
  printf("┣姓名: %s\n",head->objscore.name);
  printf("┣三门成绩: \n");
  printf("┣成绩1: \t\t%.2f\n",head->objscore.score_1);
  printf("┣成绩2: \t\t%.2f\n",head->objscore.score_2);
  printf("┣成绩3: \t\t%.2f\n",head->objscore.score_3);
  printf("┣平均成绩: \t\t%.2f\n",head->objscore.score_av);
}
else
{
  while(p!=NULL)
  {
   printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
   printf("┣学号: %d\n",p->objscore.num);
   printf("┣姓名: %s\n",p->objscore.name);
   printf("┣三门成绩: \n");
   printf("┣成绩1: \t\t%.2f\n",p->objscore.score_1);
   printf("┣成绩2: \t\t%.2f\n",p->objscore.score_2);
   printf("┣成绩3: \t\t%.2f\n",p->objscore.score_3);
   printf("┣平均成绩: \t\t%.2f\n",p->objscore.score_av);
   p=p->next;
  }
  printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
}
}
void shanchu() //按学号删除学员
{
struct student *p,*q;
int stu_num;
printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
printf("┣请输入你要删除的学员学号: ");
scanf("%d",&stu_num);
q=p=head;
while(p!=NULL)
{
  if(stu_num==p->objscore.num)
  {
   if(p==head)
   {  
    head=p->next;  
   }
   else
   {
    q->next=p->next;  
   }   
  }
  q=p;
  p=p->next;
}
}
2008-02-16 00:43
linpinjin
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-12-30
收藏
得分:0 
终于弄清楚了链表解法,谢谢楼上各位朋友。
/*谢谢楼上诸位的回帖,现在我终于搞清楚了链表怎么解这个题,请高人们指点一下缺陷和不足*/

#include<stdio.h>

typedef struct men
 {int no;
  struct men *next;
 }list;

list *create(int n)                  /*初始化链表*/
{
 list *h,*t,*p;
 int i;
 h=(list*)malloc(sizeof(list));
 h->no=1;
 t=h;
 for(i=2;i<=n;i++)
  {p=(list*)malloc(sizeof(list));
   t->next=p;
   p->no=i;
   p->next=h;
   t=p;
  }
 return (h);
}

void main(void)
 {
  int i,j,k,s,x;
  list *h,*a;
  printf("请输入总人数:");
  scanf("%d",&s);
  printf("从第几人开始报数:");
  scanf("%d",&j);
  printf("报数到多少时该人出列:");
  scanf("%d",&k);

  h=create(s);

  for(i=1;i<j;i++)              /*从第几个人开始报数*/
  h=h->next;

  i=x=0;                        /*i保存报数值,x保存出列人数*/
  while(x!=s)                   /*结束循环的条件和数组解题办法一样,当出列人数为总人数时结束循环*/
   {i++;
    if(i+1==k)
/*这里(i+1=k)实际代表的是当报数到k-1时,后一个人出列,这样做是为了方便释放内存***
 *因为结构中只有指向下一个结点的指针域,如果你们有更好的办法,请帖出来,先谢谢了*/

     {printf("%4d",(h->next)->no);
      a=h->next;h->next=(h->next)->next;free(a);i=0;x++;
      if(x%10==0)printf("\n");
     }
    h=h->next;
   }
}
2008-02-20 04:24
快速回复:我初学C语言,弄了一个简单的人员出列程序。请高人指点。如有人帖上链 ...
数据加载中...
 
   



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

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