#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct StudentMessage
{
string ClassName;
string sex;
string StudentName;
string StudentID;
};
struct LinkStackNode
{
StudentMessage m_Data;
LinkStackNode* m_Next;
};
void init_LinkStack(LinkStackNode*& LinkStack)
{
LinkStack->m_Next = NULL;
cout << "init success" << endl;
}
void push_LinkStack(LinkStackNode* & LinkStack, StudentMessage newData)
{
LinkStackNode* tempNode = new LinkStackNode();
tempNode->m_Data.ClassName = newData.ClassName;
tempNode->m_Data.sex = newData.sex;
tempNode->m_Data.StudentID = newData.StudentID;
tempNode->m_Data.StudentName = newData.StudentName;
tempNode->m_Next = LinkStack->m_Next;
LinkStack->m_Next = tempNode;
}
void Pop_LinkStack(LinkStackNode*& LinkStack)
{
if (LinkStack->m_Next == NULL)
{
cout << "empty stack" << endl;
}
else
{
StudentMessage Data;
Data.ClassName = LinkStack->m_Next->m_Data.sex;
Data.sex = LinkStack->m_Next->m_Data.sex;
Data.StudentID = LinkStack->m_Next->m_Data.StudentID;
Data.StudentName = LinkStack->m_Next->m_Data.StudentName;
cout << "the poped data is " << Data.StudentName << endl;
LinkStackNode* tempNode = new LinkStackNode();
tempNode = LinkStack->m_Next;
LinkStack = tempNode->m_Next;
delete tempNode;
cout << "The current top data is " << LinkStack->m_Data.StudentName << endl;
}
}
void LinkStack_main()
{
cout << "***********1.init function*************" << endl;
cout << "***********2.Push function*************" << endl;
cout << "***********3.Pop function*************" << endl;
cout << "***********0.Exit *************" << endl;
int choose = -1;
LinkStackNode* myLinkStack = new LinkStackNode();
while (choose != 0)
{
cout << "Please input your choose:" << endl;
cin >> choose;
switch (choose)
{
case 1:
{
init_LinkStack(myLinkStack);
}
/* code */
break;
case 2:
{
string FileName;
cout << "Please input filename" << endl;
cin >> FileName;
fstream ifs;
ifs.open(FileName, ios::in);
if (!ifs)
{
cout << "File open fail" << endl;
}
while (!ifs.eof())
{
StudentMessage newData;
ifs >> newData.ClassName >> newData.sex >> newData.StudentID >> newData.StudentName;
push_LinkStack(myLinkStack, newData);
}
cout << "The current top is " << myLinkStack->m_Data.StudentName << endl;
ifs.close();
}
break;
case 3:
{
Pop_LinkStack(myLinkStack);
}
break;
default:
break;
}
}
}
int main()
{
LinkStack_main();
system("pause");
return 0;
}
栈链表实现班级人员管理
最新推荐文章于 2025-05-17 14:04:14 发布