********************************************************************************************
5、数据结构练习
要求:
1)要求见图
***********************************************************************************************************
代码:
#include <stdio.h>
#include <Windows.h>
#include <stdlib.h>
#pragma comment(lib,"ws2_32.lib")
typedef struct list{
int Key;
int Value;
struct list* next;
}LinkList;
typedef struct Map
{
LinkList* headNode;
LinkList* endNode;
int listLen;
}Map;
int size(Map* map)
{
return map->listLen;
}
void put(Map* map,int Key,int Value)
{
if(NULL != map)
{
if(NULL != map->headNode)
{
LinkList* node = map->headNode;
while(map->endNode != node)
{
if(Key == node->Key)
{
node->Value = Value;
return ;
}
node = node->next;
}
LinkList* addNode = (LinkList*)malloc(sizeof(LinkList));
addNode->Key = Key;
addNode->Value = Value;
addNode->next = map->headNode->next;
map->headNode->next = addNode;
map->listLen++;
}
else
{
LinkList* addNode = (LinkList*)malloc(sizeof(LinkList));
addNode->Key = Key;
addNode->Value = Value;
map->headNode->next = addNode;
addNode->next = map->endNode;
map->listLen++;
}
}
}
int getValue(Map* map,int Key)
{
if(NULL != map)
{
LinkList* node = map->headNode->next;
while(NULL != node)
{
if(Key == node->Key)
{
return node->Value;
break;
}
node = node->next;
}
}
printf("没有找到键值为%d的Value",Key);
return 0;
}
int containsKey(Map* map,int Key)
{
if(NULL != map && NULL != map->headNode->next)
{
LinkList* node = map->headNode->next;
while(NULL != node)
{
if(Key == node->Key)
{
printf("存在键值为%d的键\n",Key);
return 1;
}
node = node->next;
}
}
return 0;
}
int containsValue(Map* map,int Value)
{
if(NULL != map && NULL != map->headNode->next)
{
LinkList* node = map->headNode->next;
while(NULL != node)
{
if(Value == node->Value)
{
printf("存在%d的Value值\n",Value);
return 1;
}
node = node->next;
}
printf("不存在这个value\n");
}
return 0;
}
void remove(Map* map,int Key)
{
if(NULL != map)
{
if(NULL != map->headNode->next)
{
LinkList* tmp = map->headNode;
LinkList* delNode = map->headNode->next;
while(map->endNode != delNode)
{
if(Key == delNode->Key)
{
tmp->next = delNode->next;
free(delNode);
map->listLen--;
return ;
}
tmp = tmp->next;
delNode = delNode->next;
}
}
}
}
void clear(Map* map)
{
LinkList* list = map->headNode;
if(NULL != list)
{
LinkList* h = list;
if(NULL != h->next)
{
LinkList* tmp = h->next;
while(map->endNode != tmp)
{
free(h);
h = tmp;
tmp = tmp->next;
}
}
}
}
/*
LinkList *creat(int n)
{
LinkList *head, *node, *end;
head = (LinkList*)malloc(sizeof(LinkList));
end = head;
for (int i = 0; i < n; i++)
{
node = (LinkList*)malloc(sizeof(LinkList));
printf("输入KV的值:");
scanf("%d%d", &node->K,&node->V);
end->next = node;
end = node;
}
end->next = NULL;
return head;
}
void insert(LinkList *list, int n)
{
LinkList *t = list;
LinkList *p;
if (t != NULL)
{
p = (LinkList*)malloc(sizeof(LinkList));
p->K = n;
p->next = t->next;
t->next = p;
}
}
void put(LinkList *list,int K,int V)
{
LinkList *t = list;
while (t != NULL)
{
if(t->K == K)
{
t->V = V;
break;
}
t = t->next;
}
insert(list,V);
}
void remove(LinkList *list, int n)
{
LinkList *t = list;
while(NULL != t)
{
if(n == t->K)
{
LinkList* tmp = t->next;
t->K = t->next->K;
t->V = t->next->V;
t->next = t->next->next;
free(tmp);
return ;
}
t = t->next;
}
printf("不存在%d的键\n");
}
void clear(LinkList*list)
{
if(NULL != list)
{
LinkList* h = list;
if(NULL != h->next)
{
LinkList* tmp = h->next;
while(NULL != tmp)
{
free(h);
h = tmp;
tmp = tmp->next;
}
}
}
}
void Printf(LinkList*list)
{
LinkList* h = list;
while (h->next != NULL)
{
h = h->next;
printf("%d ", h->K);
}
putchar('\n');
}
LinkList* get(LinkList*list,int n)
{
LinkList* h = list;
while(h != NULL)
{
if(h->K == n)
{
return h;
}
h = h->next;
}
return NULL;
}
int size(LinkList* head)
{
if(NULL == head)
{
return -1;
}
LinkList* p = head->next;
int num = 0;
while(NULL != p->next)
{
num++;
p = p->next;
}
return num;
}
void containKey(LinkList*list,int n)
{
LinkList* h = list;
while(h != NULL)
{
if(h->K == n)
{
printf("含有%d这个键\n",h->K);
return ;
}
h = h->next;
}
printf("不存在这样的键\n");
}
void containValue(LinkList*list,int n)
{
LinkList* h = list;
while(h != NULL)
{
if(h->V == n)
{
printf("含有%d这个值\n");
}
h = h->next;
}
printf("不存在这样的值\n");
}
*/
Map* init(int n)
{
Map* map = (Map*)malloc(sizeof(Map));
map->listLen = 0;
map->headNode = (LinkList*)malloc(sizeof(LinkList));
map->headNode->next = map->endNode;
int i = 0;
for(i;i<n;i++)
{
LinkList* node = (LinkList*)malloc(sizeof(LinkList));
printf("请输入Key、Value的值:");
scanf("%d%d",&(node->Key),&(node->Value));
node->next = map->headNode->next;
map->headNode->next = node;
map->listLen++;
}
return map;
}
int main()
{
Map* map = init(3);
printf("map的大小为:%d\n",size(map));
put(map,11,2);
put(map,31,4);
printf("map的大小为:%d\n",size(map));
printf("键值为11的value值为%d\n",getValue(map,11));
containsKey(map,3);
containsValue(map,2);
remove(map,11);
printf("map的大小为:%d\n",size(map));
clear(map);
system("pause");
return 0;
}