单位员工通讯录管理系统(线性表的使用)
#include<iostream>
#include <string>
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
typedef struct
{ //员工通讯信息的结构类型定义
string num; //员工编号
string name; //员工姓名
string tel; //手机号码
string email; //邮箱
}DataType;
typedef struct node
{
DataType data; //结点的数据域
struct node *next; //结点的指针域
}ListNode, *LinkList;
void CreateList_L(LinkList &L)
{
int i, n;
L = new ListNode;
L->next = NULL;
LinkList s = L;
LinkList p;
cin >> n;
for (i = 1; i <= n; ++i)
{
p = new ListNode;
cin >> p->data.num;
cin >> p->data.name;
cin >> p->data.tel;
cin >> p->data.email;
p->next = NULL;
s->next = p;
s = p;
}
}
void GetdataList_L(LinkList &L)
{//查询员工信息
ListNode *p;
int j = 0;
p = L->next;
string numm;
cin >> numm;
while (p)
{
if (p->data.name == numm)
{
j = 1; break;
}
p = p->next;
}
if (j)
{
//cout << "===编号===" << "===姓名===" << "===手机号码===" << "===邮箱===" << endl;
cout << p->data.num << " " << p->data.name << " " << p->data.tel << " " << p->data.email << endl;
}
else
cout << "NOT FOUND!" << endl;
}
void ModifydataList_L(LinkList &L)
{//修改员工信息
ListNode *p;
int j = 0;
p = L->next; string numm;
//cout << "请输入修改员工的姓名:" << endl;
cin >> numm;
while (p)
{
if (p->data.name == numm)
{
j = 1; break;
}
p = p->next;
}
if (j)
{
string shuxin;
string xgxx;
cin >> shuxin >> xgxx;
if (shuxin == "name")
p->data.name = xgxx;
else if (shuxin == "tel")
p->data.tel = xgxx;
else if (shuxin == "email")
p->data.email = xgxx;
}
else
cout << "NOT FOUND!" << endl;
}
void InsretdataList_L(LinkList &L)
{//插入员工信息
ListNode *p;
LinkList s = L;
p = new ListNode;
cin >> p->data.num >> p->data.name >> p->data.tel >> p->data.email;
p->next = NULL;
while (s->next != NULL)
{
s = s->next;
}
s->next = p;
}
void DeletedataList_L(LinkList &L)
{//指定位置删除元素
ListNode *p;
int j = 0;
p = L;
string numm;
//cout << "请输入删除员工的姓名:" << endl;
cin >> numm;
while (p->next)
{
if (p->next->data.name == numm)
{
j = 1; break;
}
p = p->next;
}
if (j)
{
p->next = p->next->next;
//cout << "删除成功" << endl;
}
else
cout << "NOT FOUND!" << endl;
}
void PrintfList_L(LinkList &L)
{
ListNode *p;
int j = 0;
p = L->next;
//cout << "===编号===" << "===姓名===" << "===手机号码===" << "===邮箱===" << endl;
while (p)
{
cout << p->data.num
<< " " << p->data.name
<< " " << p->data.tel
<< " " << p->data.email << endl;
p = p->next;
}
}
int main()
{
LinkList L;
CreateList_L(L);
int m;
string n;
cin >> m;
while (m != 0)
{
cin >> n;
if (n == "query")
GetdataList_L(L);
else if (n == "modify")
ModifydataList_L(L);
else if (n == "add")
InsretdataList_L(L);
else if (n == "del")
DeletedataList_L(L);
else if (n == "print")
PrintfList_L(L);
m--;
}
return 0;
}
停车场管理(栈和队列的应用)
#include <stdio.h>
#include<iostream>
#include<string>
using namespace std;
typedef struct
{
string CarNo[5000]; //车牌号
int front, rear; //队首和队尾指针
} SqQueue;
typedef struct
{
string CarNo[5000]; //车牌号
int CarTime[5000]; //进场时间
int top; //栈指针
} SqStack; //定义顺序栈类型,用于描述停车场
void InitStack(SqStack *&s) //构造一个空栈
{
s = new SqStack; //构造一个空栈
s->top = -1; //栈顶指针指向-1
}
int Push(SqStack *&s, string e1, int e2) //顺序栈入栈操作
{
s->top++;
s->CarNo[s->top] = e1;
s->CarTime[s->top] = e2;
return 1;
}
int Pop(SqStack *&s, string &e1, int &e2) //顺序栈出栈操作
{
if (s->top == -1) //判断栈是否是空栈
return 0;
e1 = s->CarNo[s->top]; //将栈顶元素出栈
e2 = s->CarTime[s->top]; //将栈顶元素出栈
s->top--; //栈顶指针减一
return 1;
}
void InitQueue(SqQueue *&q) //初始化一个循环队列(候车处)
{
q = new SqQueue;
q->front = q->rear = 0; //队头和队尾的指针置为0,表示队列为空
}
int enQueue(SqQueue *&q, string e) //进队
{
q->rear = q->rear + 1; //入队就是在队尾插入一个新元素。队尾指针加一
q->CarNo[q->rear] = e; //元素入队
return 1;
}
int deQueue(SqQueue *&q, string &e, string b) //出队
{
if (q->front == q->rear) //队空的情况
return 0;
for (int i = q->front; i <= q->rear; i++)
{
if (q->CarNo[i] == b) //表示找到了
{
if (i == q->front) //如果找到的元素是在队头
{
q->front = q->front + 1; //对头指针向后加一
e = q->CarNo[q->front]; //元素出队
return 1;
}
else if (i == q->rear) //如果找到的元素是在队尾
{
e = q->CarNo[q->rear]; //队尾元素出队
q->rear--; //队尾指针向前减一
return 1;
}
else //如果不是在头也不是在尾
{
e = q->CarNo[i]; //元素出队
for (int j = i; j < q->rear; j++)
q->CarNo[j] = q->CarNo[j + 1]; //将出队元素后面的元素向前移动一位
q->rear--; //队尾指针向前移动一位
return 1;
}
break;
}
}
}
void carsystem(SqStack *St,SqStack *St1,SqQueue *Qu)
{
int e2; //出车时间
int i, j, t, n, q;
int time;
string a;
string b;
string e1; //车牌
cin >> n >> q;
int x = 0, y = 0, f = 0;
for (int k = 0; k < q; k++)
{
cin >> a >> b >> time;
if (a == "arrive")
{
if (x < n)
{
x++;
Push(St, b, time);
cout << "Stack " << x << endl;
}
else
{
y++;
enQueue(Qu, b);
cout << "Queue " << y << endl;
}
}
else if (a == "leave")
{
for (i = 0; i < x; i++)
{
if (St->CarNo[i] == b)
break;
}
if (i == x)
{
deQueue(Qu, e1, b);
cout << f;
if (k < (q - 1))
cout << endl;
y--;
}
else
{
x--;
t = St->top - i;
for (j = 0; j < t; j++)
{
Pop(St, e1, e2);
Push(St1, e1, e2); //把后面的车弄进进辅助栈
cout << e1 << ":out ";
}
Pop(St, e1, e2); //该汽车离开
cout << e1 << ":out ";
int p = time - e2;
while (St1->top != -1) //将临时栈St1重新回到St中
{
Pop(St1, e1, e2);
Push(St, e1, e2);
cout << e1 << ":in ";
}if (Qu->front != Qu->rear)
{
deQueue(Qu, e1, Qu->CarNo[Qu->front]);
Push(St, e1, time);
y--; x++;
cout << e1 << ":in ";
}
cout << p;
if (k < (q - 1))
cout << endl;
}
}
}
}
int main()
{
SqStack *St, *St1;
SqQueue *Qu;
InitStack(St);
InitStack(St1);
InitQueue(Qu);
carsystem( St,