include stdio include stdlib struct Element AM struct Element struct E

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
#include <stdio.h>
#include <stdlib.h>
struct Element **AM;
struct Element
{
struct Element *prev, *next;
int u,key;
};
struct Element* InitDoubleLinkedList()
{
struct Element *DL_List;
DL_List = malloc(sizeof(struct Element));
DL_List->prev=DL_List;
DL_List->next=DL_List;
DL_List->u=-999999;
DL_List->key=-999999;
return DL_List;
}
void InsertAfter(int h, struct Element* y)
{
struct Element* x=AM[h]->prev;
struct Element* z=AM[h];
x->next=y;
y->prev=x;
y->next=z;
z->prev=y;
}
void Assign(int k,int u,int m)
{
struct Element *check;
struct Element *new;
int h=k%m;
if(AM[h]->next!=AM[h])
{
for(check = AM[h]->next; check != AM[h] && k!=check->key; check=check->next);
if(check==AM[h])
{
new=malloc(sizeof(struct Element));
new->u=u;
new->key=k;
InsertAfter(h,new);
}
else
{
check->u=u;
}
}
else
{
new=malloc(sizeof(struct Element));
new->u=u;
new->key=k;
InsertAfter(h,new);
}
}
void AT(int k,int m)
{
int h=k%m;
struct Element *find;
for (find=AM[h]->next;find!=AM[h] && k!=find->key;find=find->next);
if (find==AM[h])
{
printf("0\n");
}
else
{
printf("%d\n",find->u);
}
}
int main (int argc, char const *argv[])
{
int n,m,k=0,El=0,i;
char str[7];
scanf("%d", &n);
scanf("%d", &m);
AM = malloc(m * sizeof(struct Element*));
for(i = 0; i < m;i++)
{
AM[i]=InitDoubleLinkedList();
}
for(i = 0; i < n; i++)
{
scanf("%s",str);
scanf("%d",&k );
if(str[2]=='\0')
{
AT(k,m);
}
else
{
scanf("%d",&El );
Assign(k,El,m);
}
}
return 0;
}