#ifndef __1_H__
#define __1_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXSIZE 10
typedef int datatype;
typedef struct
{ int len;
datatype data[MAXSIZE];
}DD,*dd;
void output(dd list);
int insert_t(dd list,datatype e);
int empty_l(dd list);
int full_l(dd list);
int delete_t(dd list);
int search_s(dd list,int sub);
dd create();
int chang_l(dd list,int sub,datatype e);
int insert_s(dd list,int sub,datatype e);
int delete_s(dd list,int sub);
int search_data(dd list,datatype e);
int chang_data(dd list,datatype e,datatype key);
int delete_data(dd list,datatype e);
int delete_cf(dd list);
void sort(dd list);
int insert_next(dd list, int sub,datatype e);
void merge_sort(dd list, dd list1,dd list2);
dd free_l(dd list);
#endif
#include "1.h"
int main(int argc, const char *argv[])
{
dd list = create();
int n;
datatype e;
printf("input u want insert number\n");
scanf(" %d",&n);
for(int i = 0; i < n; i++)
{
printf("input data\n");
scanf("%d",&e);
insert_t(list,e);
}
output(list);
int k;
printf("input want search_s num");
scanf("%d",&k);
search_s(list,k);
printf("u want chang_l number\n");
scanf("%d",&k);
printf("u want chang_l data\n");
scanf("%d",&e);
chang_l(list,k,e);
output(list);
printf("u want insert_s number\n");
scanf("%d",&k);
printf("u want insert_s data\n");
scanf("%d",&e);
insert_s(list,k,e);
output(list);
printf("u want delete_s number\n");
scanf("%d",&k);
delete_s(list,k);
output(list);
printf("u want search_data data \n");
scanf("%d",&e);
int flag = search_data(list,e);
printf("the data is the %d number\n",flag);
datatype key;
printf("u want chang_data data \n");
scanf("%d",&e);
printf("u want chang_data new data \n");
scanf("%d",&key);
chang_data(list,e,key);
output(list);
printf("want delete num\n");
scanf("%d",&n);
for(int i = 0; i < n; i++)
{
delete_t(list);
}
output(list);
delete_cf(list);
output(list);
printf("input u want delete_data\n");
scanf("%d",&e);
delete_data(list,e);
output(list);
int k = 0;
printf("input u want insert_next number\n");
scanf("%d",&k);
printf("input u want insert_next data\n");
scanf("%d",&e);
insert_next(list,k,e);
output(list);
DD list3 = {5,3,9,5,7,1};
sort(&list3);
DD list2;
list2.len = 0;
DD list1 = {5,3,9,5,7,1};
sort(&list1);
output(&list1);
output(&list3);
merge_sort(&list3,&list1,&list2);
output(&list2);
list = free_l(list);
if(!list) printf("yes");
return 0;
}
#include "1.h"
dd create()
{
dd list = (dd) malloc (sizeof (DD));
if(!list) return NULL;
list->len = 0;
return list;
}
int full_l(dd list)
{
return list->len == MAXSIZE?-1:0;
}
int empty_l(dd list)
{
return list->len == 0?-1:0;
}
int insert_t(dd list,datatype e)
{
if(list == NULL || full_l(list))
{
printf("insert_t failed\n");
return -1;
}
list->data[list->len] = e;
list->len++;
return 0;
}
void output(dd list)
{
if(list == NULL||empty_l(list))
{
printf("output failed\n");
return;
}
for(int i = 0; i < list->len; i++)
printf("%d\t",list->data[i]);
putchar(10);
}
int delete_t(dd list)
{
if(list == NULL || empty_l(list))
{
printf("delete_t failed\n");
return -1;
}
list->len --;
return 0;
}
int search_s(dd list,int sub)
{
if(list == NULL || empty_l(list) || sub<0 || sub >= list->len)
{
printf("search_s failed\n");
return -1;
}
printf("the data is %d\n",list->data[sub]);
return 0;
}
int chang_l(dd list,int sub,datatype e)
{
if(list == NULL || empty_l(list) || sub < 0 || sub >= list->len)
{
printf("chang_l failed\n");
return -1;
}
list->data[sub] = e;
return 0;
}
int insert_s(dd list,int sub,datatype e)
{
if(!list || full_l(list) || sub < 0 || sub > list->len)
{
printf("insert_s failed\n");
return -1;
}
for(int i = (list->len-1); i >= sub; i--)
list->data[i+1] = list->data[i];
list->data[sub] = e;
list->len++;
return 0;
}
int delete_s(dd list,int sub)
{
if(list == NULL || empty_l(list) || sub < 0 || sub >= list->len)
{
printf("delete_s failed\n");
return -1;
}
for(int i = sub;i < list->len; i++)
list->data[i] = list->data[i+1];
list->len --;
return 0;
}
int search_data(dd list,datatype e)
{
if(!list || empty_l(list))
{
printf("search_data failed\n");
return -1;
}
for(int i = 0; i < list->len; i++)
{
if(list->data[i] == e)
return i;
}
return -1;
}
int chang_data(dd list,datatype e,datatype key)
{
int flag = search_data(list,e);
if(flag != -1)
{
chang_l(list,flag,key);
}
return -1;
}
int delete_data(dd list,datatype e)
{
int flag = search_data(list,e);
if(flag!= -1)
delete_s(list,flag);
return 0;
}
int delete_cf(dd list)
{
for(int i =0 ; i < list->len-1; i++)
{
for(int j = i+1; j < list->len; j++)
{
if(list->data[i] == list->data[j])
{
delete_s(list,j);
j--;
}
}
}
}
void merge_sort(dd list, dd list1,dd list2)
{
int p = 0, q = 0;
while(p < list->len && q < list1->len && list2->len < MAXSIZE)
{
if(list->data[p] <= list1->data[q])
list2->data[list2->len++] = list->data[p++];
else
list2->data[list2->len++] = list1->data[q++];
}
while(p < list->len && list2->len < MAXSIZE)
list2->data[list2->len++] = list->data[p++];
while(q < list1->len && list2->len < MAXSIZE)
list2->data[list2->len++] = list1->data[q++];
}
int insert_next(dd list, int sub,datatype e)
{
if(!list || full_l(list) || sub < 0 || sub > list->len)
{
printf("insert_next failed\n");
return -1;
}
insert_s(list,sub+1,e);
return 0;
}
void sort(dd list)
{
for(int i = 1; i < list->len; i++)
{
int count = 0;
for(int j = 0; j < (list->len)-i; j++)
{
if((list->data[j]) < (list->data[j+1]))
{
datatype t;
t = (list->data[j]);
(list->data[j]) = (list->data[j+1]);
(list->data[j+1]) = t;
count++;
}
if(!count) break;
}
}
}
dd free_l(dd list)
{
if(!list) return NULL;
for(int i = 0; i < list->len; i++)
{
delete_s(list,i);
}
free(list);
list = NULL;
return list;
}