您的位置:首页 > 其它

线性表的基本操作及其作用

2017-10-29 11:57 190 查看
**线性表的基本操作及其作用**
链表:


typedef struct {
char no[8];   //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);
/* 功能:在控制台输出欢迎界面
* 参数:choose的引用
* 返回值:无
*/

void headInsert(linkPtr &head, StuData &info);
/*
* 功能:在链表头部插入节点
* 参数:head:链表头节点的引用  info:需要插入学生信息
* 返回值:无
*/

void insert(linkPtr &head, StuData &info, int order);
/*
* 功能:在指定的位置插入学生信息
* 参数:head:链表头节点的引用   info:需要插入的学生信息   order: 插入到链表中的位置】
* 返回值: 无
*/

void add(linkPtr &head, StuData &info);
/*
* 功能:(在尾部)增加学生信息
* 参数:head:链表头节点的引用  info: 需要增加的学生信息
* 返回值: 无
*/

linkPtr search(linkPtr &head, string &target);
/*
* 功能:对于输入的ID/Name, 遍历查找各数据元素中是否存在相应的数据项
* 参数:head:链表头节点的引用    target: 需要查找的ID/Name
* 返回值:如果存在对应的匹配,返回其所在的节点指针,否则返回NULL
*/

bool isContain(const linkPtr &head, const string &target);
/*
* 功能:判断输入的ID是否已经存在,增强健壮性
* 参数:head:链表头节点的指针的引用   target:输入的学号
* 返回值:若已经存在相应的学号,返回true, 否则返回false
*/
void deleteNode(linkPtr &head, linkPtr &discard);
/*
* 功能:删除指定的数据项
* 参数:head:链表头节点指针的引用  discard: 需要删除的数据项的指针的引用
* 返回值:无
*/

void print(linkPtr p, char type='o');
/*
* 功能:打印单个或所有信息
* 参数:p: 指向某个数据项   type: 打印类型,默认输出一个数据元素,为'a'时
*        表示输出p后所有数据项
*/
void save(linkPtr &head, ofstream &allStuData);
/*
* 功能:将输入的所有数据项保存到文件中
* 参数:head: 链表头节点指针的引用    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]: ");
dfe0

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息