单链表知识
单链表的优缺点:
存储分配方式:
单链表采用链式存储结构,用一组任意的存储单元存放元素(优)
时间性能:
查找时间复杂度为O(n) ( 缺 )
插入和删除方便,时间复杂度为O(1) (优)
空间性能:
存放元素时还需要另外开辟指针域空间(缺点)
动态分配空间,不需要限定长度(优点)
单链表相关操作
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);//head 尾部插入
int list_insert(linklist H, data_t value, int pos);// 按位置插入
linklist list_get(linklist H, int pos); // 获得某个节点
int list_delete(linklist H, int pos); // 删除节点
int list_show(linklist H); // 遍历链表
linklist list_free(linklist H); // 释放链表
int list_reverse(linklist H); // 倒置
linklist list_adjmax(linklist H,data_t *sum);//相邻两个结点最大值
int list_merge(linklist H1, linklist H2); // 合并
linklist list_sort(linklist H); // 排序
linklist.c
#include <stdio.h>
#include <stdlib.h>
#include "linklist.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;
}
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_insert(linklist H, data_t value, int pos)
{
linklist p;
linklist q;
if(H == NULL){
printf("H is NULL\n");
return -1;
}
//1 locate node p(pos-1)
p = list_get(H, pos-1);
if(p == NULL){
printf("p is NULL\n");
return -1;
}
//2 malloc new node q
if((q = (linklist)malloc(sizeof(listnode))) == NULL){
printf("malloc failed\n");
return -1;
}
q->data = value;
q->next = NULL;
//3 insert
q->next = p->next;
p->next = q;
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_tail_insert(linklist H,data_t value)//head
{
linklist p;
linklist q;
if(H == NULL){
printf("H is NULL\n");
return -1;
}
//1 new node p
p = (linklist)malloc(sizeof(listnode));
if(p == NULL){
printf("malloc failed\n");
return -1;
}
p->data = value;
p->next = NULL;
//2 locate tail node
q = H;
while(q->next != NULL){
q = q->next;
}
//3 insert
q->next = p;
return 0;
}
int list_delete(linklist H,int pos)
{
linklist p;
linklist q;
//1
if(H == NULL){
printf("H is NULL\n");
return -1;
}
//2 locate prior
p = list_get(H, pos-1);
if(p == NULL){
printf("p is NULL\n");
return -1;
}
if(p->next == NULL){
printf("delete pos is invalid\n");
return -1;
}
//3 update list
q = p->next;
p->next = q->next;// p->next = p->next->next;
//4 free
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);
}
printf("\n");
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 q,p,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; // p = q->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 = q;
}
return 0;
}
linklist list_sort(linklist H)
{
linklist q,p;
data_t tmp;
if(H == NULL){
printf("H is NULL\n");
return NULL;
}
if(H->next == NULL || H->next->next == NULL){
return NULL;
}
q = H->next;
while(q != NULL){
p = q->next;
while(p != NULL){
if(q->data > p->data){
tmp = q->data;
q->data = p->data;
p->data = tmp;
}
p = p->next;
}
q = q->next;
}
q = H;
return H;
}
test.c
#include <stdio.h>
#include "linklist.h"
void test_get();
void test_insert();
void test_delete();
void test_free();
void test_reverse();
void test_adjmax();
void test_merge();
void test_sort();
int main(int argc,const char *argv[])
{
//test_free();
//test_reverse();
//test_adjmax();
//test_merge();
test_sort();
return 0;
}
void test_get()
{
linklist H;
int value;
linklist p;
H = list_create();
if(H == NULL){
printf("H is NULL\n");
return ;
}
printf("input:");
while(1){
scanf("%d",&value);
if(value == -1){
break;
}
list_tail_insert(H, value);
printf("input:");
}
list_show(H);
p = list_get(H, 5);//1,3,5,7
printf("value=%d\n",p->data);
return ;
}
void test_insert()
{
linklist H;
int value;
linklist p;
H = list_create();
if(H == NULL){
printf("H is NULL\n");
return ;
}
printf("input:");
while(1){
scanf("%d",&value);
if(value == -1){
break;
}
list_tail_insert(H, value);
printf("input:");
}
list_show(H);
list_insert(H, 100,4);//1,3,5,7
list_show(H);
}
void delete()
{
linklist H;
int value;
linklist p;
H = list_create();
if(H == NULL){
printf("H is NULL\n");
return ;
}
printf("input:");
while(1){
scanf("%d",&value);
if(value == -1){
break;
}
list_tail_insert(H, value);
printf("input:");
}
list_show(H);
list_delete(H,-2);//1,3,5,7
list_show(H);
}
void test_free()
{
linklist H;
int value;
linklist p;
H = list_create();
if(H == NULL){
printf("H is NULL\n");
return ;
}
printf("input:");
while(1){
scanf("%d",&value);
if(value == -1){
break;
}
list_tail_insert(H, value);
printf("input:");
}
list_show(H);
printf("H = %p\n",H);
H = list_free(H);
printf("H = %p\n",H);
list_show(H);
}
void test_reverse()
{
linklist H;
int value;
linklist p;
H = list_create();
if(H == NULL){
printf("H is NULL\n");
return ;
}
printf("input:");
while(1){
scanf("%d",&value);
if(value == -1){
break;
}
list_tail_insert(H, value);
printf("input:");
}
list_show(H);
list_reverse(H);
list_show(H);
list_free(H);
}
void test_adjmax()
{
linklist H;
linklist r;
int value;
int sum;
linklist p;
H = list_create();
if(H == NULL){
printf("H is NULL\n");
return ;
}
printf("input:");
while(1){
scanf("%d",&value);
if(value == -1){
break;
}
list_tail_insert(H, value);
printf("input:");
}
list_show(H);
r = list_adjmax(H,&sum);
if(r != NULL && r != H){
printf("data=%d ,sum=%d\n",r->data,sum);
}
list_show(H);
list_free(H);
}
void test_merge()
{
linklist H1,H2;
int a[] = {1,3,5,7,9,11};
int b[] = {2,3,4,10,12,32,44};
int i;
H1 = list_create();
if(H1 == NULL){
return;
}
H2 = list_create();
if(H2 == NULL){
return;
}
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);
printf("merge:\n");
list_show(H1);
list_show(H2);
list_free(H1);
list_free(H2);
}
void test_sort()
{
linklist H;
int value;
linklist p;
H = list_create();
if(H == NULL){
printf("H is NULL\n");
return ;
}
printf("input:");
while(1){
scanf("%d",&value);
if(value == -1){
break;
}
list_tail_insert(H, value);
printf("input:");
}
list_show(H);
list_sort(H);
list_show(H);
list_free(H);
}