有错误的地方,希望大家能指正哦
这是我第一次写博客,以后会将linux,,C和指针有很多很好的知识点,会慢慢总结,然后上传的,,,,
顺序表的简单操作,,需要注意的就是插入操作,定长与不定长顺序表的初始化,还有就是链表的排序(用的是冒泡法),本来想用快排的(qsort),对其内部的实现还不是特别清楚,所以,,,,
ps:以下代码是完整且调试通过的
"List.h"
typedef int ElemType;
const int SUM_SIZE = 2;
typedef struct _changeList
{
ElemType *elem;
int length;
int total;
}List;
// 初始化顺序表
void Init_List(List *p);
//judege the list is full//(0)is not,(1)yes
bool is_full(List *p);
//add data
bool insert(List *p,int pos,ElemType a);
//print
void show(List *p);
//find
int search(List *p, ElemType val);
//delete one
bool delete_pos(List *P,int pos);
//delete all
bool delete_by_val(List *p, int a);
//find the max of the list
int find_max(List *p);
//find the min of the list
int find_min(List *p);
//get the length of List
int get_length(List *p);
//swap
void swap(void *a,void *b);
//sort
void common_sort(List *p);
//destory pointer
void destory(List *p);
“List.cpp”
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"alter list.h"
void Init_List(List *p)
{
p->total = SUM_SIZE;
if(p == NULL)
{
printf("error\n");
exit(-1);
}
p->length = 0;
p->elem =(ElemType *)malloc(sizeof(ElemType )*p->total );
}
/*
bool is_full(List *p)
{
if(p == NULL)
{
return false;
}
if(p->length == p->total )
{
return true;
}
else
return false;
}
*/
//
bool insert(List *p,int pos,ElemType a)
{
if(p == NULL || pos<0 || pos>p->length || p->length >p->total )
{
return false;
}
if(p->length == p->total) //PS:此处操作就可以代替 is_full(判该表是否满)
{
p->elem =(ElemType *)realloc(p->elem ,sizeof(ElemType)*p->total *2);
p->total = p->total*2;
printf("enlarge successfully\n");
}
for(int i=p->length-1;i>=pos;i--)
{
p->elem[i+1] = p->elem [i];
}
p->elem[pos] = a;
p->length++;
return true;
}
//
void show(List *p)
{
for(int i=0;i<p->length;i++)
{
printf("%d\n",p->elem [i]);
}
printf("\n");
}
//
int search(List *p, ElemType val)
{
if(p == NULL)
{
return -1;
}
while(p->length != 0)
{
if(p->elem [p->length] == val)
{
return p->length;
}
p->length--;
}
return -1;
}
//
bool delete_pos(List *p,int pos)
{
assert(p != NULL);
if(pos<0 || pos>p->length )
{
return false;
}
for(int i=pos;i<p->length;i++)
{
p->elem [i]=p->elem [i+1];
}
p->length--;
return true;
}
//一次删全部
bool delete_by_val(List *p, int a)
{
while(p->length-- != 0)
{
int pos = search(p, a);
if (pos == -1)
{
return false;
}
delete_pos(p, pos);
}
return true;
}
//
void swap(void *a,void *b)
{
int tmp;
int *m =(ElemType *)a;
int *n =(ElemType *)b;
tmp = *n;
*n = *m;
*m = tmp;
}
//冒泡法排序
void common_sort(List *p)
{
int i;
int j;
if(p == NULL)
{
exit(-1);
}
for(j =0;j<p->length-1;j++)
{
for( i=0;i<p->length-1-j;i++)
{
if(p->elem [i] > p->elem [i+1])
{
swap(&(p->elem [i]),&(p->elem [i+1]));
}
}
}
}
//find max
int find_max(List *p)
{
int max = p->elem[0] ;
if(p == NULL)
{
return -1;
}
for(int i = 0;i<p->length;i++)
{
if(max<p->elem [i])
{
max = p->elem [i];
}
}
return max;
}
//find min
int find_min(List *p)
{
int min = p->elem[0] ;
if(p == NULL)
{
return -1;
}
for(int i = 0;i<p->length;i++)
{
if(min>p->elem [i])
{
min = p->elem [i];
}
}
return min;
}
//gain the length
int get_length(List *p)
{
return p->length ;
}
//
void destory(List *p)
{
if(p == NULL)
{
exit(-1);
}
free(p->elem );
}
“main.cpp”
#include<stdlib.h>
#include"alter list.h"
int main()
{
List s;
int m=2;
Init_List (&s);
insert(&s,0,10);
for(int i=1;i<10;i++)
{
insert(&s,0,-1*i);
insert(&s,0,i*3);
}
common_sort(&s);
show(&s);
destory(&s);
//delete_by_val (&s,27);
//
// printf("%d\n",get_length(&s));
//printf("%d\n",find_max(&s));
//printf("%d\n",find_min(&s));
//printf("\n\n\n\n");
//show(&s);
//printf("*************************\n");
//printf("%d\n",search(&s,-1));
//printf("**************************\n");
//delete_pos (&s,17);
//show(&s);
//qsort(&s,s.length ,sizeof(List),compare);
return 0;
}