数据结构day6单链表的(复杂)操作实现
链表的反转
代码:
int list_reverse(linklist H)
{
linklist p;
linklist q;
if(H == NULL)
{
printf("H is NULL\n");
return -1;
}
if(H->next == NULL || H->next->next == NULL)
{
return 0;
}
p = H->next->next;
H->next->next = NULL;
while(p != NULL)
{
q = p;
p = p->next;
q->next = H->next;
H->next = q;
}
return 0;
}
链表求相邻两个节点最大值的第一个指针,和其相邻两个节点的最大值
代码:
linklist list_adjmax(linklist H, data_t *value)
{
linklist p, q, r;
data_t sum;
if(H == NULL)
{
printf("H is NULL\n");
return NULL;
}
if(H->next == NULL || H->next->next == NULL || H->next->next->next == NULL)
{
return H;
}
q = H->next;
p = H->next->next;
r = q;
sum = q->data + p->data;
while(p->next != NULL)
{
p = p->next;
q = q->next;
if(sum < q->data + p->data)
{
sum = q->data + p->data;
r = q;
}
}
*value = sum;
return r;
}
两个有序链表的合并
代码:
int list_merge(linklist H1, linklist H2)
{
linklist p, q, r;
if(H1 == NULL || H2 == NULL)
{
printf("H1 || H2 is NULL\n");
return -1;
}
p = H1->next;
q = H2->next;
r = H1;
H1->next = NULL;
H2->next = NULL;
while(p && q)
{
if(p->data <= q->data)
{
r->next = p;
p = p->next;
r = r->next;
r->next = NULL;
}
else
{
r->next = q;
q = q->next;
r = r->next;
r->next = NULL;
}
}
if(p == NULL)
{
r->next = q;
}
else
{
r->next = p;
}
return 0;
}
所有代码(day4-day6)
linklist.h
typedef int data_t;
typedef struct node{
data_t data;
struct node * next;
}listnode, * linklist;
linklist list_create();
int list_tail_insert(linklist H, data_t value);
int list_show(linklist H);
int list_insert(linklist H, data_t value, int pos);
linklist list_get(linklist H, int pos);
int list_delete(linklist H, int pos);
linklist list_free(linklist H);
int list_reverse(linklist H);
linklist list_adjmax(linklist H, data_t *value);
int list_merge(linklist H1, linklist H2);
linklist.c
#include <stdio.h>
#include "linklist.h"
#include <stdlib.h>
linklist list_create()
{
linklist H;
H = (linklist)malloc(sizeof(listnode));
if(H == NULL)
{
printf("malloc failed\n");
return H;
}
H->data = 0;
H->next = NULL;
return H;
}
int list_tail_insert(linklist H, data_t value)
{
linklist p;
linklist q;
if(H == NULL)
{
printf("H is empty\n");
return -1;
}
if((p = (linklist)malloc(sizeof(listnode))) == NULL)
{
printf("malloc failed\n");
return -1;
}
p->data = value;
p->next = NULL;
q = H;
while(q->next != NULL)
{
q = q->next;
}
q->next = p;
return 0;
}
int list_show(linklist H)
{
linklist p;
if(H == NULL)
{
printf("H is NULL\n");
return -1;
}
p = H;
while(p->next != NULL)
{
printf("%d ", p->next->data);
p = p->next;
}
puts(" ");
return 0;
}
int list_insert(linklist H, data_t value, int pos)
{
linklist p;
linklist q;
if(H == NULL)
{
printf("H is NULL\n");
}
p = list_get(H, pos-1);
if(p == NULL)
{
return -1;
}
if((q = (linklist)malloc(sizeof(listnode))) == NULL)
{
printf("malloc failed\n");
return -1;
}
q->data = value;
q->next = NULL;
q->next = p->next;
p->next = q;
return 0;
}
linklist list_get(linklist H, int pos)
{
linklist p;
int i;
if(H == NULL)
{
printf("H is NULL\n");
return NULL;
}
if(pos == -1)
{
return H;
}
if(pos < -1)
{
printf("pos is invalid\n");
return NULL;
}
p = H;
i = -1;
while(i < pos)
{
p = p->next;
if(p == NULL)
{
printf("pos is invalid\n");
return NULL;
}
i++;
}
return p;
}
int list_delete(linklist H, int pos)
{
linklist p;
linklist q;
if(H == NULL)
{
printf("H is NULL\n");
return -1;
}
p = list_get(H, pos-1);
if(p == NULL)
return -1;
if(p->next == NULL)
{
printf("delete pos is invalid\n");
return -1;
}
q = p->next;
p->next = q->next;
printf("free:%d\n",q->data);
free(q);
q = NULL;
return 0;
}
linklist list_free(linklist H)
{
linklist p;
if(H == NULL)
{
printf("H is NULL\n");
return NULL;
}
p = H;
printf("free:");
while(H != NULL)
{
p = H;
printf("%d ",p->data);
H = H->next;
free(p);
}
puts("");
return NULL;
}
int list_reverse(linklist H)
{
linklist p;
linklist q;
if(H == NULL)
{
printf("H is NULL\n");
return -1;
}
if(H->next == NULL || H->next->next == NULL)
{
return 0;
}
p = H->next->next;
H->next->next = NULL;
while(p != NULL)
{
q = p;
p = p->next;
q->next = H->next;
H->next = q;
}
return 0;
}
linklist list_adjmax(linklist H, data_t *value)
{
linklist p, q, r;
data_t sum;
if(H == NULL)
{
printf("H is NULL\n");
return NULL;
}
if(H->next == NULL || H->next->next == NULL || H->next->next->next == NULL)
{
return H;
}
q = H->next;
p = H->next->next;
r = q;
sum = q->data + p->data;
while(p->next != NULL)
{
p = p->next;
q = q->next;
if(sum < q->data + p->data)
{
sum = q->data + p->data;
r = q;
}
}
*value = sum;
return r;
}
int list_merge(linklist H1, linklist H2)
{
linklist p, q, r;
if(H1 == NULL || H2 == NULL)
{
printf("H1 || H2 is NULL\n");
return -1;
}
p = H1->next;
q = H2->next;
r = H1;
H1->next = NULL;
H2->next = NULL;
while(p && q)
{
if(p->data <= q->data)
{
r->next = p;
p = p->next;
r = r->next;
r->next = NULL;
}
else
{
r->next = q;
q = q->next;
r = r->next;
r->next = NULL;
}
}
if(p == NULL)
{
r->next = q;
}
else
{
r->next = p;
}
return 0;
}
test.c
#include "linklist.h"
#include <stdio.h>
int test_get(void);
int test_insert(void);
int test_delete(void);
int test_free(void);
int test_reverse(void);
int test_adjmax(void);
int test_merge(void);
int main(int argc, char *argv[])
{
test_merge();
return 0;
}
int test_get(void)
{
linklist H;
linklist p;
int value;
H = list_create();
if(H == NULL)
return -1;
printf("input:\n");
while(1)
{
scanf("%d",&value);
if(value == -1)
break;
list_tail_insert(H, value);
printf("input:\n");
}
list_show(H);
p = list_get(H, 0);
if(p != NULL)
{
printf("value:%d\n", p->data);
}
}
int test_insert(void)
{
linklist H;
linklist p;
int value;
H = list_create();
if(H == NULL)
return -1;
printf("input:\n");
while(1)
{
scanf("%d",&value);
if(value == -1)
break;
list_tail_insert(H, value);
printf("input:\n");
}
list_show(H);
list_insert(H, 100, 4);
printf("*******************************\n");
list_show(H);
if(p != NULL)
{
printf("value:%d\n", p->data);
}
}
int test_delete(void)
{
linklist H;
linklist p;
int value;
H = list_create();
if(H == NULL)
return -1;
printf("input:\n");
while(1)
{
scanf("%d",&value);
if(value == -1)
break;
list_tail_insert(H, value);
printf("input:\n");
}
list_show(H);
list_delete(H, 2);
printf("*******************************\n");
list_show(H);
}
int test_free(void)
{
linklist H;
linklist p;
int value;
H = list_create();
if(H == NULL)
return -1;
printf("input:\n");
while(1)
{
scanf("%d",&value);
if(value == -1)
break;
list_tail_insert(H, value);
printf("input:\n");
}
list_show(H);
list_delete(H, 2);
printf("*******************************\n");
list_show(H);
printf("free before:H's position:%p\n",H);
H = list_free(H);
printf("free after:H's position:%p\n",H);
}
int test_reverse(void)
{
linklist H;
linklist p;
int value;
H = list_create();
if(H == NULL)
return -1;
printf("input:\n");
while(1)
{
scanf("%d",&value);
if(value == -1)
break;
list_tail_insert(H, value);
printf("input:\n");
}
list_show(H);
list_reverse(H);
printf("*******************************\n");
list_show(H);
H = list_free(H);
}
int test_adjmax(void)
{
linklist H;
linklist p;
linklist r;
int value;
int sum;
H = list_create();
if(H == NULL)
return -1;
printf("input:\n");
while(1)
{
scanf("%d",&value);
if(value == -1)
break;
list_tail_insert(H, value);
printf("input:\n");
}
list_show(H);
r = list_adjmax(H, &sum);
if( r != NULL && r != H)
{
printf("data is %d\n", r->data);
printf("max is %d\n", sum);
}
list_show(H);
H = list_free(H);
}
int test_merge(void)
{
linklist H1;
linklist H2;
int i;
int a[] = {1, 4, 6, 8, 10};
int b[] = {2, 4, 8, 16, 20};
H1 = list_create();
H2 = list_create();
if(H1 == NULL)
{
return -1;
}
if(H2 == NULL)
{
return -1;
}
for(i = 0; i < sizeof(a)/sizeof(int); i++)
{
list_tail_insert(H1, a[i]);
}
for(i = 0; i < sizeof(b)/sizeof(int); i++)
{
list_tail_insert(H2, b[i]);
}
list_show(H1);
list_show(H2);
list_merge(H1, H2);
list_show(H1);
list_show(H2);
list_free(H1);
list_free(H2);
}