#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OVERFLOW -1
typedef int Status;
typedef int ElemType;
struct LinkedListNode
{
int value;
int length;
struct LinkedListNode *next;
};
typedef struct LinkedListNode List;
typedef struct LinkedListNode *PtrList;
Status InitList(List *);
void DestroyList(List *);
void ClearList(List *);
Status ListEmpty(List);
int ListLength(List);
Status GetElem(List, int, ElemType *);
int LocateElem(List, ElemType);
Status ListInsert(List *, int, ElemType);
Status ListDelete(List *, int, ElemType *);
Status ListAppend(List *, ElemType);
void ListTraverse(List);
Status CreateSet(List *);
Status SetUnion(List *, List);
void SetIntersect(List *, List);
void SetSubtract(List *, List);
List *RetPriorAdd(List *t, int);
Status InitList(List *LinkedList)
{
if (!LinkedList)
return ERROR;
LinkedList->next = NULL;
LinkedList->length = 0;
return OK;
}
void DestroyList(List *LinkedList)
{
if (!LinkedList)
return;
ClearList(LinkedList);
}
void ClearList(List *LinkedList)
{
if (!LinkedList)
return;
ElemType buf;
while (!ListEmpty(*LinkedList))
ListDelete(LinkedList, 1, &buf);
LinkedList->next = NULL;
}
Status ListEmpty(List list)
{
return list.length == 0;
}
int ListLength(List list)
{
return list.length;
}
Status GetElem(List list, int n, ElemType *bufData)
{
if (n > list.length || n < 1)
return ERROR;
PtrList PtrToNode = RetPriorAdd(&list, n);
if (!bufData || !PtrToNode)
return ERROR;
*bufData = PtrToNode->next->value;
return OK;
}
int LocateElem(List list, ElemType bufData)
{
int i = 0;
PtrList PtrToNode = list.next;
while (PtrToNode)
{
i++;
if (PtrToNode->value == bufData)
return i;
PtrToNode = PtrToNode->next;
}
return 0;
}
List *RetPriorAdd(List *LinkedList, int n)
{
if (!LinkedList)
return ERROR;
if (n < 1 || n > LinkedList->length + 1)
return ERROR;
PtrList PtrToNode = LinkedList;
int i = 0;
while (PtrToNode->next && i < n - 1)
{
PtrToNode = PtrToNode->next;
i++;
}
return PtrToNode;
}
Status ListInsert(List *LinkedList, int n, ElemType bufData)
{
if (!LinkedList)
return ERROR;
if (n < 1 || n > LinkedList->length + 1)
return ERROR;
PtrList PtrToNode = RetPriorAdd(LinkedList, n);
PtrList PtrNewNode = (PtrList)malloc(sizeof(List));
if (!PtrNewNode)
return OVERFLOW;
PtrNewNode->value = bufData;
PtrNewNode->next = PtrToNode->next;
PtrToNode->next = PtrNewNode;
(LinkedList->length)++;
return OK;
}
Status ListDelete(List *LinkedList, int n, ElemType *bufData)
{
if (!LinkedList)
return ERROR;
if (n < 1 || n > LinkedList->length)
return ERROR;
PtrList PtrToNode_a, PtrToNode = RetPriorAdd(LinkedList, n);
PtrToNode_a = PtrToNode->next;
PtrToNode->next = PtrToNode_a->next;
*bufData = PtrToNode_a->value;
free(PtrToNode_a);
(LinkedList->length)--;
return OK;
}
Status ListAppend(List *LinkedList, ElemType bufData)
{
if (!LinkedList)
return ERROR;
Status Result = ListInsert(LinkedList, ListLength(*LinkedList) + 1, bufData);
if (Result == OK)
return OK;
else if (Result == OVERFLOW)
return OVERFLOW;
else
return ERROR;
}
void ListTraverse(List list)
{
printf("{ ");
ElemType bufData;
for (int i = 1; i <= list.length; i++)
{
GetElem(list, i, &bufData);
printf("%d ", bufData);
}
printf("}");
}
Status CreateSet(List *LinkedList)
{
if (!LinkedList)
return ERROR;
int n;
ElemType data;
ClearList(LinkedList);
scanf("%d", &n);
if (n < 0)
return OK;
for (int i = 1; i <= n && (scanf("%d", &data) != EOF); i++)
{
if (!LocateElem(*LinkedList, data))
{
Status res;
res = ListAppend(LinkedList, data);
if (res == OVERFLOW)
return OVERFLOW;
}
}
return OK;
}
// Status CreateSet(List *LinkedList)
// {
// if (!LinkedList)
// return ERROR;
// int n;
// ElemType data;
// PtrList PtrToNode = LinkedList->next;
// scanf("%d", &n);
// if (n < 0)
// return ERROR;
// for (int i = 1; i <= n && (scanf("%d", &data) != EOF); i++)
// {
// if (PtrToNode)
// {
// PtrToNode->value = data;
// PtrToNode = PtrToNode->next;
// }
// else
// {
// Status res;
// res = ListAppend(LinkedList, data);
// if (res == OVERFLOW)
// return OVERFLOW;
// }
// }
// return OK;
// }
Status SetUnion(List *LinkedList, List list)
{
if (!LinkedList)
return ERROR;
ElemType Data;
int len_b = ListLength(list), position = 0;
for (int i = 1; i <= len_b; i++)
{
GetElem(list, i, &Data);
position = LocateElem(*LinkedList, Data);
if (position != 0)
;
else
{
ListAppend(LinkedList, Data);
}
}
return OK;
}
void SetIntersect(List *LinkedList, List list)
{
ElemType data;
int position = 0;
for (int i = 1; i <= ListLength(*LinkedList);)
{
GetElem(*LinkedList, i, &data);
position = LocateElem(list, data);
if (position == 0)
{
ListDelete(LinkedList, i, &data);
continue;
}
i++;
}
}
void SetSubtract(List *LinkedList, List list)
{
ElemType data;
int position = 0;
for (int i = 1; i <= ListLength(*LinkedList);)
{
GetElem(*LinkedList, i, &data);
position = LocateElem(list, data);
if (position != 0)
{
ListDelete(LinkedList, i, &data);
continue;
}
i++;
}
}
int main()
{
List linkedlist, linkedlist_a, linkedlist_b;
InitList(&linkedlist);
InitList(&linkedlist_a);
InitList(&linkedlist_b);
ClearList(&linkedlist);
CreateSet(&linkedlist);
ListTraverse(linkedlist);
printf("\n");
ElemType data = 100;
ListAppend(&linkedlist, data);
ListTraverse(linkedlist);
printf("%d\n", ListLength(linkedlist));
ClearList(&linkedlist);
ListTraverse(linkedlist);
printf("%d\n", ListLength(linkedlist));
ListTraverse(linkedlist_a);
DestroyList(&linkedlist);
ListTraverse(linkedlist_a);
printf("\n");
// SetIntersect(&linkedlist, linkedlist_a);
// ListTraverse(linkedlist);
system("pause");
return 0;
}
计算机超级小白白
- 粉丝: 0
- 资源: 1
最新资源
- 【重磅,更新!】国自然管理学部标书80+份(内附清单)(2005-2021年)
- windows 自动关机小程序
- YUV视频播放器,包含图片显示,解码
- Kotlin编程语言详解及其在Android开发中的应用
- 基于C#使用Blazor+AutoGen打造多角色的会话Agent,打造有趣的智能体,通过.Net 集成AutoGen,可以在页面快速的配置不同角色的Agent进行群聊+源码(毕业设计&课程设计)
- cocos creator 3.8 抖音侧边栏复访功能
- 【重磅,更新!】中国2839个站点逐日降水数据集(0.1°/0.25°/0.5°)(1961-2022年)
- RPC远程调用示例,zeroc入门例程
- 基于python实现的多智能体强化学习(MARL)算法复现,包括QMIX,VDN,QTRAN、MAVEN+源码(毕业设计&课程设计&项目开发)
- 【重磅,更新!】教学成果、一流学科申报书范本、最全教改、课程思政(内附清单)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