链表文件结构体操作例子(员工信息管理系统)

员工信息结构体如下:

 
  
typedef struct employee
{
int num;
float salary;
struct employee * next;
}EMP;

 

 

 本程序采用菜单进行控制,包含以下功能:

1、链表的创建

2、链表的插入(有序链表的插入)

3、链表的插入(按指定位置插入)

4、链表删除(按制定员工号删除)

5、从文件读入链表内容

6、保存链表内容到文件

7、显示链表内容

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
#include < stdio.h >
#include
< stdlib.h >
#include
< conio.h >
#include
< ctype.h >
#include
< windows.h >
#define LEN sizeof(struct employee)

typedef
struct employee
{
int num;
float salary;
struct employee * next;
}EMP;

int n;

/* 插入表尾 */
EMP
* CreateEnd()
{
EMP
* p, * last, * head;
n
= 0 ;
head
= last = NULL;
p
= (EMP * )malloc(LEN); /* 创建第一个结点 */
printf(
" 员工号\t工资\n " );
scanf(
" %d%f " , & p -> num, & p -> salary);
p
-> next = NULL;
while (p -> num != 0 ) /* 将结点加入链表 */
{
++ n;
if (n == 1 ) /* 是第一个结点,作表头 */
head
= p;
else /* 不是第一个结点,作表尾 */
last
-> next = p;
last
= p;
p
= (EMP * )malloc(LEN); /* 开辟下一个结点 */
scanf(
" %d%f " , & p -> num, & p -> salary);
p
-> next = NULL;
}
free(p);
/* 释放最后一个结点所占的内存 */
return head; /* 返回链表的头指针 */
}

/* 插入表头 */
EMP
* CreateStart()
{
EMP
* head, * p;
n
= 0 ;
head
= NULL;
p
= (EMP * )malloc(LEN);
printf(
" 员工号\t工资\n " );
scanf(
" %d%f " , & p -> num, & p -> salary);
p
-> next = head;
while (p -> num != 0 )
{
++ n;
head
= p;
p
= (EMP * )malloc(LEN);
scanf(
" %d%f " , & p -> num, & p -> salary);
p
-> next = head;
}
free(p);
return head;
}

/* 链表显示 */
void list(EMP * head)
{
EMP
* p;
printf(
" \n%d个职工信息如下:\n " ,n);
printf(
" 员工号\t工资\n " );
p
= head;
if (head != NULL)
{
do
{
printf(
" %d\t%.2f\n " ,p -> num,p -> salary);
p
= p -> next;
}
while (p != NULL);
}
else
printf(
" 无员工信息记录!\n " );

}


EMP
* insert_sort(EMP * head) // 按照员工号从小到大顺序插入
{
EMP
* p0, * p1, * p2;
p1
= p2 = head; /* p1指向第一个结点 */

printf(
" 请输入要插入员工的信息:\n " );
p0
= (EMP * )malloc(LEN);
scanf(
" %d%f " , & p0 -> num, & p0 -> salary);
p0
-> next = NULL;

if (head == NULL) /* 原来是空表 */
{
head
= p0;p0 -> next = NULL; /* 使p0指向的结点作为链表第一个结点 */
}
else
{
while ((p0 -> num > p1 -> num) && (p1 -> next != NULL)) // 防止空节点进行员工号比较,因此条件为p1->next!=NULL,而不是p1!=NULL
{
p2
= p1;p1 = p1 -> next; /* 找插入点 */
}
if (head == p1) /* 如果只有一个头结点 */
{
if (p0 -> num > head -> num) // 如果待插入节点员工号大于头结点员工号
head -> next = p0; // 待插入节点插入到头结点之后
else // 如果待插入节点员工号小于头结点员工号
{
p0
-> next = head;head = p0; // 待插入节点插入到头结点之前
}
}
else
{
if (p0 -> num > p1 -> num) // 如果待插入节点员工号大于最后节点的员工号
{
p1
-> next = p0; // 待插入节点插入到最后
}
else // 找到插入位置
{
p2
-> next = p0;p0 -> next = p1; // 插入到p2,p1之间
}

}
}
++ n;
return head;
}

