0% found this document useful (0 votes)
0 views

link_list_test

The document contains C code that implements data structures and functions for handling a sequential list (SqList) and a linked list (LinkList). It includes functions for creating, initializing, destroying, inserting, and printing these lists, as well as computing absolute values and finding the minimum of three integers. The main function demonstrates the usage of these list operations with an example array.

Uploaded by

lijiade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

link_list_test

The document contains C code that implements data structures and functions for handling a sequential list (SqList) and a linked list (LinkList). It includes functions for creating, initializing, destroying, inserting, and printing these lists, as well as computing absolute values and finding the minimum of three integers. The main function demonstrates the usage of these list operations with an example array.

Uploaded by

lijiade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>

#define MAXSIZE 100

typedef int ElemType;

typedef struct
{
ElemType data[MAXSIZE];
int length;
} SqList;

typedef struct LinkNode


{
ElemType data;
struct LinkNode* next;
} LinkNode, *LinkList;

bool CreateSqList(SqList& L, ElemType arr[], int len)


{
if (len > MAXSIZE)
return false;

L.length = len;
for (int i = 0; i < len; i++)
{
L.data[i] = arr[i];
}

return true;
}

bool InitList(LinkList& L)
{
L = (LinkNode*)malloc(sizeof(LinkNode));
L->next = NULL;
return true;
}

bool DestroyList(LinkList& L)
{
LinkNode* p = L;
LinkNode* pNext = p->next;

while (pNext != NULL)


{
free(p);
p = pNext;
pNext = p->next;
}

free(p);
return true;
}

int LinkLen(LinkList L)
{
int len = 0;
LinkNode* p = L;

while (p->next != NULL)


{
p = p->next;
len++;
}

return len;
}

bool ListHeadInsert(LinkList& L, ElemType arr[], int len)


{
LinkNode* s;

L = (LinkNode*)malloc(sizeof(LinkNode));
L->next = NULL;

for (int i = 0; i < len; i++)


{
s = (LinkNode*)malloc(sizeof(LinkNode));
s->data = arr[i];
s->next = L->next;
L->next = s;
}

return true;
}

bool ListTailInsert(LinkList& L, ElemType arr[], int len)


{
LinkNode* p, *q;

L = (LinkNode*)malloc(sizeof(LinkNode));
L->next = NULL;
p = L;

for (int i = 0; i < len; i++)


{
q = (LinkNode*)malloc(sizeof(LinkNode));
q->data = arr[i];
p->next = q;
p = p->next;
}

p->next = NULL;
return true;
}

void PrintSqList(const SqList L)


{
for (int i = 0; i < L.length; i++)
printf("%d ", L.data[i]);
printf("\n");
}

void PrintLinkList(const LinkList& L)


{
LinkNode* s = L->next;
while (s != NULL)
{
printf("%d ", s->data);
s = s->next;
}

printf("\n");
}

void PrintArr(const ElemType arr[], int len)


{
for (int i = 0; i < len; i++)
printf("%d ", arr[i]);
printf("\n");
}

int AbsCompute(int x)
{
if (x < 0)
return (-x);
else
return x;
}

int FindMinInTriple(int x, int y, int z)


{
int min = x;
if (y < min)
min = y;
if (z < min)
min = z;
return min;
}

bool func(LinkList& L)
{
if (L == NULL)
{
printf("缺少头节点\n");
return false;
}
if (L->next == NULL)
{
printf("链表为空表\n");
return false;
}
if (L->next->next == NULL)
{
printf("链表只有一个带数据的节点,逆置前后不变\n");
return true; // 直接返回即可
}

LinkNode *p, *pre, *pNext;


p = L->next;
pNext = p->next;
p->next = NULL;

while (pNext != NULL)


{
pre = p;
p = pNext;
pNext = p->next;
p->next = pre;
}

L->next = p;

return true;
}

int main()
{
// ElemType arr[] = { 1, 2, 2, 2, 2, 3, 3, 4, 4, 5 };
ElemType arr[] = { 4, 2, 3, 4, 3, 6, 7, 3, 11, 3, 1 };
// ElemType arr[] = { 1 };
int len = sizeof(arr) / sizeof(arr[0]);

LinkList L;
if (ListTailInsert(L, arr, len))
PrintLinkList(L);

func(L);

PrintLinkList(L);

// PrintArr(arr, len);

// int minMissNum = func(arr, len);


// PrintSqList(L);
// PrintArr(arr, len);
// printf("minMissNum=%d\n", minMissNum);

return 0;
}

You might also like