学完双链表后写的,欢迎各位读者纠错。
双链表的定义
typedef struct Lnode
{
int data;
struct Lnode* next;
struct Lnode* pre;
}Lnode, * pLnode;
typedef struct Linklist
{
int len;
pLnode head;
}Linklist, * pLinklist;
我把申请空间写成了一个函数以后再用的时候就不用再写这句话了,直接调用这个函数就行了。
pLnode makenode()
{
pLnode s = (pLnode)malloc(sizeof(Lnode));
if (!s)exit(-1);
return s;
}
然后初始化链表
void Initlist(pLinklist L)
{
pLnode p = makenode();
p->next = NULL;
p->pre = NULL;
L->head = p;
L->len = 0;
}
头插法
void pushhead(pLinklist L, int e)
{
assert(L != NULL);
pLnode p = makenode();
if (L->len == 0)
{
p->data = e;
p->next = L->head->next;
p->pre = L->head;
L->head->next = p;
L->len++;
}
else
{
p->data = e;
p->pre = L->head;
p->next = L->head->next;
L->head->next->pre = p;
L->head->next = p;
L->len++;
}
}
尾插法
void pushback(pLinklist L, int e)
{
assert(L != NULL);
pLnode p = makenode();
pLnode s = makenode();
s = L->head;
while (s->next != NULL)
{
s = s->next;
}
p->data = e;
p->next = NULL;
p->pre = s;
s->next = p;
L->len++;
}
头删法
void delhead(pLinklist L)
{
assert(L != NULL);
if (L->head->next->next == NULL)
{
free(L->head->next);
L->head->next = NULL;
L->len--;
}
else
{
L->head->next = L->head->next->next;
L->head->next->pre = L->head;
L->len--;
}
}
尾删法
void delback(pLinklist L)
{
assert(L != NULL);
pLnode p ;
p = L->head;
while (p->next != NULL)
{
p = p->next;
}
p->pre->next = NULL;
L->len--;
free(p);
p = NULL;
}
交换两个数据的值
void exchange(pLinklist L, int s1,int s2)
{
assert(L != NULL);
pLnode p ;
pLnode s ;
p = L->head->next;
s = L->head->next;
for (int i = 0; i < s1-1; i++)
{
p = p->next;
}
for (int i = 0; i < s2-1; i++)
{
s = s->next;
}
int e;
e = p->data;
p->data = s->data;
s->data = e;
}
排序
pLinklist sort(pLinklist L)
{
assert(L != NULL);
pLnode p ;
int s=0;
for (int i = 0; i < (L->len) - 1; i++)
{
p = L->head->next;
for (int j = 0; j < (L->len) - 1; j++)
{
if (p->data > p->next->data)
{
s = p->data;
p->data = p->next->data;
p->next->data = s;
}
p = p->next;
}
}
return L;
}
打印到屏幕上
void show(pLinklist L)
{
assert(L != NULL);
pLnode p ;
p = L->head->next;
while (p)
{
if (p->next == NULL)
{
printf("%d\n", p->data);
}
else {
printf("%d->", p->data);
}
p = p->next;
}
}
输入
void input(pLinklist L)
{
assert(L != NULL);
int s, e;
scanf("%d", &s);
for (int i = 0; i < s; i++)
{
scanf("%d", &e);
pushback(L, e);
}
}
清空链表
void clearlist(pLinklist L)
{
assert(L != NULL);
pLnode p;
p = L->head->next;
while (p->next != NULL)
{
p = p->next;
}
while (p != L->head)
{
p = p->pre;
free(p->next);
p->next = NULL;
L->len--;
}
}
销毁链表
void Destorylist(pLinklist L)
{
assert(L != NULL);
clearlist(L);
free(L->head);
L->head = NULL;
}
主函数
int main()
{
Linklist L;
Initlist(&L);
input(&L);
show(&L);
delback(&L);
sort(&L);
show(&L);
exchange(&L, 1, 4);
show(&L);
clearlist(&L);
Destorylist(&L);
return 0;
}
整体代码
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct Lnode
{
int data;
struct Lnode* next;
struct Lnode* pre;
}Lnode, * pLnode;
typedef struct Linklist
{
int len;
pLnode head;
}Linklist, * pLinklist;
pLnode makenode()
{
pLnode s = (pLnode)malloc(sizeof(Lnode));
if (!s)exit(-1);
return s;
}
void Initlist(pLinklist L)
{
pLnode p = makenode();
p->next = NULL;
p->pre = NULL;
L->head = p;
L->len = 0;
}
void pushhead(pLinklist L, int e)
{
assert(L != NULL);
pLnode p = makenode();
if (L->len == 0)
{
p->data = e;
p->next = L->head->next;
p->pre = L->head;
L->head->next = p;
L->len++;
}
else
{
p->data = e;
p->pre = L->head;
p->next = L->head->next;
L->head->next->pre = p;
L->head->next = p;
L->len++;
}
}
void pushback(pLinklist L, int e)
{
assert(L != NULL);
pLnode p = makenode();
pLnode s = makenode();
s = L->head;
while (s->next != NULL)
{
s = s->next;
}
p->data = e;
p->next = NULL;
p->pre = s;
s->next = p;
L->len++;
}
void delhead(pLinklist L)
{
assert(L != NULL);
if (L->head->next->next == NULL)
{
free(L->head->next);
L->head->next = NULL;
L->len--;
}
else
{
L->head->next = L->head->next->next;
L->head->next->pre = L->head;
L->len--;
}
}
void delback(pLinklist L)
{
assert(L != NULL);
pLnode p ;
p = L->head;
while (p->next != NULL)
{
p = p->next;
}
p->pre->next = NULL;
L->len--;
free(p);
p = NULL;
}
void exchange(pLinklist L, int s1,int s2)
{
assert(L != NULL);
pLnode p ;
pLnode s ;
p = L->head->next;
s = L->head->next;
for (int i = 0; i < s1-1; i++)
{
p = p->next;
}
for (int i = 0; i < s2-1; i++)
{
s = s->next;
}
int e;
e = p->data;
p->data = s->data;
s->data = e;
}
pLinklist sort(pLinklist L)
{
assert(L != NULL);
pLnode p ;
int s=0;
for (int i = 0; i < (L->len) - 1; i++)
{
p = L->head->next;
for (int j = 0; j < (L->len) - 1; j++)
{
if (p->data > p->next->data)
{
s = p->data;
p->data = p->next->data;
p->next->data = s;
}
p = p->next;
}
}
return L;
}
void show(pLinklist L)
{
assert(L != NULL);
pLnode p ;
p = L->head->next;
while (p)
{
if (p->next == NULL)
{
printf("%d\n", p->data);
}
else {
printf("%d->", p->data);
}
p = p->next;
}
}
void input(pLinklist L)
{
assert(L != NULL);
int s, e;
scanf("%d", &s);
for (int i = 0; i < s; i++)
{
scanf("%d", &e);
pushback(L, e);
}
}
void clearlist(pLinklist L)
{
assert(L != NULL);
pLnode p;
p = L->head->next;
while (p != NULL)
{
pLnode n;
n = p->next;
free(p);
p = n;
}
L->head->next = NULL;
}
void Destorylist(pLinklist L)
{
assert(L != NULL);
clearlist(L);
free(L->head);
L->head = NULL;
}
int main()
{
Linklist L;
Initlist(&L);
input(&L);
show(&L);
delback(&L);
sort(&L);
show(&L);
exchange(&L, 1, 4);
show(&L);
clearlist(&L);
Destorylist(&L);
return 0;
}