EMP
* insert_index(EMP * head) // 按指定位置插入员工信息
{
int i = 0 ,index;
EMP
* p0, * p1, * p2;
p1
= p2 = head;

printf(
" 请输入要插入员工的信息:\n " );
p0
= (EMP * )malloc(LEN);
scanf(
" %d%f " , & p0 -> num, & p0 -> salary);
p0
-> next = NULL;
printf(
" 请输入要插入的位置: " );
scanf(
" %d " , & index);

if (head == NULL) // 如果原来链表是空的,头结点指向新插入的结点
{
head
= p0;p0 -> next = NULL;
}
else
{
while (p1 != NULL && i < index - 1 ){
p2
= p1;p1 = p1 -> next; // 找插入点
i ++ ;
}
if (p1 == head) // 如果插入点是第一位
{
p0
-> next = head;head = p0;
}
else // 插入到指定的位置
{
p2
-> next = p0;p0 -> next = p1;
}
}

++ n;
return head;
}

EMP
* del(EMP * head)
{
EMP
* p1, * p2;
int num;

printf(
" 请输入要删除的员工编号:\n " );
scanf(
" %d " , & num);

if (head == NULL)
{
printf(
" \n链表为空!\n " ); /* 链表为空 */
goto end;
}

p1
= p2 = head; /* 从头结点开始查找 */
while (num != p1 -> num && p1 -> next != NULL) /* p1指向的不是所要找的结点,并且没有到表尾 */
{
p2
= p1;p1 = p1 -> next; /* 后移一个结点 */
}
if (num == p1 -> num) /* 找到需要删除的结点 */
{
if (p1 == head) /* p1指向的是头结点 */
head
= p1 -> next; /* 第二个结点成为新的头结点 */
else
p2
-> next = p1 -> next; /* 后继结点的地址赋给前一结点 */

printf(员工号为
% d的信息已经被删除\n " ,num);
free(p1);
n
-- ;
}
else
printf(
" 没有发现 %d 号员工的信息!\n " ,num);
end:
return head;
}

int writeInfo(EMP * head)
{
FILE
* fp;
EMP
* p;
p
= head;
if ((fp = fopen( " test.txt " , " w " )) == NULL)
{
printf(
" 打开文件失败! " );
exit(
0 );
}

while (p != NULL)
{
fprintf(fp,
" %d %.2f\n " ,p -> num,p -> salary);
p
= p -> next;
}
printf(
" 链表保存成功!\n " );
fclose(fp);
}

EMP
* readInfo()
{
FILE
* fp;
EMP
* p, * last, * head;
n
= 0 ;
head
= last = NULL;
if ((fp = fopen( " test.txt " , " r " )) == NULL)
{
printf(
" 打开文件失败! " );
exit(
0 );
}
p
= (EMP * )malloc(LEN);
while (fscanf(fp, " %d %f " , & p -> num, & p -> salary) != EOF)
{
p
-> next = NULL;
++ n;
if (n == 1 )
head
= p;
else
last
-> next = p;
last
= p;
p
= (EMP * )malloc(LEN);

}
free(p);
printf(
" 链表读入成功!\n " );
fclose(fp);
// 关闭文件
return head; /* 返回链表的头指针 */
}

void main()
{
EMP
* head, * emp;
int del_num,index;
char letter;

do
{
printf(
" ********************************\n " );
printf(
" A 创建链表\n " );
printf(
" B 插入员工信息(按员工号排序插入)\n " );
printf(
" C 插入员工信息(按指定位置插入)\n " );
printf(
" D 删除员工信息\n " );
printf(
" E 读入链表内容\n " );
printf(
" F 保存链表内容\n " );
printf(
" G 显示链表内容\n " );
printf(
" Q 退出\n " );
printf(
" ********************************\n " );
printf(
" 请根据编号选择功能: " );


scanf(
" %c " , & letter); // 注意:%c前面要加空格,用来吸收多余的回车符,否则会重复打印菜单
letter = toupper(letter);

switch (letter)
{
case ' A ' :
system(
" cls " );
head
= CreateEnd();
break ;
case ' B ' :
system(
" cls " );
head
= insert_sort(head,emp);
list(head);
break ;
case ' C ' :
system(
" cls " );
head
= insert_index(head);
list(head);
break ;
case ' D ' :
system(
" cls " );
head
= del(head);
list(head);
break ;
case ' E ' :
system(
" cls " );
head
= readInfo();
break ;
case ' F ' :
system(
" cls " );
writeInfo(head);
break ;
case ' G ' :
system(
" cls " );
list(head);
break ;
}
}
while (letter != ' Q ' );
}

 

 

 

 

转载于:https://www.cnblogs.com/qixin622/archive/2010/02/25/1673199.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值