include stdio include stdlib include string struct Element struct Elem

  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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Element
{
struct Element *next;
int u,key;
};
struct Element* InitSingleLinkedList()
{
struct Element *list;
list = (struct Element*)malloc(sizeof(struct Element));
list->next=NULL;
list->u=-999999;
list->key=-999999;
return list;
}
void InsertAfter(struct Element* list, struct Element* y)
{
struct Element* x,*z;
for (x=list; x!=NULL;)
{
z=x;
x=x->next;
}
z->next=y;
y->next=NULL;
}
void Assign(struct Element **AM,int k,int u,int m,struct Element *new)
{
struct Element *check;
int h=k%m;
if(AM[h]->next!=AM[h])
{
for(check = AM[h]->next; check != NULL && k!=check->key; check=check->next);
if(check==NULL)
{
new->u=u;
new->key=k;
InsertAfter(AM[h],new);
}
else
{
check->u=u;
}
}
else
{
new->u=u;
new->key=k;
InsertAfter(AM[h],new);
}
new=NULL;
free(new);
}
void AT(struct Element **AM,int k,int m)
{
int h=k%m;
struct Element *find;
for (find=AM[h]->next;find!=NULL && k!=find->key;find=find->next);
if (find==NULL)
{
printf("0\n");
}
else
{
printf("%d\n",find->u);
}
}
int main (int argc, char const *argv[])
{
int n,m,k=0,El=0,i;
struct Element **AM;
struct Element *freee;
struct Element *next;
char str[6];
scanf("%d", &n);
scanf("%d", &m);
AM = malloc(m * sizeof(struct Element*));
for(i = 0; i < m;i++)
{
AM[i]=InitSingleLinkedList();
}
for(i = 0; i < n; i++)
{
scanf("%s",str);
scanf("%d",&k );
if(str[2]=='\0')
{
AT(AM,k,m);
}
else
{
freee=InitSingleLinkedList();
scanf("%d",&El );
Assign(AM,k,El,m,freee);
}
}
for (i=0;i<m;i++)
{
for (freee=AM[i];freee!=NULL;)
{
next=freee->next;
free(freee);
freee=next;
}
}
free(AM);
return 0;
}