单向循环链表的创建以及C++中pair标准类型的使用
程序员文章站
2024-03-22 14:17:40
...
本程序中涉及两个知识点:
1、数据结构:
单向循环链表的创建
2、C++关联容器中pair标准类型的使用(使用的头函数utility)
题目:从固定格式字符串中创建姓名、薪资单向循环链表并输出薪资最高的人员的姓名和薪资。
#include<iostream>
#include<string>
#include <cstring>
#include<vector>
#include<algorithm>
#include<functional>
#include<utility>
#include <cstdlib>
using namespace std;
struct staff_info {
char name[40];
int data;
struct staff_info * next;
};
struct staff_info * compute_salary(const char * s) {
int n = 0;
staff_info * sta = new staff_info();
staff_info * head = sta;
while (s[n++] == '>') {
string name = "";
int i = 0;
char ch[100] = {""};
while (s[n] != '$') {
ch[i++] = s[n++];
}
memset(sta->name, 0, 40);
for (int j = 0; j < i ; j++) {
sta->name[j] = ch[j];
}
n++;
string str = "";
while (s[n] != '\0'&& isdigit(s[n])) {
str += s[n++];
}
sta->data = stoi(str);
if (s[n] != '\0'){
staff_info * p = new staff_info();
sta->next = p;
sta = p;
}
else
{
sta->next = head;
}
}
pair<string, int> max = { head->name, head->data };
staff_info * temp = head->next;
while (temp != head){
if (temp->data > max.second){
max = { temp->name, temp->data };
}
temp = temp->next;
}
cout << max.first << " " << max.second << endl;
return head;
}
int main() {
char *c = ">Zhang$12000>Wang$9114>Li$8456>Hu$7200";
staff_info *head = compute_salary(c);
return 0;
}