/*************************************************************************
> File Name: SingleLinkList.c
> Author: Wenfei6316
> Mail: Wenfei6316@163.com
> Created Time: 2018年06月16日 星期六 16时20分24秒
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define LEN 25
typedef int data_t;
typedef struct Node
{
data_t data;
struct Node *next;
}Node, *Linklist;
Linklist CreateEmptyLinklist(void);
Linklist CreateListHead(Linklist head, int len);
Linklist CreateListTail(Linklist head, int len);
void PrintList(Linklist linklist);
void ListInsert(Linklist linklist, int posi, int value);
void ListDelete(Linklist linklist, int posi, int *value);
Linklist AdjMax(Linklist linklist);
void ListReverse(Linklist linklist);
void Clearlist(Linklist linklist);
int main(int argc, const char *argv[])
{
int posi = 5;
data_t data = 113;
Linklist head1, head2, singlelist, adjmax;
head1 = CreateEmptyLinklist();
head2 = CreateEmptyLinklist();
singlelist = CreateListHead(head1, LEN);
PrintList(singlelist);
printf("\n");
head2 = CreateListTail(head2, LEN);
PrintList(head2);
printf("\n");
ListInsert(singlelist, posi, data);
PrintList(singlelist);
printf("\n");
ListDelete(singlelist, posi, &data);
PrintList(singlelist);
printf("\n");
adjmax = AdjMax(singlelist);
printf("Adjmax data is %d, next data is %d\n",
adjmax->data, adjmax->next->data);
ListReverse(singlelist);
PrintList(singlelist);
Clearlist(singlelist);
Clearlist(head2);
return 0;
}
//创建一个空表,即头节点
Linklist CreateEmptyLinklist(void)
{
Linklist head;
head = (Linklist)malloc(sizeof(Node));
if (head == NULL)
{
perror("Create head failed");
exit(EXIT_FAILURE);
}
head->data = -1;
head->next = NULL;
return head;
}
//头插法创建链表
Linklist CreateListHead(Linklist head, int len)
{
int i;
Linklist p;
srand(time(NULL));
for (i=0; i<len; i++)
{
p = (Linklist)malloc(sizeof(Node));
if (p == NULL)
{
perror("Create linklist failed");
return NULL;
}
p->data = rand() % 100;
p->next = head->next;
head->next = p;
}
return head;
}
//尾插法创建链表
Linklist CreateListTail(Linklist head, int len)
{
int i;
Linklist p, q;
q = head;
srand(time(NULL));
for (i=0; i<len; i++)
{
p = (Linklist)malloc(sizeof(Node));
if (p == NULL)
{
perror("Create linklist failed");
return NULL;
}
p->data = rand() % 100;
q->next = p;
q = p;
}
q->next = NULL;
return head;
}
//遍历链表
void PrintList(Linklist linklist)
{
int count = 0;
Linklist p;
if (linklist == NULL)
printf("Error linklist!\n");
p = linklist->next;
if (p == NULL)
printf("Empty data!\n");
while (p != NULL)
{
printf("%3d ", p->data);
if (!(++count % 5))
printf("\n");
p = p->next;
}
if (count % 5)
printf("\n");
}
//插入新节点
void ListInsert(Linklist linklist, int posi, int value)
{
int i;
Linklist p, q;
if (posi <= 0)
{
printf("Insert position can not be zero or less than zero!\n");
return;
}
p = linklist;
for (i=0; i<posi-1 && p != NULL; i++)
p = p->next;
if (p == NULL)
{
printf("Insert position error!\n");
return;
}
q = (Linklist)malloc(sizeof(Node));
if (q == NULL)
{
printf("Create linklist error!\n");
return;
}
q->data = value;
q->next = p->next;
p->next = q;
return;
}
//删除第 posi 位置节点
void ListDelete(Linklist linklist, int posi, int *value)
{
int i;
Linklist p, q;
if (posi <= 0)
{
printf("Delete position can not be zero or less than zero!\n");
return;
}
p = linklist;
for (i=0; i<posi-1 && p != NULL; i++)
p = p->next;
if (p == NULL || p->next == NULL)
{
printf("Delete position error!\n");
return;
}
q = p->next;
*value = q->data;
p->next = q->next;
free(q);
}
//寻找最大元素对
Linklist AdjMax(Linklist linklist)
{
int max = 0, ret;
Linklist p, q;
if (linklist->next==NULL || linklist->next->next==NULL)
{
printf("Linklist error!\n");
exit(EXIT_FAILURE);
}
p = linklist->next;
q = p->next;
max = p->data + q->data;
while (q->next != NULL)
{
if (max < q->data+q->next->data)
{
max = q->data + q->next->data;
p = q;
}
q = q->next;
}
return p;
}
//单链表反序
void ListReverse(Linklist linklist)
{
Linklist p, q;
if (linklist->next == NULL)
{
printf("Linklist empty!\n");
return;
}
p = linklist->next;
linklist->next = NULL;
while (p != NULL)
{
q = p;
p = p->next; //必须先,否则 p->next 直接被设置为 NULL 了
q->next = linklist->next;
linklist->next = q;
}
}
//清除链表,但不清楚链表头
void Clearlist(Linklist linklist)
{
Linklist p;
p = linklist->next;
while (p != NULL)
{
linklist->next = p->next;
free(p);
p = linklist->next;
}
printf("Free linklist OK!\n");
}
基于Linux的单链表的增删改查代码
最新推荐文章于 2023-08-10 21:53:39 发布