| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1333 人关注过本帖
标题:建立一个链表,每个结点包含:取工号,工资和指针,要求编程完成以下:
只看楼主 加入收藏
王冥刈
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-5-23
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
建立一个链表,每个结点包含:取工号,工资和指针,要求编程完成以下:
建立一个链表,每个结点包含:取工号,工资和指针,要求编程完成以下:
1、从键盘输入各结点的数据,按职工号的顺序建立链表,然后输出各结点的数据。
2、插入一个职工的结点数据,按工号的顺序播放到链表中去。
3、从上建立链表中删除一个指定职工号的特点。
 
搜索更多相关主题的帖子: 链表 工资 结点 工号 指针 
2010-05-23 17:06
Dzhfield
Rank: 2
等 级:论坛游民
帖 子:12
专家分:37
注 册:2010-5-19
收藏
得分:10 
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define  LENGTH  12
#define  OK  1
#define  ERROR  0
#define  Null  0

struct Node     /*定义职工信息的结点*/
{
    char jobnum[LENGTH];
    long  wages;
    struct Node *next;
};
typedef struct Node staff;
typedef struct Node *StInfor;

int InitLinklist(StInfor List,int num)     /*初始化链表,默认所有信息为空*/
{
    StInfor peop,p=List;     /*peop:指向新结点的指针。p:指向当前结点的指针。*/
    while(num--)
    {
        peop=(staff *)malloc(sizeof(staff));
        p->next=peop;
        p=peop;        
    }
    p->next=Null;
   
    printf("\nThe InitLinklist operation is OK.\n");
}

int InputInfor(StInfor List,int num)      /*向链表中输入信息*/
{
    StInfor p=List->next;    /*p:指向当前结点的指针。*/
    int i=1;
   
    printf("\nThe number of the staff is %d.\n",num);
    while(num--)
    {
        printf("\nPlease enter the %d staff's information:",i);
        printf("\nEnter the staff's jobnumber:");
        fflush(stdin);
        scanf("%s",p->jobnum);
        printf("\nEnter the staff's wages:");
        scanf("%ld",&p->wages);
        p=p->next;
        i++;
    }
    printf("\nThe InputInfor operation is OK.\n");
}

int printfLinklist(StInfor List)     /*输出链表*/
{
    StInfor p=List->next;
    printf("\nThe information of the list are:\n");
    printf("\n staff's number \tstaff's wages");
    while(p)
    {
        printf("\n %-14s \t%12ld",p->jobnum,p->wages);
        p=p->next;
    }
}

int InsertLinklist(StInfor List)      /*向链表中插入结点*/
{
    StInfor peop,q=List,p=q->next;   
    /*peop:指向待插入的新结点的指针。q:指向新节点的前驱结点的指针。p:指向新节点的后继结点的指针。*/
   
    printf("\nPlease enter the staff's information which you want to insert.");
    peop=(staff *)malloc(sizeof(staff));
    printf("\nEnter the staff's jobnumber:");
    fflush(stdin);
    scanf("%s",peop->jobnum);
    printf("\nEnter the staff's wages:");
    scanf("%ld",&peop->wages);
   
    while(strcmp(p->jobnum,peop->jobnum)!=1)
    {
        q=p;
        p=p->next;
        if(!p)
        {
            peop->next=q->next;
            q->next=peop;
            return OK;
        }
    }
    peop->next=q->next;
    q->next=peop;
   
    printf("\nThe InsertLinklist operation is OK.\n");   
}

int deleteLinklist(StInfor List)     /*删除链表中相应的职工信息*/
{
    char jobnum[LENGTH];    /*jobnum:待删职工的工号。*/   
    StInfor q=List,p=q->next;    /*q:指向待删职工结点的前驱结点的指针。p:指向待删职工结点的指针。*/
   
    printf("\nPlease enter the staff's information which you want to delete.");
    fflush(stdin);
    scanf("%s",jobnum);
   
    while(strcmp(p->jobnum,jobnum))
    {
        q=p;
        p=p->next;
        if(!p)
        {
            printf("\nDoesn't find the staff.\n");
            return ERROR;
        }
    }
    q->next=p->next;
    free(p);
   
    printf("\nThe InsertLinklist operation is OK.\n");
}

int main()
{
    StInfor List;    /*List:定义链表*/
    int num;      /*num:链表中的职工数目*/
    char conti;     /*conti:是否继续进行操作*/
   
    printf("\nplease enter the total number of the staff:");
    scanf("%d",&num);
    InitLinklist(List,num);    /*初始化链表*/
    InputInfor(List,num);     /*向链表输入信息*/
   
    do{
        printfLinklist(List);    /*输出链表*/
        InsertLinklist(List);    /*插入职工*/        
        deleteLinklist(List);    /*删除职工*/
        
        printf("\nContinue,enter 'Y/y';or enter others.\n");    /*确定是否继续进行操作*/
        fflush(stdin);
        conti=getchar();        
    }while(conti=='Y'||conti=='y');
}

  希望对你能有帮助
2010-05-25 21:36
委琐的C
Rank: 2
等 级:论坛游民
帖 子:3
专家分:10
注 册:2010-5-26
收藏
得分:10 
菜鸟膜拜。。
2010-05-26 09:17
快速回复:建立一个链表,每个结点包含:取工号,工资和指针,要求编程完成以下: ...
数据加载中...
 
   



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

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