#include <iostream>
#include <stdio.h>
#include <string.h>
#include<malloc.h>
using namespace std;
typedef struct {
string name;
int age;
string score;
}ElemTpye;
typedef struct LNode{
ElemTpye data;
struct LNode *next;
}LNode,*LinkList;
void InitList(LinkList &L){
L = new LNode;
if (!L){
cout<<"False"<<endl;
}
L->next = NULL;
return ;
}
void DestroyList(LinkList &L){
LinkList p;
while(L){
p = L;
L = L->next;
delete p;
}
cout<<"OK"<<endl;
return ;
}
int CountNumber(LinkList &L){
int j = 0;
LinkList p = L->next;
while(p){
p = p->next;
j++;
}
return j;
}
void GetValue(LinkList &L,int value){
int j = 0;
LinkList p = L;
while(p&&value>j){
p = p->next;
j++;
}
if(!p || j>value){
cout<<"查询位置不存在"<<endl;
return ;
}
else{
cout<<"姓名: "<<p->data.name<<" 年龄: "<<p->data.age<<" 学号: "<<p->data.score<<endl;
return;
}
}
void FindValue(LinkList &L,ElemTpye a){
int j = 1;
LinkList p = L->next;
while(p){
if((p->data.name == a.name)&&(p->data.age == a.age) && (p->data.score == a.score))
{
break;
}
p = p->next;
j++;
}
if(p){
cout<<j<<endl;
return;
}
else{
cout<<"NULL"<<endl;
return;
}
}
void ListDelete(LinkList &L,int i){
LinkList p=L;
int j=0;
while(p->next &&j<i-1){//寻找第i个结点,并令p指向其前驱
p=p->next; ++j;
}
if(!(p->next)||j>i-1){
cout<<"删除位置不存在"<<endl;
}; //删除位置不合理
LinkList r=p->next; //临时保存被删结点的地址以备释放
p->next=r->next; //改变删除结点前驱结点的指针域
delete r;//释放结点
cout<<"OK"<<endl;
return ;
}
void ListInsert(LinkList &L,int i,ElemTpye e){
LinkList p = L;
int j = 1;
while(p&&j<i){
p = p->next;
j++;
}
if(!p || j < i){
cout<<"false"<<endl;
return ;
}
LinkList s=new LNode;
s->data = e;
s->next=p->next;
p->next=s;
cout<<"OK"<<endl;
return ;
}
void ListPrint(LinkList &L){
LinkList p = L -> next;
while(p){
cout<<p->data.name<<" "<<p->data.age<<" "<<p->data.score<<endl;
p = p -> next;
}
cout<<"OK"<<endl;
return;
}
int main()
{
int choose;
LNode *head;
do
{
cout<<"**********************************************************************"<<endl;
cout<<"************************ 1.初始化或重置链表 ******************"<<endl;
cout<<"************************ 2.销毁链表 ******************"<<endl;
cout<<"************************ 3.链表中数据的个数 ******************"<<endl;
cout<<"************************ 4.所指位序的元素值 ******************"<<endl;
cout<<"************************ 5.链表已存在元素的位序 ******************"<<endl;
cout<<"************************ 6.请输入元素,求其直接前驱 ******************"<<endl;
cout<<"************************ 7.请输入元素,求其直接后继 ******************"<<endl;
cout<<"************************ 8.在第i个位置插入元素 ******************"<<endl;
cout<<"************************ 9.删除第i个元素 ******************"<<endl;
cout<<"************************ 10.输出所输入的链表元素 ******************"<<endl;
cout<<"************************ 11.初始化并输入链表元素 ******************"<<endl;
cout<<"************************ 12.退出 ******************"<<endl;
cout<<"**********************************************************************"<<endl;
cin>>choose;
if(choose>12 || choose <1){
cout<<"输入无效"<<endl;
}
else if(choose == 1){
InitList(head);
}
else if(choose == 2){
DestroyList(head);
}
else if(choose == 3){
int j = CountNumber(head);
cout<<"元素数目为"<<j<<endl;
}
else if(choose ==4){
cout<<"请输入你想查询的位置:"<<endl;
int value;cin>>value;
GetValue(head,value);
}
else if(choose == 5){
ElemTpye other;
cout<<"请输入你想查询的学生的姓名,年纪,学号:";
cin>>other.name;
cin>>other.age;
cin>>other.score;
FindValue(head,other);
}
else if(choose == 6){
cout<<"请输入你想查询的位置:"<<endl;
int value;cin>>value;
GetValue(head,value-1);
}
else if(choose == 7){
cout<<"请输入你想查询的位置:"<<endl;
int value;cin>>value;
GetValue(head,value+1);
}
else if(choose == 8){
ElemTpye* e;
e = new ElemTpye;
cout<<"请输入姓名,年纪,学号:";
cin>>e->name>>e->age>>e->score;
cout<<"输入你要插入的位置: ";
int i;
cin>>i;
ListInsert(head,i,*e);
}
else if (choose ==9){
cout<<"请输入你想删除的位置:"<<endl;
int value;cin>>value;
ListDelete(head,value);
}
else if (choose ==10){
ListPrint(head);
}
else if (choose == 11){
int i;
do{
ElemTpye *e;
e = new ElemTpye;
cout<<"请输入姓名: ";
cin>>e->name;
cout<<"请输入年纪: ";
cin>>e->age;
cout<<"请输入学号: ";
cin>>e->score;
i = CountNumber(head);
cout<<i<<endl;
ListInsert(head,i+1,*e);
cout<<"你是否还要插入如果否请输入1……"<<endl;
cin>>i;
}while(i!=1);
}
}while(choose!=12);
return 0;
}
链式线性表实现
最新推荐文章于 2022-07-17 16:45:05 发布