1.List

sqlist

sqlist.h

typedef int data_t;

#define N 128

typedef struct
{
  data_t data[N];
  int last;

} sqlist, *sqlink;

sqlink list_create();
int list_clear(sqlink L);
int list_empty(sqlink L);
int list_length(sqlink L);
int list_locate(sqlink L, data_t value);
int list_insert(sqlink L, data_t value, int pos);
int list_show(sqlink L);
int list_free(sqlink L);
int list_delete_index(sqlink L, int pos);
int list_merge(sqlink L1, sqlink L2);
int list_purge(sqlink L);

sqlist.c

#include "sqlist.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

sqlink list_create()
{
  // malloc
  sqlink L;
  L = (sqlink)malloc(sizeof(sqlist));
  if (NULL == L)
  {
    printf("list malloc failed\n");
    return L;
  }
  // initialize
  memset(L, 0, sizeof(sqlist));
  L->last = -1;
  // return
  return L;
}

/**
 *0:success
 *1:return
 *-1:failed
 **/
int list_clear(sqlink L)
{
  if (NULL == L)
  {
    return -1;
  }
  memset(L, 0, sizeof(sqlist));
  L->last = -1;
  return 0;
}

int list_empty(sqlink L)
{
  if (L->last == -1)
  {
    return 1;
  }
  return 0;
}

int list_length(sqlink L)
{
  if (NULL == L)
  {
    return -1;
  }
  return (L->last + 1);
}

int list_locate(sqlink L, data_t value)
{
  int i;
  for (i = 0; i <= L->last; i++)
  {
    if (L->data[i] == value)
    {
      return i;
    }
  }
  return -1;
}
int list_insert(sqlink L, data_t value, int pos)
{
  printf("list insert....\n");
  int i;
  // full
  if (L->last == N - 1)
  {
    printf("list is full");
    return -1;
  }
  // check param pos
  if (pos >= 0 && pos <= L->last + 1)
  {
  }
  else
  {
    printf("pos input error!");
    return -1;
  }
  //
  //
  // move
  for (i = L->last; i >= pos; i--)
  {
    L->data[i + 1] = L->data[i];
  }

  // update last

  L->data[pos] = value;
  L->last++;
  return 0;
}

int list_show(sqlink L)
{
  int i;
  if (NULL == L)
    return -1;
  if (L->last == -1)
    printf("list is empty\n");
  printf("list_show.....\n");
  for (i = 0; i <= L->last; i++)
  {
    printf("%d ", L->data[i]);
  }
  puts("");
  return 0;
}

int list_free(sqlink L)
{
  if (NULL == L)
  {
    return -1;
  }
  printf("list free...\n");
  free(L);
  L = NULL;
  return 0;
}

int list_delete_index(sqlink L, int pos)
{
  if (NULL == L)
  {
    return -1;
  }
  int i;
  printf("list delete index...\n");
  if (list_empty(L) == 1)
  {
    printf("list is empty,delete error\n");
    return -1;
  }

  if (pos < 0 || pos > L->last)
  {
    printf("pos error,delete error\n");
    return -1;
  }
  // move
  for (i = pos + 1; i <= L->last; i++)
  {
    L->data[i - 1] = L->data[i];
  }

  // update
  L->last--;
  return 0;
}

int list_merge(sqlink L1, sqlink L2)
{
  if (NULL == L1 || NULL == L2)
  {
    printf("L1 or L2 is NULL\n");
    return -1;
  }
  int i = 0;
  int ret;
  while (i <= L2->last)
  {
    ret = list_locate(L1, L2->data[i]);
    if (ret == -1)
    {
      if (list_insert(L1, L2->data[i], L1->last + 1) == -1)
        return -1;
    }
    i++;
  }
  return 0;
}

