1.头文件
#ifndef _MYLIST_H
#define _MYLIST_H
#include <string.h>
#include <stdlib.h>
typedef int DATA;
typedef struct mylist
{
DATA *phead;
int size;
int count;
}MYLIST;
MYLIST *create(int size);
int destroy(MYLIST **pplist);
void input(MYLIST *plist);
void output(MYLIST *plist);
void insret(MYLIST *plist);
void change(MYLIST *plist);
void deletef(MYLIST *plist);
void find(MYLIST *plist);
void sort(MYLIST *plist);
#endif
2.主函数
#include <stdio.h>
#include "mylist.h"
int main(int argc, const char *argv[])
{
MYLIST *plist = NULL;
int size = 100;
plist = create(size);
if(NULL==plist || NULL==plist->phead)
{
puts("create fail!");
return -1;
}
int OP = 0;
while (1)
{
printf("0----quit\n");
printf("1----input\n");
printf("2----insret\n");
printf("3----change\n");
printf("4----delete\n");
printf("5----find\n");
printf("6----sort\n");
printf("input operation:\n");
printf("***********************\n");
scanf("%d",&OP);
switch (OP)
{
case 0:
printf("Quit!\n");
return 0;
case 1:
input(plist);
break;
case 2:
insret(plist);
break;
case 3:
change(plist);
break;
case 4:
deletef(plist);
break;
case 5:
find(plist);
break;
case 6:
sort(plist);
break;
default:
printf("input operation error!\n");
break;
}
}
destory(&plist);
return 0;
}
3.其他子函数
#include <stdio.h>
#include "mylist.h"
MYLIST *create(int size)
{
MYLIST *plist = NULL;
plist = (MYLIST *)malloc(sizeof(MYLIST));
if (NULL == plist)
{
puts("create mylist struct fail!");
return NULL;
}
plist->phead = (DATA *)malloc(sizeof(DATA)*size);
if (NULL == plist->phead)
{
puts("create mylist fail!");
return NULL;
}
plist->count = 0;
return plist;
}
int destory(MYLIST **pplist)
{
free((*pplist)->phead);
free(*pplist);
return 0;
}
void input(MYLIST *plist)
{
if (plist->count == 0)
{
puts("input five number:");
plist->count = 5;
int i = 0;
for (i=0;i<5;i++)
{
scanf("%d",&plist->phead[i]);
}
output(plist);
}
else
{
puts("count != 0,input other operation:\n");
}
}
void output(MYLIST *plist)
{
if (plist->count == 0)
{
puts("no content!\n");
return;
}
int i = 0;
for (i=0;i<plist->count;i++)
{
printf("%d->",plist->phead[i]);
}
printf("NULL\n");
printf("plist->count = %d\n",plist->count);
}
void insret(MYLIST *plist)
{
if (plist->count == 0)
{
puts("no content!");
return;
}
printf("mylist count is %d\n",plist->count);
int i = 0;
int insid = 0;
DATA content = 0;
printf("input insid and content:\n");
scanf("%d%d",&insid,&content);
if (insid<0 || insid>plist->count)
{
puts("insid out of range!");
return;
}
if (insid == plist->count)
{
plist->phead[insid] = content;
}
if (insid<plist->count)
{
i = plist->count-1;
for (;i>=insid;i--)
{
plist->phead[i+1] = plist->phead[i];
}
plist->phead[insid] = content;
}
plist->count += 1;
output(plist);
}
void change(MYLIST *plist)
{
if (plist->count == 0)
{
puts("no content!\n");
return;
}
int insid = 0;
int content = 0;
printf("input insid and content:\n");
scanf("%d%d",&insid,&content);
if (insid<0 || insid>=plist->count)
{
puts("insid out of range!");
return;
}
plist->phead[insid] = content;
output(plist);
}
void deletef(MYLIST *plist)
{
if (plist->count == 0)
{
puts("no content!\n");
return;
}
int insid = 0;
printf("input insid:\n");
scanf("%d",&insid);
if (insid<0 || insid>=plist->count)
{
puts("insid out of range!");
return;
}
int i = insid;
for (;i<plist->count;i++)
{
plist->phead[i] = plist->phead[i+1];
}
plist->count -=1;
output(plist);
}
void find(MYLIST *plist)
{
if (plist->count == 0)
{
puts("no content!\n");
return;
}
int insid = 0;
int num = 0;
printf("input insid\n");
scanf("%d",&insid);
if (insid<0 || insid>=plist->count)
{
puts("insid out of range!");
return;
}
num = plist->phead[insid];
printf("plist->phead[%d] = %d\n",insid,num);
}
void sort(MYLIST *plist)
{
if (plist->count == 0)
{
puts("no content!\n");
return;
}
int i = 0;
int j = 0;
DATA temp = 0;
int OP = plist->count;
for (i=0;i<OP-1;i++)
{
for(j=0;j<OP-1-i;j++)
{
if (plist->phead[j] > plist->phead[j+1])
{
temp = plist->phead[j];
plist->phead[j] = plist->phead[j+1];
plist->phead[j+1] = temp;
}
}
}
output(plist);
}
4.程序运行结果