链表创建的源程序
linklist.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "staff.h"
#include "linklist.h"
node *Createlinklist()
{
node *head, *p;
head = (node *)malloc(sizeof(node));
head->next = NULL;
staff a[100] = {{"11111", "mmm", "f", 12000, 2000, 14000},
{"22222", "aaa", "m", 13000, 3000, 16000},
{"33333", "sss", "f", 15000, 3000, 18000},
{"44444", "fff", "m", 17000, 8000, 25000},
{"55555", "ggg", "f", 20000, 5000, 25000}};
for(int i = 0; i<5; i++)
{
p = (node *)malloc(sizeof(node));
p->Staff = a[i];
p->next = head->next;
head->next = p;
}
return head;
}
void Displaylinklist(node *head)
{
linklist p;
p = head->next;
while(p!=NULL)
{
Displaystaff(p->Staff);
p = p->next;
}
}
node *searchnode(node *head, char ID[])
{
linklist p;
p = head;
while(p!=NULL&&strcmp(p->next->Staff.ID, ID)!=0)
{
p = p->next;
}
return p->next;
}
void searchnodebyname(node *head, char name[])
{
linklist p;
p = head;
while((p!=NULL)&&(strcmp((p->next)->Staff.name, name)!=0))
{
p = p->next;
}
printf("-----´ËÈËΪ---------n");
printf("%s", p->next->Staff.name);
printf("n");
}
void delenode(linklist head, char ID[])
{
linklist p;
p = head;
while(p->next&&(strcmp(p->next->Staff.ID, ID)!=0))
{
p = p->next;
}
if(p->next)
{
p->next = p->next->next;
}
else
{
printf("=====NO FOUND========n");
}
}
void insertnode(linklist head, staff Staff)
{
linklist p;
p = (node *)malloc(sizeof(node));
p->Staff = Staff;
p->next = head->next;
head->next = p;
}
void distroylinklist(linklist head)
{
linklist p;
p = head;
while(p!=NULL)
{
p = p->next;
free(p);
}
}
3.文件存盘
file.h
#ifndef FILE_H_INCLUDED
#define FILE_H_INCLUDED
#include "linklist.h"
#include "staff.h"
//职工信息存盘
void saveinformation(linklist head );
//职工信息加载
void loadinformation(linklist head );
#endif // FILE_H_INCLUDED
file.cpp
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "file.h"
#include "linklist.h"
#include "staff.h"
void saveinformation(linklist h )
{
FILE *fp;
linklist p;
if ( (fp = fopen("stu.txt","w") ) == NULL)
{
printf("Failure to open stu.txt!n");
exit(0);
}
for ( p = h->next; p; p=p->next )
{
fwrite( &(p->Staff), sizeof(node), 1, fp);
}
fclose(fp);
}
void loadinformation( linklist h )
{
FILE *fp;
staff nodeBuffer;
if ((fp = fopen("stu.txt","r")) == NULL)
{
printf("nt数据文件丢失或为首次运行, 将加载测试数据n");
return ;
}
while( fread(&nodeBuffer, sizeof(node), 1, fp)!=0 )
{
insertnode(h, nodeBuffer);
}
}










