C语言中没有list
公司主营业务:成都网站制作、网站建设、外贸网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出古县免费做网站回馈大家。
list是C++中的一个类
具体使用可以从网上查一下,有很多应用
函数功能:该函数可以获得与系统中输入点的当前集相对应的键盘布局句柄。该函数将句柄拷贝到指定的缓冲区中。
函数原型:UINT
GetKeyboardLayoutList(int
nBuff,HKL
FAR
*IpList)
参数:
nBuff:指定缓冲区中可以存放的最大句柄数目。
Iplist:缓冲区指针,缓冲区中存放着键盘布局句柄数组。
返回值:若函数调用成功,则返回值为拷贝到缓冲区的键盘布局句柄的数目,或者,若nBuff为0,则运回值为接受所有当前键盘布局的缓冲区中的大小(以数组成员为单位)。若函数调用失败,返回值为0。若想获得更多错误信息,可调用GetLastError函数。
#include time.h
#include stdio.h
#define NULL -2
#define ERROR -1
#define OK 1
#define TRUE 2
#define FALSE 3
#define Boolen int
#define Status int
#define LIST_INIT_SIZE 3
#define LIST_INCREMENT 2
#define NAME_LEN 13
#define DES_LEN 30
char ErrDescription[DES_LEN];
typedef struct{
int NO;
char Name[NAME_LEN];
enum{male,female} Sex;
int Age;
char Tel[15];
char Inserttime[64];
}ElemType,*ElemPointer;
typedef struct{
ElemPointer base; //基址
int length; //表长
int listsize; //内存占用
int elemcount; //记录数
}SqList,*SqPointer;
int ErrorEXP(int i)
{
switch(i)
{ case 1: strcpy(ErrDescription,"InitList::(ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType)) 空间申请失败");break;
case 2: strcpy(ErrDescription,"IncreaseList::(ElemType *)realloc(L-base,(L-length + LIST_INCREMENT) * sizeof(ElemType)) 空间申请失败");break;
case 3: strcpy(ErrDescription,"if(!L-base) return Error; SqList不存在");break;
case 4: strcpy(ErrDescription,"GetElem:: i 越界");break;
case 5: strcpy(ErrDescription,"ListInsert:: i 越界");break;
case 6: strcpy(ErrDescription,"ListInsert:: CALL IncreaseList(L)==ERROR return Error 邻接空间申请失败,由ListInsert返回");break;
case 7: strcpy(ErrDescription,"ListDelete:: i 越界");break;
case 8: strcpy(ErrDescription,"KeyInList:: i 越界");break;
case 9: strcpy(ErrDescription,"KeyInList:: CALL ListInsert(L,i,temp)==ERROR return Error 邻接空间申请失败,由KeyInList返回");break;
case 10: strcpy(ErrDescription,"ScanfList:: CALL KeyInList(L,i++)==ERROR return Error");break;
}
puts("!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!\n");
puts(ErrDescription);
puts("\n!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!\n");
return ERROR;
}
Status InitList(SqPointer L)
{
L-base = 0; //不可不要!!! 去掉后即使(ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType))失败,系统也会认为正常
L-base = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if(!L-base) return ErrorEXP(1); //空间申请失败,返回
L-length = LIST_INIT_SIZE;
L-listsize = L-length * sizeof(ElemType);
L-elemcount = 0;
return OK;
}
Status IncreaseList(SqPointer L)
{
ElemPointer newbase;
newbase = (ElemType *)realloc(L-base,(L-length + LIST_INCREMENT) * sizeof(ElemType));
if(!newbase) return ErrorEXP(2);
L-base = newbase;
L-length += LIST_INCREMENT;
L-listsize = L-length * sizeof(ElemType);
return OK;
}
Status DestroyList(SqPointer L)
{
if(!L-base) return ErrorEXP(3); //L不存在,返回
free(L-base);
L-length = NULL;
L-listsize = NULL;
L-elemcount = NULL;
return OK;
}
Status ClearList(SqPointer L)
{
if(!L-base) return ErrorEXP(3); //L不存在,返回
L-elemcount = 0;
return OK;
}
Boolen ListEmpty(SqPointer L)
{
if(!L-base) return ErrorEXP(3); //L不存在,返回
if(L-elemcount == 0)
return TRUE;
else
return FALSE;
}
int ListElemCount(SqPointer L)
{
if(!L-base) return ErrorEXP(3); //L不存在,返回
return L-elemcount;
}
Status GetElem(SqPointer L,int i,ElemType *ret) //调用此函数需将ret指向main函数域某一ElemType变量
{
if(!L-base) return ErrorEXP(3); //L不存在,返回
if(i L-elemcount) return ErrorEXP(4); //i越界,返回
*ret = L-base[i-1]; //i 从1开始 此种方法在main中改变*ret会直接更改链表中数据
return OK;
}
//重大发现 指针型 temp-base 普通型L.base
int LocateElem(SqPointer L,char Locatename[]) //返回的i从1开始
{
int i=0;
ElemType *temp;
if(!L-base) return ErrorEXP(3); //L不存在,返回
while(iL-elemcount)
{
temp=(L-base[i]); //改为temp=L-base[i++];并去除下面的i++; ??
if(strcmp(temp-Name,Locatename) == 0) return i+1; //不能用temp-Name==locatename来试图比较字符串
i++;
}
return 0;
}
Status ListInsert(SqPointer L,int i,ElemType newelem) //插入位置1=i=elemcount+1
{
ElemPointer newbase;
ElemType *temp,*flag;
if(!L-base) return ErrorEXP(3); //L不存在,返回
if(i1 || iL-elemcount + 1) return ErrorEXP(5);
if(L-elemcount == L-length)
if(IncreaseList(L)==ERROR) return ErrorEXP(6);
flag=(L-base[i-1]); //插入位置
for(temp=(L-base[L-elemcount-1]);temp=flag;temp--)
*(temp+1)=*temp;
*flag=newelem;
L-elemcount++;
return OK;
}
Status ListDelete(SqPointer L,int i,ElemType *ret) //调用此函数需将ret指向main函数域某一ElemType变量
{
ElemType *temp;
if(!L-base) return ErrorEXP(3); //L不存在,返回
if(i1 || iL-elemcount) return ErrorEXP(7);
*ret=L-base[i-1]; //删除位置,这里先返回该值
for(temp=(L-base[i]);temp=(L-base[L-elemcount-1]);temp++)
*(temp-1)=*temp;
L-elemcount--;
return OK;
}
Status KeyInList(SqPointer L,int i)
{
ElemType temp;
time_t t;
char tmp[64];
char S;
if(!L-base) return ErrorEXP(3); //L不存在,返回
if(i1 || iL-elemcount + 1) return ErrorEXP(8);
printf("正在输入第%d个元素的值:",i);
printf("\n编号:(int)\n");
scanf("%d",temp.NO);
printf("\n姓名:(char *)\n");
scanf("%s",temp.Name);
printf("\n性别:(m or f)\n");
do{
S=getch();
if(S=='m')
temp.Sex=male;
else if(S=='f')
temp.Sex=female;
else
puts("Key in 'm' or 'f'.\n");
}while(S!='m' S!='f');
putchar(S);
printf("\n年龄:(int)\n");
scanf("%d",temp.Age);
printf("\n电话:(char *)\n");
scanf("%s",temp.Tel);
printf("\n记录时间:\n");
t=time(0);
strftime(tmp,sizeof(tmp),"%Y/%m/%d %X %A 本年第%j天 %z",localtime(t));
puts(tmp);
strcpy(temp.Inserttime,tmp);
if(ListInsert(L,i,temp)==OK)
return OK;
else
return ErrorEXP(9);
}
ElemType ScanfElem()
{
ElemType temp;
time_t t;
char tmp[64];
char S;
printf("正在录入元素:");
printf("\n编号:(int)\n");
scanf("%d",temp.NO);
printf("\n姓名:(char *)\n");
scanf("%s",temp.Name);
printf("\n性别:(m or f)\n");
do{
S=getch();
if(S=='m')
temp.Sex=male;
else if(S=='f')
temp.Sex=female;
else
puts("Key in 'm' or 'f'.\n");
}while(S!='m' S!='f');
putchar(S);
printf("\n年龄:(int)\n");
scanf("%d",temp.Age);
printf("\n电话:(char *)\n");
scanf("%s",temp.Tel);
printf("\n记录时间:\n");
t=time(0);
strftime(tmp,sizeof(tmp),"%Y/%m/%d %X %A 本年第%j天 %z",localtime(t));
puts(tmp);
strcpy(temp.Inserttime,tmp);
return temp;
}
Status ScanfList(SqPointer L,int i)
{
char p='c';
while(putchar('\n'),p=='c'||p=='C')
{ p='\0';
if(KeyInList(L,i++)==ERROR) return ErrorEXP(10);
printf("\nPress ESC key to exit or 'C' to continue...");
while(p!='c' p!='C' (int)p!=27)
p=getch();
}
return OK;
}
Status PrintListProperty(SqPointer L)
{
puts("SqList L Property:");
if(!L-base)
{ puts("链表不存在!");
return OK;}
else
puts("链表已初始化...\n");
printf("%d/%d BASE=%d,MemoryStatus=%d\n",L-elemcount,L-length,L-base,L-listsize);
return OK;
}
Status PrintOnScreen(SqPointer L)
{
int i;
char Stmp[7],t;
if(!L-base) return ErrorEXP(3); //L不存在,返回
puts("Push 'C' shell CLS or other key to skip.");
t=getch();
if(t=='c' || t=='C')
system("cls");
puts("数据表打印:");
for(i=0;i=L-elemcount-1;i++)
{ printf("\nElem %d st:\n",i+1);
if(L-base[i].Sex == male)
strcpy(Stmp,"male");
else if(L-base[i].Sex == female)
strcpy(Stmp,"female");
else
strcpy(Stmp,"Unknow");
printf("NO:%d\tName:%s\t\tSex:%s\tAge:%d\n\tTel:%s\n\tInsertTime:%s\n",L-base[i].NO,L-base[i].Name,Stmp,L-base[i].Age,L-base[i].Tel,L-base[i].Inserttime);
}
return OK;
}
Status PrintElem(ElemPointer elem)
{
char Stmp[7];
printf("\nPrintElem:\n");
if(elem-Sex == male)
strcpy(Stmp,"male");
else if(elem-Sex == female)
strcpy(Stmp,"female");
else
strcpy(Stmp,"Unknow");
printf("NO:%d\tName:%s\t\tSex:%s\tAge:%d\n\tTel:%s\n\tInsertTime:%s\n",elem-NO,elem-Name,Stmp,elem-Age,elem-Tel,elem-Inserttime);
return OK;
}
void main() //把以上所有函数都串了起来
{
SqList TheList;
SqPointer ListP;
ElemType mylistelem,*elemtemp;
ElemPointer mylist;
int i;
char nameT[20];
elemtemp=mylistelem; //*ret
ListP=TheList;
if(InitList(ListP)==OK) puts("InitList(TheList)==OK");
PrintListProperty(ListP);
if(ListEmpty(ListP)==TRUE) puts("ListEmpty==True");
else puts("ListEmpty==False");
ScanfList(ListP,1);
PrintListProperty(ListP);
PrintOnScreen(ListP);
printf("ListElemCount return %d.",ListElemCount(ListP));
puts("\nGetElem index? ");
scanf("%d",i);
if(GetElem(ListP,i,elemtemp)==OK) PrintElem(elemtemp);
puts("\nLocateElem name? ");
scanf("%s",nameT);
printf("LocateElem return %d.",LocateElem(ListP,nameT));
puts("\nListDelete index? ");
scanf("%d",i);
if(ListDelete(ListP,i,elemtemp)==OK) PrintElem(elemtemp);
puts("\nListInsert index? ");
scanf("%d",i);
puts("\nListInsert NEWELEM? ");
ListInsert(ListP,i,ScanfElem());
PrintListProperty(ListP);
PrintOnScreen(ListP);
if(ClearList(ListP)==OK) puts("ClearList==OK");
if(ListEmpty(ListP)==TRUE) puts("ListEmpty==True");
if(DestroyList(ListP)==OK) puts("DestroyList==OK");
getch();
}
/* 函数列表
类型 名称 参数 说明
int ErrorEXP (int i) 错误描述符
Status InitList (SqPointer L) 初始化SqPointer L... 通过L返回base
Status IncreaseList (SqPointer L) L当前满时,继续申请空间
Status DestroyList (SqPointer L) 销毁L
Status ClearList (SqPointer L) 把L置为空表
Boolen ListEmpty (SqPointer L) 判断L是否为空表,是则返回TRUE
int ListElemCount (SqPointer L) 返回当前L中记录的元素个数
Status GetElem (SqPointer L,int i,ElemType *ret) 通过*ret返回i号元素
int LocateElem (SqPointer L,char Locatename[]) 顺序查找表,根据name字段,返回首个匹配元素的i,无则返回0
Status ListInsert (SqPointer L,int i,ElemType newelem) 在L中的i号位置插入newelem元素
Status ListDelete (SqPointer L,int i,ElemType *ret) 删除L中第i号元素,并用*ret返回该元素
Status KeyInList (SqPointer L,int i) 从键盘输入单个元素并插入到i号位置
ElemType ScanfElem () 从键盘输入单个元素返回一个ElemType类型的节点
Status ScanfList (SqPointer L,int i) 从i号开始递增顺序录入元素到L,直到按'ESC'
Status PrintListProperty(SqPointer L) 打印L的属性,打印格式为(已用空间/已申请空间 基址 内存占用)
Status PrintOnScreen (SqPointer L) 打印整张L表到屏幕
Status PrintElem (ElemPointer elem) 打印单个ElemType类型的元素
时间仓促,所以乱了些,书上2章开头 动态线性的顺序表 的基本操作几乎都写了
不知你说的是不是这个,mian函数比较乱,只是把所有的基本操作都串了起来,你
可以根据情况改改主函数的调用过程,就会比较清楚是怎么实现的了。你可以按F10
进行单部跟踪,F11可以进入调用过程,一步一步跟着程序走一遍就好了。
关于动态链表的我之前写过一个,也好象给你看过,这里再附上一起发过去。文件LinkList.c
只实现了构造链表,并打印出来的功能。
*/
1、createlist不是库函数,一般是数据结构中实现新建一个链表的自定义函数。因此没有什么用法好说的,关键是看自己怎么定义。
2、例程:
NODE *creatlist(int a[])
{ NODE *h,*p,*q;int i;
h=(NODE *)malloc(sizeof(NODE));
h-next=NULL;
for(i=0;iN;i++)
{q=(NODE *)malloc(sizeof(NODE));
q-data=a[i];
q-next=NULL;
if(h-next==NULL) h-next=p=q;
else {p-next=q;p=q;} }
return h;
}
C语言没有类的概念。C++有现成的List类, #includelist即可。
如果要自己实现可以参考C++数据结构的书籍,是最基本的练习。
这里实现一个简单的例程,请参考:
#include iostream
#include fstream
#include stdlib.h
#include string.h
using namespace std;
#includestdio.h
#includestring
#include "math.h"
templateclass T class List{
public:
List() //构造函数
{
pFirst = NULL;
}
void Add(T t) //在Link表头添加新结点
{
if(pFirst == NULL)
{
pFirst = new Node;
*(pFirst-pT) = t;
}
else
{
Node* pNewNode = new Node;
*(pNewNode-pT) = t;
pNewNode-pNext = pFirst;
pFirst = pNewNode;
}
}
void Remove(T t) //在Link中删除含有特定值的元素
{
Node* pNode = pFirst;
if(*(pNode-pT) == t)
{
pFirst = pFirst-pNext;
delete pNode;
return;
}
while(pNode != NULL)
{
Node* pNextNode = pNode-pNext;
if(pNextNode!=NULL)
{
if(*(pNextNode-pT) == t)
{
pNode-pNext = pNextNode-pNext;
delete pNextNode;
return;
}
}
else
return;//没有相同的
pNode = pNode-pNext;
}
}
T* Find(T t) //查找含有特定值的结点
{
Node* pNode = pFirst;
while(pNode != NULL)
{
if(*(pNode-pT) == t)
{
return pNode-pT;
}
pNode = pNode-pNext;
}
return NULL;
}
void PrintList() // 打印输出整个链表
{
if(pFirst == NULL)
{
cout"列表为空列表!"endl;
return;
}
Node* pNode = pFirst;
while(pNode != NULL)
{
cout*(pNode-pT)endl;
pNode = pNode-pNext;
}
}
~List()
{
Node* pNode = pFirst;
while(pNode != NULL)
{
Node* pNextNode = pNode-pNext;
delete pNode;
pNode = pNextNode;
}
}
protected:
struct Node{
Node* pNext;
T* pT;
Node()
{
pNext = NULL;
pT = new T;
}
~Node()
{
delete pT;
}
};
Node *pFirst; //链首结点指针
};
class Student
{
public:
char id[20]; //学号
char name[20]; //姓名
int age; //年龄
Student()
{
}
~Student()
{
}
Student(const char* pid, const char* pname, int _age)
{
strcpy(id, pid);
strcpy(name, pname);
age = _age;
}
bool operator==(const Student stu)
{
return strcmp(id, stu.id) == 0 strcmp(id, stu.id) == 0 age==stu.age;
}
Student operator=(const Student stu)
{
strcpy(id, stu.id);
strcpy(name, stu.name);
age = stu.age;
}
friend ostream operator (ostream out,const Student stu);
};
ostream operator (ostream out,const Student stu)
{
out"id:"stu.id"\tname:"stu.name"\tage:"stu.ageendl;
}
int main()
{
ListStudent stuList;
cout"添加学生前:"endl;
stuList.PrintList();
Student stu1("1", "张三", 18);
Student stu2("2", "李四", 18);
Student stu3("3", "王五", 18);
Student stu4("4", "至尊宝", 18);
Student stu5("5", "猪八戒", 18);
Student stu6("6", "唐僧", 18);
Student stu7("7", "沙和尚", 18);
Student stu8("8", "观音", 18);
stuList.Add(stu1);
stuList.Add(stu2);
stuList.Add(stu3);
stuList.Add(stu4);
stuList.Add(stu5);
stuList.Add(stu6);
stuList.Add(stu7);
stuList.Add(stu8);
cout"添加学生后:"endl;
stuList.PrintList();
Student stu11("1", "张三", 18);
Student* pStu = stuList.Find(stu11);
cout"查找到的同学是:"*pStu;
stuList.Remove(stu11);
cout"\n\n删除第一个后:"endl;
stuList.PrintList();
return 0;
}