C语言学生链表实现代码
作者:互联网
//目标,一个基于win32控制台程序的单向链表C语言项目,暂无实现模块化
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
//student unit,create define for student discribe block;
typedef struct _STU
{
char arrStuNum[11];
char arrStuName[11];
int iStuScore;
struct _STU* pNext;
} STUNODE;
//add a students info
void addStuMSG(char* arrStuNum, char* arrStuName, int iStuScore);
//clear the linked list
void FreeData();
//show the linked list info
void ShowData();
//show menu
void Menu();
//head linked list add
void AddStuMSGToLinkhead(char* arrStuNum, char* arrStuName, int iStuScore);
//find students info Pointer function
STUNODE* FindStuByNum(char* arrStuNum);
//isnert STU info for user
void InsertNode(STUNODE* pTemp, char* arrStuNum, char* arrStuName, int iStuScore);
//delete students info
void DeleteStuNode(STUNODE* pNode);
//save students info to localfiel
void SaveStuToFile();
//read file from disk
void ReadStuFule();
//define global head and tail needle;
STUNODE* g_phead = NULL;
STUNODE* g_pEnd = NULL;
int main(void) {
char arrStuNum[11] = { 0 };
char arrStuName[11] = { 0 };
int iStuScore = -1;
int nOrder = -1; //user chose
STUNODE* pTemp = NULL;
int FLOG = 1;
Menu();
while (FLOG)
{
printf("useage function(use number 9):");
scanf("%d", &nOrder);
switch (nOrder)
{
case 1:
printf("input students number,name,score:\n");
printf("number:");
scanf("%s", arrStuNum);
printf("name:");
scanf("%s", arrStuName);
printf("score:");
scanf("%d", &iStuScore);
addStuMSG(arrStuNum, arrStuName, iStuScore);
break;
case 2:
printf("input student number : ");
scanf("%s", arrStuNum);
pTemp = FindStuByNum(arrStuNum);
if (NULL != pTemp)
{
printf("number:%s,name:%s,score:%d\n", pTemp->arrStuNum, pTemp->arrStuName, pTemp->iStuScore);
}
break;
case 3:
printf("input search students number:");
scanf("%s", arrStuNum);
pTemp = FindStuByNum(arrStuNum);
if (NULL != pTemp)
{
printf("input change number:");
scanf("%s", arrStuNum);
strcpy(pTemp->arrStuNum, arrStuNum);
printf("input change name:");
scanf("%s", arrStuName);
strcpy(pTemp->arrStuName, arrStuName);
printf("input change score:");
scanf("%d", &iStuScore);
pTemp->iStuScore = iStuScore;
}
break;
case 4:
SaveStuToFile();
break;
case 5:
ReadStuFule();
break;
case 6:
printf("input search students number to be delete:");
scanf("%s", arrStuNum);
pTemp = FindStuByNum(arrStuNum);
if (NULL != pTemp)
{
DeleteStuNode(pTemp);
}
break;
case 7:
FreeData();
g_phead = NULL;
g_pEnd = NULL;
ReadStuFule();
break;
case 8:
ShowData();
break;
case 9:
Menu();
break;
case 11:
printf("input head students number,name,score:\n");
printf("number:");
scanf("%s", arrStuNum);
printf("name:");
scanf("%s", arrStuName);
printf("score:");
scanf("%d", &iStuScore);
AddStuMSGToLinkhead(arrStuNum, arrStuName, iStuScore);
break;
case 111:
printf("input students number:\n");
printf("number:");
scanf("%s", arrStuNum);
pTemp = FindStuByNum(arrStuName);
if (NULL != pTemp)
{
//insert
printf("input insert students number,name,score:\n");
printf("number:");
scanf("%s", arrStuNum);
printf("name:");
scanf("%s", arrStuName);
printf("score:");
scanf("%d", &iStuScore);
InsertNode(pTemp, arrStuNum, arrStuName, iStuScore);
}
break;
case 0:
FLOG = 0;
break;
default:
printf("INPUT IS WRONG");
break;
}
}
FreeData();
system("pause");
return 0;
}
//add a student massage
void addStuMSG(char* arrStuNum, char* arrStuName, int iStuScore) {
//crate a new node;
STUNODE* pTemp = malloc(sizeof(STUNODE));
//check data is legal
if (NULL == arrStuNum || NULL == arrStuName || iStuScore < 0)
{
printf("student info is wrong\n");
return;
}
//copy data from user
strcpy(pTemp->arrStuName, arrStuName);
strcpy(pTemp->arrStuNum, arrStuNum);
pTemp->iStuScore = iStuScore;
pTemp->pNext = NULL;
//check exist and crate next node to be connect;
if (NULL == g_phead || NULL == g_pEnd)
{
g_phead = pTemp;
g_pEnd = pTemp;
}
else //move needlo to next struct;
{
g_pEnd->pNext = pTemp;
g_pEnd = pTemp;
}
}
//show info linked list
void ShowData()
{
STUNODE* pTemp = g_phead;
while (pTemp != NULL)
{
printf("number:%s,name:%s,scroe:%d\n", pTemp->arrStuNum, pTemp->arrStuName, pTemp->iStuScore);
pTemp = pTemp->pNext;
}
}
//free linked list mermory
void FreeData()
{
//record node
STUNODE* pTemp = g_phead;
while (g_phead != NULL)
{
pTemp = g_phead;
g_phead = g_phead->pNext;
//remove node
free(pTemp);
}
}
// add head link
void AddStuMSGToLinkhead(char* arrStuNum, char* arrStuName, int iStuScore)
{
STUNODE* pTemp = malloc(sizeof(STUNODE));
//mental error
if (NULL == arrStuNum || NULL == arrStuName || iStuScore < 0)
{
printf("wrong\n");
return;
}
//create a head link
strcpy(pTemp->arrStuNum, arrStuNum);
strcpy(pTemp->arrStuName, arrStuName);
pTemp->iStuScore = iStuScore;
pTemp->pNext = NULL;
if (NULL == g_phead || NULL == g_pEnd)
{
g_pEnd = pTemp;
}
else //move needlo to next struct;
{
pTemp->pNext = g_phead;
}
g_phead = pTemp;
}
//find students info Pointer function
STUNODE* FindStuByNum(char* arrStuNum)
{
STUNODE* pTemp = g_phead;
//Check the validity of parameters
if (NULL == arrStuNum)
{
printf("wrong!\n");
return NULL;
}
//is NULL
if (NULL == g_phead || NULL == g_pEnd)
{
printf("wrong, linked list is NULL\n");
return NULL;
}
//Traversing linked list
while (pTemp != NULL)
{
if (0 == strcmp(pTemp->arrStuNum, arrStuNum))
{
return pTemp;
}
pTemp = pTemp->pNext;
}
printf("no this info\n");
return NULL;
}
//isnert STU info for user
void InsertNode(STUNODE* pTemp, char* arrStuNum, char* arrStuName, int iStuScore)
{
STUNODE* pNewTemp = malloc(sizeof(STUNODE));
strcpy(pNewTemp->arrStuNum, arrStuNum);
strcpy(pNewTemp->arrStuName, arrStuName);
pNewTemp->iStuScore = iStuScore;
pNewTemp->pNext = NULL;
if (pTemp == g_pEnd)
{
g_pEnd->pNext = pNewTemp;
g_pEnd = pNewTemp;
}
else
{
pNewTemp->pNext = pTemp->pNext;
pTemp->pNext = pNewTemp;
}
}
//delete node
void DeleteStuNode(STUNODE* pNode)
{
//node = 1
if (g_phead = g_pEnd)
{
free(g_phead);
g_phead = NULL;
g_pEnd = NULL;
}
//node = 2
else if (g_phead->pNext == pNode)
{
if (g_phead == pNode)
{
free(g_phead);
g_phead = g_pEnd;
}
else
{
free(g_pEnd);
g_pEnd = g_phead;
g_phead->pNext = NULL;
}
}
//node > 3
else
{
STUNODE* pTemp = g_phead;
//is head ?
if (g_phead == pNode)
{
pTemp = g_phead;
g_phead = g_phead->pNext;
free(g_phead);
pTemp = NULL;
return;
}
while (pTemp)
{
if (pTemp->pNext == pNode)
{
//delete is end ?
if (pNode == g_pEnd)
{
free(pNode);
pNode = NULL;
g_pEnd = pTemp;
g_pEnd->pNext = NULL;
return;
}
//delete middle
else
{
STUNODE* p = pTemp->pNext;
//connect
pTemp->pNext = pNode->pNext->pNext;
free(p);
return;
}
}
pTemp = pTemp->pNext;
}
}
}
//save students info to localfiel
void SaveStuToFile()
{
FILE* pFile = g_phead;
STUNODE* pTemp = g_phead;
char strBuf[30] = { 0 };
char strScore[10] = { 0 };
if (NULL == g_phead)
{
printf("null");
return;
}
//save funtions
pFile = fopen("data.txt", "wb+");
if (NULL == pFile)
{
printf("open file fail\n");
return;
}
while (pTemp)
{
strcpy(strBuf, pTemp->arrStuNum);
strcat(strBuf, ".");
strcat(strBuf, pTemp->arrStuName);
strcat(strBuf, ".");
itoa(pTemp->iStuScore, strScore, 10);
strcat(strBuf, strScore);
fwrite(strBuf, 1, strlen(strBuf), pFile);
fwrite("\r\n", 1, strlen(strBuf), pFile);
pTemp = pTemp->pNext;
}
fclose(pFile);
}
//read file to disk
void ReadStuFule()
{
FILE* pfile = fopen("data.txt", "rb+");
if (NULL == pfile)
{
printf("null");
return;
}
char strStuNUM[10] = { 0 };
char strStuName[10] = { 0 };
char strScore[10] = { 0 };
int nCount = 0;
int j = 0;
char strBuf[30] = { 0 };
while (0 == feof(pfile))
{
int i = 0;
nCount = 0;
j = 0;
fgets(strBuf, 30, pfile);
for (i = 0; strBuf[i] != '\r'; i++)
{
if (0 == nCount)
{
strStuNUM[i] = strBuf[i];
if ('.' == strBuf[i])
{
nCount++;
}
}
else if (1 == nCount)
{
strStuName[j] = strBuf[i];
if ('.' == strBuf[i])
{
nCount++;
j = 0;
continue;
}
j++;
}
else
{
int j = 0;
strScore[j] = strBuf[i];
j++;
}
}
//add to links
addStuMSG(strStuNUM, strStuName, stioi(strScore));
}
fclose(pfile);
}
void Menu()
{
printf("********** 陈铭C项目 ******\n");
printf("-------------------------------------------------\n");
printf("********** 学生信息管理系统 ******\n");
printf("********** *指令* ******\n");
printf("-------------------------------------------------\n");
printf("********** 1.增加一个学生信息 ******\n");
printf("********** 11.增加学生信息(头添加) ******\n");
printf("********** 111.增加学生信息(指定位置)*****\n");
printf("********** 2.查找一个学生 ******\n");
printf("********** 3.修改学生信息 ******\n");
printf("********** 4.保存学生信息到文件 ******\n");
printf("********** 5.读取文件中的学生信息 ******\n");
printf("********** 6.删除指定学生信息 ******\n");
printf("********** 7.恢复删除的学生信息 ******\n");
printf("********** 8.显示所有学生信息 ******\n");
printf("********** 9.显示菜单 ******\n");
printf("********** 0.退出 ******\n");
printf("-------------------------------------------------\n");
}
标签:代码,pTemp,C语言,链表,phead,printf,arrStuName,NULL,arrStuNum 来源: https://www.cnblogs.com/chenming-1998/p/11632439.html