int list_purge(sqlink L)
{
  if (NULL == L)
  {
    return -1;
  }
  printf("list purge ....\n");
  if (list_empty(L) == 1)
  {
    printf("list is empty,purge error\n");
    return -1;
  }
  if (L->last == 0)
  {
    return 0;
  }
  int i, j;
  i = 1;
  while (i <= L->last)
  {
    j = i - 1;
    while (j >= 0)
    {
      if (L->data[i] == L->data[j])
      {
        list_delete_index(L, i);
        break;
      }
      else
      {
        j--;
      }
    }
    if (j < 0)
    {
      i++;
    }
  }

  return 0;
}

demo.c

#include "sqlist.h"
#include<stdio.h>

void test_insert();
void test_delete();
void test_merge();
void test_purge();
void job_1();
void job_2();
int main(){
  job_1();
  printf("*****************\n");
  job_2();
  return 0;

}

void job_1(){
  sqlink L;
  L = list_create();
  if(NULL == L){
	printf("L is NULL\n");
	return;
  }
  list_insert(L,1,0);
  list_insert(L,1,1);
  list_insert(L,1,2);
  list_insert(L,2,3);
  list_insert(L,3,4);
  list_insert(L,4,5);
  list_insert(L,4,6);
  list_insert(L,5,7);
  list_insert(L,6,8);
  list_show(L);
  list_purge(L);
  list_show(L);
  list_free(L);
}

void job_2(){
  sqlink La,Lb;
  La = list_create();
  Lb = list_create();
  if(NULL == La || NULL == Lb){
   	printf("L is NULL\n");
	return;
  }
  list_insert(La,1,0);
  list_insert(La,2,1);
  list_insert(La,3,2);
  list_insert(La,4,3);
  list_insert(Lb,4,0);
  list_insert(Lb,5,1);
  list_insert(Lb,6,2);
  list_insert(Lb,7,3);
  list_show(La);
  list_show(Lb);
  list_merge(La,Lb);
  list_show(La);
  list_show(Lb);
  list_free(La);
  list_free(Lb);
}

void test_insert(){

  sqlink L;
  L = list_create();
  if(NULL == L){
	printf("L is NULL\n");
	return;
  }
  list_insert(L,10,0);
  list_insert(L,20,0);
  list_insert(L,30,0);
  list_insert(L,40,0);
  list_insert(L,50,0);
  list_show(L);
  list_free(L);
}

void test_delete(){

  sqlink L;
  L = list_create();
  if(NULL == L){
	printf("L is NULL\n");
	return;
  }
  list_insert(L,10,0);
  list_insert(L,20,0);
  list_insert(L,30,0);
  list_insert(L,40,0);
  list_insert(L,50,0);
  list_show(L);
  list_delete_index(L,9);
  list_show(L);
  
  list_free(L);
}


void test_merge(){

  sqlink L1,L2;
  L1 = list_create();
  L2 = list_create();
  if(NULL == L1 || NULL == L2){
    printf("L1 or L2 is NULL\n");
	return;
  }

  list_insert(L1,10,0);
  list_insert(L1,20,0);
  list_insert(L1,30,0);
  list_insert(L2,40,0);
  list_insert(L2,20,0);
  list_show(L1);
  list_show(L2);
  list_merge(L1,L2);
  list_show(L1);
  list_show(L2);
  list_free(L1);
  list_free(L2);
}

void test_purge(){

  sqlink L1;
  L1 = list_create();
  if(NULL == L1){
    printf("L1 is NULL\n");
	return;
  }

  list_insert(L1,10,0);
  list_insert(L1,10,0);
  list_insert(L1,10,0);
  list_show(L1);
  list_purge(L1);
  list_show(L1);
  list_free(L1);
}


linklist

linklist.h

typedef int data_t;

typedef struct node
{
  data_t data;
  struct node *next;
} linknode, *linklist;

linklist list_create();
int list_tail_insert(linklist H, data_t value);
linklist list_get(linklist H, int pos);
int list_insert(linklist H, data_t value, int pos);
int list_show(linklist H);
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);
int list_sort(linklist H);
int list_sort2(linklist H);
int list_delete_pointer(linklist H, linklist pointer);

