Win32控制台程序, 练习

list.h
#ifndef _LIST_H
#define _LIST_H

#define LIST_INIT_SIZE 10
#define LIST_INCREMENT 10

typedef struct


{
ElemType * elem;
int length;
int size;
}LIST;

LIST* InitList();
void FreeList(LIST* l);
int InsertList(LIST* l,int i,ElemType* e);
int DeleteList(LIST* l,int i);

#endif

list.c
#include <stdio.h>
#include <stdlib.h>
#include "stu.h"
#include "list.h"


LIST * InitList()


{
LIST* l=(LIST*)malloc(sizeof(LIST));
if (l==NULL)
exit(0);
l->elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(l->elem == NULL)

{
free(l);
exit(0);
}
l->length = 0;
l->size = LIST_INIT_SIZE;
return l;
}

void FreeList(LIST* l)


{
free(l->elem);
free(l);
}

int InsertList(LIST* l,int i,ElemType* e)


{
ElemType *p=NULL,*q=NULL,*newElem = NULL;
if (l==NULL || e==NULL)
return 0;
if ( i < 1 || i > l->length+1 )
return 0;
if(l->length >= l->size)

{
newElem=realloc(l->elem,(l->size+LIST_INCREMENT)*sizeof(ElemType));
if (newElem == NULL)

{
return 0;
}
l->elem = newElem;
l->size = LIST_INCREMENT;
}
q=&l->elem[i-1];
for (p=&(l->elem[l->length-1]);p>=q;--p)

{
*(p+1)=*p;
}
*q=*e;
++l->length;
return 1;
}

int DeleteList(LIST* l,int i)


{
ElemType* p = NULL,*q =NULL;
if (l == NULL)

{
return 0;
}
if (i <1 || i > l->length )

{
return 0;
}
p = &l->elem[i-1];
q = &l->elem[l->length-1];
for (;p <q;++p)

{
*p = *(p+1);
}
--l->length;
return 1;
}

stu.h
#ifndef _STU_H
#define _STU_H


typedef struct


{
char sno[5];
char name[21];
char sex[3];
int scroe;
}ElemType;


#endif

main.c
#include <stdio.h>
#include "stu.h"
#include "list.h"



ElemType stu[3]=
{

{"S103","jerry","man",100},

{"S102","tom","man",90},

{"S101","lili","man",80}
};
void main()


{
int i;
LIST* list = NULL;
list = InitList();
for (i = 0;i < 3; ++i)

{
InsertList(list,1,&stu[i]);
}
DeleteList(list,2);
FreeList(list);
}

posted on 2010-05-31 00:53
CrazyNerd 阅读(168)
评论(0) 编辑 收藏 引用 所属分类:
数据结构与算法