**线性表的基本操作及其作用**
链表:
typedef struct {
char no[8];
char name[20];
int score;
}Student;
#include <iostream>
#include <cstdio>
#include <fstream>
#include <unistd.h>
#include <cstdlib>
#include <cstddef>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::ofstream;
using std::string;
struct StuData
{
string stuID;
string stuName;
double stuScore;
};
class LinkList
{
public:
LinkList( ) { }
LinkList(StuData info, LinkList *previous, LinkList *next)
: stuInfo(info), previousLink(previous), nextLink(next)
{ }
LinkList *getPreviousLink( )
{
return previousLink;
}
LinkList *getNextLink( )
{
return nextLink;
}
StuData &getData( )
{
return stuInfo;
}
void setPreviousLink(LinkList *pointer)
{
previousLink = pointer;
}
void setNextLink(LinkList *pointer)
{
nextLink = pointer;
}
private:
StuData stuInfo;
LinkList *previousLink;
LinkList *nextLink;
};
typedef LinkList* linkPtr;
void welcome(unsigned &choose);
void headInsert(linkPtr &head, StuData &info);
void insert(linkPtr &head, StuData &info, int order);
void add(linkPtr &head, StuData &info);
linkPtr search(linkPtr &head, string &target);
bool isContain(const linkPtr &head, const string &target);
void deleteNode(linkPtr &head, linkPtr &discard);
void print(linkPtr p, char type='o');
void save(linkPtr &head, ofstream &allStuData);
int main(void)
{
linkPtr head = NULL;
StuData info;
unsigned choose;
while (true)
{
fflush(stdin);
system("color 2F");
char ans = 'y';
welcome(choose);
switch (choose)
{
case 1:
{
system("color 8F");
while (ans == 'y')
{
printf("Please enter data:\n");
printf("ID: ");
cin >> info.stuID;
printf("Name: ");
getchar();
getline(cin, info.stuName);
printf("Score: ");
cin >> info.stuScore;
add(head, info);
fflush(stdin);
printf("Continue![y]: ");
ans = getchar( );
if (ans=='\n')
{
ans = 'y';
}
system("cls");
}
break;
}
case 2:
{
system("color 6F");
string ID_Name;
linkPtr locate;
while (ans == 'y')
{
printf("Please give the stu's ID/Name: ");
getchar();
getline(cin, ID_Name);
locate = search(head, ID_Name);
if (locate != NULL)
{
putchar('\n');
print(locate, 'o');
}
else
{
printf("\nWarning: You entered information is not exist!\n");
}
putchar('\n');
fflush(stdin);
printf("Continue![y]: ");
scanf("%c", &ans);
if (ans=='\n')
{
ans = 'y';
}
system("cls");
}
break;
}
case 3:
{
system("color B4");
linkPtr discard;
string litter;
printf("Please input the ID/Name of the student you want to delete: ");
fflush(stdin);
getline(cin, litter);
discard = search(head, litter);
if (discard != NULL)
{
deleteNode(head, discard);
printf("\aDelete succeed!\n");
}
getchar( );
system("cls");
break;
}
case 4:
{
system("color DF");
print(head, 'a');
system("cls");
break;
}
case 5:
{
ofstream allStuData;
allStuData.open("stuInformation.dat");
save(head, allStuData);
allStuData.close();
system("cls");
break;
}
case 6:
{
printf("\a\n\n\t\tBye Bye!\n");
sleep(1);
system("color 0F");
exit(0);
break;
}
default :
{
printf("\a\n\tSorry! I haven't been creat this operation!\n");
sleep(2);
system("cls");
break;
}
}
}
return 0;
}
void welcome(unsigned &choose)
{
printf("\n\n\n\n\n WELCOME\n");
string reticule = "-------------------------------------------------------\n";
string prompt1 = "-- 1.增加或建立学生信息\n";
string prompt2 = "-- 2.根据姓名或学号找人\n";
string prompt3 = "-- 3.删除不必要学生信息\n";
string prompt4 = "-- 4.显示所有的学生信息\n";
string prompt5 = "-- 5.保存\n";
string prompt6 = "-- 6.退出\n\n";
unsigned sleepTime = 900;
cout << reticule << endl;
for (unsigned i=0; i<prompt1.length(); i++)
{
usleep(sleepTime);
putchar(prompt1[i]);
fflush(stdout);
}
for (unsigned i=0; i<prompt2.length(); i++)
{
usleep(sleepTime);
putchar(prompt2[i]);
fflush(stdout);
}
for (unsigned i=0; i<prompt3.length(); i++)
{
usleep(sleepTime);
putchar(prompt3[i]);
fflush(stdout);
}
for (unsigned i=0; i<prompt4.length(); i++)
{
usleep(sleepTime);
putchar(prompt4[i]);
fflush(stdout);
}
for (unsigned i=0; i<prompt5.length(); i++)
{
usleep(sleepTime);
putchar(prompt5[i]);
fflush(stdout);
}
for (unsigned i=0; i<prompt6.length(); i++)
{
usleep(sleepTime);
putchar(prompt6[i]);
fflush(stdout);
}
cout << reticule << endl;
printf("你想进行什么操作: ");
scanf("%u", &choose);
system("cls");
}
void headInsert(linkPtr &head, StuData &info)
{
linkPtr newHead = new LinkList(info, NULL, head);
newHead->setPreviousLink(newHead);
head = newHead;
}
void insert(linkPtr &head, StuData &info, int order)
{
linkPtr p = head;
for (int i=0; i<order; i++)
{
p = p->getNextLink( );
}
linkPtr next = p->getNextLink( );
p->setNextLink(new LinkList(info, p, p->getNextLink( )));
p = p->getNextLink( );
next->setPreviousLink(p);
}
void add(linkPtr &head, StuData &info)
{
static unsigned int count = 0;
if (isContain(head, info.stuID))
{
printf("\a\n\n\t\t!!!Warning: This stu's ID have already exist!!!'\n");
return;
}
else
{
if (head == NULL)
{
head = new LinkList(info, NULL, NULL);
}
else
{
static linkPtr point = head;
while (point->getNextLink() != NULL)
{
point = point->getNextLink( );
}
point->setNextLink(new LinkList(info, point, NULL));
point = point->getNextLink( );
}
count++;
printf("\nAdd succeed!\t\t\tstu's num %u\n", count);
}
}
linkPtr search(linkPtr &head, string &target)
{
linkPtr here = head;
if (here == NULL)
{
printf("\a\n\t\t!!! Warning: NO data exist !!!\n\n");
return NULL;
}
else
{
while (here != NULL)
{
StuData ans = here->getData( );
if (ans.stuID == target || ans.stuName == target)
{
return here;
}
here = here->getNextLink( );
}
return NULL;
}
}
bool isContain(const linkPtr &head, const string &target)
{
linkPtr here = head;
while (here != NULL)
{
StuData ans = here->getData( );
if (ans.stuID == target || ans.stuName == target)
{
return true;
}
else
{
here = here->getNextLink( );
}
}
return false;
}
void deleteNode(linkPtr &head, linkPtr &discard)
{
if (head == discard)
{
head = head->getNextLink( );
if (head != NULL)
{
head->setPreviousLink(NULL);
}
}
else
{
linkPtr prev = discard->getPreviousLink( );
linkPtr next = discard->getNextLink( );
prev->setNextLink(next);
if (next != NULL)
{
next->setPreviousLink(prev);
}
}
delete discard;
}
void print(linkPtr p, char type)
{
switch (type)
{
case 'o':
{
StuData ans = p->getData( );
cout << "- ";
cout << ans.stuID;
cout << "\t\t";
cout << ans.stuName;
cout << "\t\t";
cout << ans.stuScore;
cout << endl;
break;
}
case 'a':
{
linkPtr point = p;
printf("All of the students are:\n");
printf(" ID Name Score\n");
printf(" -------------------------------------------------\n");
if (p == NULL)
{
printf("\n\tError: No data exist!\n");
}
else
{
unsigned count=0;
while (point != NULL)
{
printf("%3d- ", ++count);
StuData ans = point->getData( );
cout << ans.stuID;
cout << "\t\t";
cout << ans.stuName;
cout << "\t\t\t";
cout << ans.stuScore;
cout << endl;
point = point->getNextLink( );
}
}
printf("\n -------------------------------------------------\n");
getchar();
getchar();
break;
}
}
}
void save(linkPtr &head, ofstream &allStuData)
{
linkPtr point = head;
allStuData << "All of the students are:\n";
allStuData << " ID Name Score\n";
allStuData << " ----------------------------------------------\n";
if (point == NULL)
{
allStuData << "\n\tError: No data exist!\n";
}
else
{
int count=0;
while (point != NULL)
{
StuData ans = point->getData( );
allStuData << "";
allStuData << (++count) << " - ";
allStuData << ans.stuID;
allStuData << "\t\t";
allStuData << ans.stuName;
allStuData << "\t\t";
allStuData << ans.stuScore;
allStuData << endl;
point = point->getNextLink( );
}
}
allStuData << "\a\n ---------------------------------------------\n";
printf("\a\n\n\t\tSaving successfully!\n");
sleep(1);
}