linklist.c

#include <stdio.h>
#include "linklist.h"
#include <stdlib.h>

linklist list_create()
{
  printf("create linklist....\n");
  linklist H;
  H = (linklist)malloc(sizeof(linknode));
  if (NULL == H)
  {
    printf("malloc fail\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 (NULL == H)
  {
    printf("H is NULL \n");
    return -1;
  }
  if ((p = (linklist)malloc(sizeof(linknode))) == NULL)
  {
    printf("malloc failed\n");
  }
  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 (NULL == H)
  {
    printf("H is NULL\n");
    return -1;
  }
  p = H;
  while (p->next != NULL)
  {
    printf("%d ", p->next->data);
    p = p->next;
  }
  puts("");
  return 0;
}

linklist list_get(linklist H, int pos)
{
  linklist p;
  int i;
  if (NULL == H)
  {
    printf("H is NULL\n");
    return NULL;
  }
  if (pos < -1)
  {
    printf("pos is invalid\n");
    return NULL;
  }

  if (pos == -1)
  {

    return H;
  }
  p = H;
  i = -1;
  while (i < pos)
  {
    p = p->next;
    if (p == NULL)
    {
      printf("pos not in\n");
      return NULL;
    }
    i++;
  }
  return p;
}

int list_insert(linklist H, data_t value, int pos)
{
  linklist p;
  linklist q;
  if (NULL == H)
  {
    printf("H is NULL\n");
    return -1;
  }
  p = list_get(H, pos - 1);
  if (p == NULL)
  {
    return -1;
  }
  if ((q = (linklist)malloc(sizeof(linknode))) == NULL)
  {

    printf("malloc failed\n");
    return -1;
  }
  q->data = value;
  q->next = NULL;

  q->next = p->next;
  p->next = q;
  return 0;
}

int list_delete(linklist H, int pos)
{
  // check
  if (NULL == H)
  {
    printf("H is NULL\n");
    return -1;
  }
  linklist q, p;
  // get
  p = list_get(H, pos - 1);

  // check
  if (NULL == p)
  {
    return -1;
  }
  if (NULL == p->next)
  {
    printf("delete pos is invalid\n");
    return -1;
  }
  // move
  q = p->next;
  p->next = q->next;
  // free
  free(q);
  q = NULL;
  return 0;
}

linklist list_free(linklist H)
{
  printf("free loading...\n");
  // check
  if (NULL == H)
  {
    printf("H is NULL\n");
    return NULL;
  }
  linklist p, q;
  p = H;
  // printf("H free before = %p\n",H);
  // printf("=======================\n");
  // free
  while (p != NULL)
  {
    q = p;
    // printf("q free before = %p\n",q);
    // printf("p free before = %p\n",p);
    // printf("=======================\n");
    free(q);
    // printf("q free after = %p\n",q);
    // printf("p free after = %p\n",p);
    // printf("=======================\n");
    p = p->next;
  }
  free(p);
  p = NULL;
  // printf("p free after = %p\n",p);
  // printf("=======================\n");
  free(H);
  H = NULL;
  // printf("H free after = %p\n",H);
  // printf("=======================\n");
  return NULL;
}

int list_reverse(linklist H)
{
  if (NULL == H)
  {
    printf("H is NULL\n");
    return -1;
  }
  if (H->next == NULL || H->next->next == NULL)
  {
    return 0;
  }
  linklist p, q;
  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)
{
  if (NULL == H)
  {
    printf("H is NULL\n");
    return NULL;
  }
  if (NULL == H->next || NULL == H->next->next || NULL == H->next->next->next)
  {
    return NULL;
  }

  linklist p, q, r;
  data_t sum;
  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)
{
  if (NULL == H1 || NULL == H2)
  {
    printf("H1,H2 is NULL\n");
    return -1;
  }
  linklist p, q, r;
  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;
}
int list_sort(linklist H)
{
  if (NULL == H)
  {
    printf("H is NULL\n");
    return -1;
  }
  linklist p, q;
  p = H->next;
  data_t temp;
  while (p)
  {
    q = p->next;
    while (q)
    {
      if (p->data > q->data)
      {
        temp = p->data;
        p->data = q->data;
        q->data = temp;
      }
      q = q->next;
    }
    p = p->next;
  }
  return 0;
}

int list_sort2(linklist H)
{
  if (NULL == H)
  {
    printf("H is NULL\n");
    return -1;
  }
  linklist p, q, r;
  p = H->next;
  H->next = NULL;
  r = H;
  while (p != NULL)
  {
    q = p;
    p = p->next;
    r = H;
    while (r->next && q->data > r->next->data)
    {
      r = r->next;
    }
    q->next = r->next;
    r->next = q;
  }

  return 0;
}
int list_delete_pointer(linklist H, linklist pointer)
{
  if (NULL == H)
  {
    printf("H is NULL\n");
    return -1;
  }
  linklist p, a;
  p = H->next;
  // before pointer == p
  while (p != NULL && p->next != NULL)
  {
    if (p->next->data == pointer->data)
      break;
    p = p->next;
  }
  a = p->next; // A pointer
  p->next = a->next;
  free(a);
  a = NULL;
  return 0;
}

demo.c

#include <stdio.h>
#include "linklist.h"

linklist list_create();
int list_delete_pointer(linklist H, linklist pointer);

void test_get();
void test_create();
void test_insert();
void test_delete();
void test_free();
void test_reverse();
void test_adjMax();
void test_merge();
void test_sort();
void test_delete_pointer();

int main()
{
  test_sort();
  return 0;
}

void test_sort()
{
  linklist H;
  int value;
  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_sort2(H);
  list_show(H);
  list_free(H);
}
void test_delete_pointer()
{
  linklist H;
  int value;
  linknode node;
  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:");
  }
  printf("************\n");
  printf("please delete node data\n");
  printf("input:");
  scanf("%d", &value);
  node.data = value;
  node.next = NULL;
  list_show(H);
  list_delete_pointer(H, &node);
  list_show(H);
  list_free(H);
}

void test_merge()
{
  linklist H1, H2;
  int a[] = {1, 4, 6, 8, 10};
  int b[] = {2, 4, 16, 18, 30};
  int i;
  H1 = list_create();
  H2 = list_create();
  if (H1 == NULL)
  {
    printf("H1 is NULL\n");
    return;
  }
  if (H2 == NULL)
  {
    printf("H2 is NULL\n");
    return;
  }
  printf("input:\n");
  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);
}
void test_adjMax()
{
  linklist H;
  int value;
  linklist r;
  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);
  data_t sum;
  r = list_adjMax(H, &sum);
  if (r != NULL && r != H)
  {
    printf("data = %d,sum = %d\n", r->data, sum);
  }
  list_free(H);
}

void test_reverse()
{
  linklist H;
  int value;
  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_free()
{
  linklist H;
  int value;
  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);
  H = list_free(H);
  list_show(H);
}

void test_delete()
{
  linklist H;
  int value;
  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);
  list_show(H);
}

void test_insert()
{
  linklist H;
  int value;
  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, 10, 1);
  list_show(H);
}
void test_create()
{
  linklist H;
  int value;
  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);
}
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("intput:");
  }
  list_show(H);
  p = list_get(H, 4);
  if (p != NULL)
    printf("value=%d\n", p->data);
}

makefile

CC = gcc

SRC = $(wildcard *.c)
OBJ = $(patsubst %.c, %.o, $(SRC))

test:$(SRC)
	$(CC)	$^	-o	$@	-Wall

.PHONY:clean
clean:
	rm	./*.o
  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值