#include <stdio.h>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <Windows.h>
using namespace std;
typedef struct data
{
struct data *next;
struct data *prior;
string name;
long long num;
string sex;
data()
{
next = NULL;
prior = NULL;
name = "NULL";
num = -1;
sex = "NULL";
}
}list;
#define Len sizeof(list)
list* search(list *tp);
list* creatList()
{
list* tp = new list;
tp->prior = tp;
tp->next = tp;
return tp;
}
void clearList(list *tp)
{
while(tp->name != "NULL" && tp->num != -1 && tp->sex != "NULL")
tp = tp->next;
list* head = tp;
tp = tp->next;
while(head->next->name != "NULL" && head->next->num != -1 && head->next->sex != "NULL")
{
head->next = tp->next;
free(tp);
tp = head->next;
}
head->prior = head;
cout << "Contract Empty" << endl;
}
void destoryList(list *tp)
{
clearList(tp);
free(tp);
}
void insert(list *tp, list *tep)
{
tep->next = tp->next;
tep->prior = tp;
tp->next->prior = tep;
tp->next = tep;
}
list* makeNode()
{
list* tp = new list;
cout << "please input name, sex, num : ";
cin >> tp->name >> tp->sex >> tp->num;
return tp;
}
void delNode(list *tp)
{
cout << "Delete element : ";
if(!search(tp)) cout << "The element which you want to delete is not exist" << endl;
else
{
tp->prior->next = tp->next;
tp->next->prior = tp->prior;
free(tp);
}
}
list* search(list *tp)
{
string str;
cout << "please input the key style (include name,num) : ";
cin >> str;
list* head = tp;
if(str == "name")
{
cout << "please input which name you want to search : ";
cin >> str;
tp = tp->next;
while(tp->name != str && tp != head)
tp = tp->next;
if(tp->name != str)
return NULL;
else
return tp;
}
else if(str == "num")
{
long long num;
cout << "please input which number you want to search : ";
cin >> num;
tp = tp->next;
while(tp->num != num && tp != head)
tp = tp->next;
if(tp->num != num)
return NULL;
else return tp;
}
else
return NULL;
}
void modify(list *head)
{
cout << "Modify elemtion : ";
list *tp = search(head);
if(!tp) cout << "the key can‘t find in the list" << endl;
else
{
cout << "the elemtion which you want modify is " << tp->name << ‘ ‘ << tp->sex << ‘ ‘ << tp->num << endl;
cout << "please input key what you want to modify :" << endl << "1. name" << endl << "2. sex" << endl << "3. num" << endl;
int key;
L:
cin >> key;
if(key == 1)
{
cout << "please input name : ";
cin >> tp->name;
}
else if(key ==2)
{
cout << "please input sex : ";
cin >> tp->sex;
}
else if(key == 3)
{
cout << "please input num : ";
cin >> tp->num;
}
else
{
cout << "Input error , please input again :";
goto L;
}
}
cout << "the elemtion which you modify is " << tp->name << ‘ ‘ << tp->sex << ‘ ‘ << tp->num << endl;
}
void print(list *tp)
{
list* head = tp;
tp = tp->next;
while(tp!= head)
{
if(tp->name != "NULL" && tp->sex != "NULL" && tp->num != -1)
cout << tp->name << ‘ ‘ << tp->sex << ‘ ‘ << tp->num << endl;
tp = tp->next;
}
if(head->name != "NULL" && head->sex != "NULL" && head->num != -1)
cout << head->name << ‘ ‘ << head->sex << ‘ ‘ << head->num << endl;
}
int main()
{
printf("\t\t\t\tThe Contract List\n\n");
system("color a");
list* head = creatList();
cout << "please select which operate you want to do :" << endl;
cout << "1. Insert" << endl << "2. Search " << endl << "3. Delete" << endl << "4. Modify" << endl
<< "5. Print all elemtion" << endl << "6. Empty contract"<< endl <<"0. Exit" << endl;
cout << "_____________________________________________________________________________" << endl;
//system("color b");
int key;
while(cin >> key && key != 0)
{
if(key == 1)
{
list*a =makeNode();
insert(head, a);
}
else if(key == 2)
{
A:
list *b = search(head);
if(!b)
{
cout << "the key you input is not exist in element , please input again" << endl;
goto A;
}
else
cout << "The element is :" << b->name << ‘ ‘ << b->sex << ‘ ‘ << b->num << endl;
}
else if(key == 3)
{
delNode(head);
}
else if(key == 4)
{
modify(head);
}
else if(key == 5)
{
print(head);
}
else if(key == 6)
clearList(head);
else
cout << "Input Error" << endl;
}
destoryList(head);
return 0;
}
附件列表
时间: 2024-10-11 21:01:29