zx142407789 @ 2023-07-01 20:19:36
#include<stdio.h>
#include<stdlib.h>
typedef struct linklist {
int loc;//存放位置
long int data;//存的数据
struct linklist* next;
}*Locker, node;
long int locnode(node* head, int a);
void renode(node* head, int n, long int m);
void addnode(node* head, int a, long int b);
int main() {
int n, m;
scanf("%d %d", &n, &m);
node* locker = malloc(sizeof(Locker) * n);//分配数组存放每个柜子的头指针
node* head = malloc(sizeof(Locker));
head->next = NULL;
for (int a = 1; a <= n; a++) {
locker[a].next = head;
}
int x, i, j, k;
long int out[10000];//存储查询的结果
long int count = 0;//记录查询的次数
while (m > 0) {
scanf("%d ", &x);
if (x == 1) {//做写入操作1
scanf("%d %d %ld", &i, &j, &k);
if (locnode(locker[i].next, j)) {//查找是否存在
renode(locker[i].next, j, k);//存在,进行修改
}
else {
addnode(locker[i].next, j, k);//不存在,插入新的结点
}
}
else {//做读取操作2
scanf("%d %d", &i, &j);
out[count] = locnode(locker[i].next, j);//查找并返回
count++;
}
m--;
}
for (int z = 0; z < count; z++) {
printf("%ld\n", out[z]);
}
return 0;
}
long int locnode(node* head, int a) {
node* Pnode = head->next;
while (Pnode != NULL) {
if (a == Pnode->loc) {
return Pnode->data;
}
Pnode = Pnode->next;
}
return 0;
}
void renode(node* head, int n, long int m) {
node* Pnode = head->next;
while (Pnode != NULL) {
if (n == Pnode->loc) {
Pnode->data = m;
break;
}
Pnode = Pnode->next;
}
}
void addnode(node* head, int a, long int b) {
node* spot = (node*)malloc(sizeof(node));
node* p = head;
while (p->next != NULL) {
p = p->next;
}
spot->loc = a;
spot->data = b;
p->next = spot;
spot->next = NULL;
}