嵌入式学习第五天
1、
main.c
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "list.h"
int main()
{
link q;
q = list_creat();
list_insert(q, 10, 0);
list_insert(q, 10, 0);
list_insert(q, 10, 0);
list_insert(q, 10, 0);
list_insert(q, 10, 3);
list_delate_equal(q);
list_printf(q);
free_list(q);
return 0;
}
list.c
#include "list.h"
#include "stdlib.h"
#include "string.h"
link list_creat()
{
link p;
p = (link)malloc(sizeof(sqlink));
p->last = 0;
return p;
}
int list_insert(link q,int value,int w)
{
int i;
if ((w > q->last)||(w<0))
return -1;
if (q->last == N-1)
return 0;
for (i = q->last; i >= w; i--)
{
q->a[i + 1] = q->a[i];
}
q->a[w] = value;
q->last++;
return 1;
}
void list_printf(link p)
{
int i = 0;
if (p->last == (N - 1))
printf("list is full\n");
if (p == NULL)
printf("list is phty");
for (i = 0; i < p->last; i++)
{
printf("%d ",p->a[i]);
}
}
void list_delate(link p,int w)
{
int i;
if (p == NULL)
{
printf("list is puty");
}
for (i=w;i<p->last;i++)
{
p->a[i] = p->a[i + 1];
}
p->last--;
}
void list_delate_equal(link p)
{
int i, j;
for (i = 0; i < p->last; i++)
{
for (j = i + 1; j < p->last; j++)
{
if (p->a[i] == p->a[j])
{
list_delate(p, j);
j--;
}
}
}
}
void free_list(link p)
{
free(p);
p = NULL;
}
list.h
#ifndef __LIST_H
#define __LIST_H
#include "stdio.h"
#define N 128
typedef struct {
int a[N];
int last;
} sqlink, * link;
link list_creat();
void free_list(link p);
int list_insert(link q, int value, int w);
void list_printf(link p);
void list_delate(link p,int w);
void list_delate_equal(link p);
#endif