顺序表和链表
所谓的顺序表就是结构里面有个数组和数组的长度两个成员而已,在掌握这些知识的时候可以将顺序表的各种应用用函数来总结.
#define MaxSuze 50
typedef struct data{
int data[MaxSize];
int last;
}SqList;
void CreateList(SqList *L, int a[], int n) //在已有数组情况下创顺序表,
{
int i;
L = (SqList *)malloc(sizeof(SqList));
if(NULL == p){
printf("malloc failed!\n");
exit(1);
}
for(i = 0; i < n; i++)
L -> data[i] = a[i];
L -> last = n -1;
}
Sqlist seqlist_init(void)
{
SqList p;
p = (SqList)malloc(sizeof(SqList));
if(NULL == p){
printf("malloc failed!\n");
exit(1);
}
p->last = -1;
return p;
}
bool seqlist_empty(SqList l)
{
if(l->last == -1)
return true;
else
return false;
}
bool seqlist_full(SqList l)
{
if(l->last == SIZE-1)
return true;
else
return false;
}
void DisPlist(SqList *L)
{
int i;
for(i = 0; i < L -> length; i++)
printf("%d", L -> data[i]);
printf("\n");
}
bool GetElem(SqList *L, int i, int *e) //取出L中的第i个元素.
{
if(i < 1) || i > L -> last +1)
return false;
e = L -> data[i-1];
return true;
}
int LocateElem(Sqlist *L, int e) //查找L中值为e的个数
{
int i = 0;
while(i < L -> last + 1 && L -> data[i] != e)
i++;
if( i >= L -> last + 1)
return 0;
else
return i+1;
}
bool ListInsert(SqList *L, int i, int e)
{
int j;
if(i < 1 || i > L->last +1)
return false;
i--;
for(j = L -> last+1; j > i; j--)
L -> data[j] = L -> data[j - 1];
L -> data[i] = e;
L -> last++;
return true;
}
链表比顺序表灵活一点, 就是一个个结构体相连起来的而已.
单链表:
link_plist linklist_init(void)
{
link_plist p;
p = (link_plist)malloc(sizeof(link_list));
if(NULL == p){
printf("malloc failed!\n");
exit(1);
}
p->next = NULL;
return p;
}
void linklist_insert(link_plist p,link_plist new)
{
new->next = p->next;
p->next = new;
}
void linklist_del(link_plist p) //删除p后面的结点
{
link_plist q;
q = p->next;
p->next = q->next;
free(q);
}
void linklist_create(link_plist h)
{
int n,i;
link_plist new,p = h;
printf("请输入链表的额长度:");
scanf("%d",&n);
for(i = 0; i<n; i++){
new = (link_plist)malloc(sizeof(link_list));
if(NULL == new){
printf("malloc failed!\n");
exit(1);
}
scanf("%d",&new->data);
//将新结点插入到表尾
linklist_insert(p,new);
p = p->next;
linklist_show(h);
}
}
bool linklist_empty(link_plist h)
{
if(h->next == NULL)
return true;
else
return false;
}
void linklist_show(link_plist h)
{
link_plist p;
for(p = h->next; p != NULL; p = p->next)
printf("%d\t",p->data);
printf("\n");
}
双链表:
double_plist doublelist_init(void)
{
double_plist p;
p = (double_plist)malloc(sizeof(double_list));
if(NULL == p){
printf("malloc failed!\n");
exit(1);
}
p->next = p->prior = p;
return p;
}
void doublelist_insert_pre(double_plist p,double_plist new)
{
new->prior = p->prior;
p->prior->next = new;
new->next = p;
p->prior = new;
}
void doublelist_insert_post(double_plist p,double_plist new)
{
new->next = p->next;
p->next->prior = new;
new->prior = p;
p->next = new;
}
void doublelist_del(double_plist p) //删除p指向的结点
{
p->prior->next = p->next;
p->next->prior = p->prior;
//free(p);
}
void doublelist_create(double_plist h)
{
int n,i;
double_plist new;
printf("请输入链表的额长度:");
scanf("%d",&n);
for(i = 0; i<n; i++){
new = (double_plist)malloc(sizeof(double_list));
if(NULL == new){
printf("malloc failed!\n");
exit(1);
}
scanf("%d",&new->data);
//将新结点插入到表尾
doublelist_insert_pre(h,new);
doublelist_show(h);
}
}
void bool doublelist_empty(double_plist h)
{
if(h->next == h)
return true;
else
return false;
}
void doublelist_show(double_plist h)
{
double_plist p;
for(p = h->next; p != h; p = p->next)
printf("%d\t",p->data);
printf("\n");
}
#define MaxSuze 50
typedef struct data{
int data[MaxSize];
int last;
}SqList;
void CreateList(SqList *L, int a[], int n) //在已有数组情况下创顺序表,
{
int i;
L = (SqList *)malloc(sizeof(SqList));
if(NULL == p){
printf("malloc failed!\n");
exit(1);
}
for(i = 0; i < n; i++)
L -> data[i] = a[i];
L -> last = n -1;
}
Sqlist seqlist_init(void)
{
SqList p;
p = (SqList)malloc(sizeof(SqList));
if(NULL == p){
printf("malloc failed!\n");
exit(1);
}
p->last = -1;
return p;
}
bool seqlist_empty(SqList l)
{
if(l->last == -1)
return true;
else
return false;
}
bool seqlist_full(SqList l)
{
if(l->last == SIZE-1)
return true;
else
return false;
}
void DisPlist(SqList *L)
{
int i;
for(i = 0; i < L -> length; i++)
printf("%d", L -> data[i]);
printf("\n");
}
bool GetElem(SqList *L, int i, int *e) //取出L中的第i个元素.
{
if(i < 1) || i > L -> last +1)
return false;
e = L -> data[i-1];
return true;
}
int LocateElem(Sqlist *L, int e) //查找L中值为e的个数
{
int i = 0;
while(i < L -> last + 1 && L -> data[i] != e)
i++;
if( i >= L -> last + 1)
return 0;
else
return i+1;
}
bool ListInsert(SqList *L, int i, int e)
{
int j;
if(i < 1 || i > L->last +1)
return false;
i--;
for(j = L -> last+1; j > i; j--)
L -> data[j] = L -> data[j - 1];
L -> data[i] = e;
L -> last++;
return true;
}
链表比顺序表灵活一点, 就是一个个结构体相连起来的而已.
单链表:
link_plist linklist_init(void)
{
link_plist p;
p = (link_plist)malloc(sizeof(link_list));
if(NULL == p){
printf("malloc failed!\n");
exit(1);
}
p->next = NULL;
return p;
}
void linklist_insert(link_plist p,link_plist new)
{
new->next = p->next;
p->next = new;
}
void linklist_del(link_plist p) //删除p后面的结点
{
link_plist q;
q = p->next;
p->next = q->next;
free(q);
}
void linklist_create(link_plist h)
{
int n,i;
link_plist new,p = h;
printf("请输入链表的额长度:");
scanf("%d",&n);
for(i = 0; i<n; i++){
new = (link_plist)malloc(sizeof(link_list));
if(NULL == new){
printf("malloc failed!\n");
exit(1);
}
scanf("%d",&new->data);
//将新结点插入到表尾
linklist_insert(p,new);
p = p->next;
linklist_show(h);
}
}
bool linklist_empty(link_plist h)
{
if(h->next == NULL)
return true;
else
return false;
}
void linklist_show(link_plist h)
{
link_plist p;
for(p = h->next; p != NULL; p = p->next)
printf("%d\t",p->data);
printf("\n");
}
双链表:
double_plist doublelist_init(void)
{
double_plist p;
p = (double_plist)malloc(sizeof(double_list));
if(NULL == p){
printf("malloc failed!\n");
exit(1);
}
p->next = p->prior = p;
return p;
}
void doublelist_insert_pre(double_plist p,double_plist new)
{
new->prior = p->prior;
p->prior->next = new;
new->next = p;
p->prior = new;
}
void doublelist_insert_post(double_plist p,double_plist new)
{
new->next = p->next;
p->next->prior = new;
new->prior = p;
p->next = new;
}
void doublelist_del(double_plist p) //删除p指向的结点
{
p->prior->next = p->next;
p->next->prior = p->prior;
//free(p);
}
void doublelist_create(double_plist h)
{
int n,i;
double_plist new;
printf("请输入链表的额长度:");
scanf("%d",&n);
for(i = 0; i<n; i++){
new = (double_plist)malloc(sizeof(double_list));
if(NULL == new){
printf("malloc failed!\n");
exit(1);
}
scanf("%d",&new->data);
//将新结点插入到表尾
doublelist_insert_pre(h,new);
doublelist_show(h);
}
}
void bool doublelist_empty(double_plist h)
{
if(h->next == h)
return true;
else
return false;
}
void doublelist_show(double_plist h)
{
double_plist p;
for(p = h->next; p != h; p = p->next)
printf("%d\t",p->data);
printf("\n");
}