1. c语言中creatlist的用法
1、createlist不是库函数,一般是数据结构中实现新建一个链表的自定义函数。因此没有什么用法好说的,关键是看自己怎么定义。
2、例程:
NODE*creatlist(inta[])
{NODE*h,*p,*q;inti;
h=(NODE*)malloc(sizeof(NODE));
h->next=NULL;
for(i=0;i<N;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;}}
returnh;
}
2. C语言中的list是指什么求一个简单的list代码
C语言中没有list
list是C++中的一个类
具体使用可以从网上查一下,有很多应用
3. 怎样在C语言中正确运用链表链表的使用需要注意哪些要点
1.使用链表时候,先确认要使用的是单向链表,还是双向链表,或者是循环链表。一定要初始化。
2.添加节点时候,要注意是否队列已满。
3.删除节点的时候,要注意队列是否为空。
4.要有可以判断链表是否为空的函数。
5.要有可以判断链表节点个数的函数。
4. 如何用C语言或C++实现一个List类
C语言没有类的概念。C++有现成的List类, #include<list>即可。
如果要自己实现可以参考C++数据结构的书籍,是最基本的练习。
这里实现一个简单的例程,请参考:
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
usingnamespacestd;
#include<stdio.h>
#include<string>
#include"math.h"
template<classT>classList{
public:
List()//构造函数
{
pFirst=NULL;
}
voidAdd(T&t)//在Link表头添加新结点
{
if(pFirst==NULL)
{
pFirst=newNode;
*(pFirst->pT)=t;
}
else
{
Node*pNewNode=newNode;
*(pNewNode->pT)=t;
pNewNode->pNext=pFirst;
pFirst=pNewNode;
}
}
voidRemove(T&t)//在Link中删除含有特定值的元素
{
Node*pNode=pFirst;
if(*(pNode->pT)==t)
{
pFirst=pFirst->pNext;
deletepNode;
return;
}
while(pNode!=NULL)
{
Node*pNextNode=pNode->pNext;
if(pNextNode!=NULL)
{
if(*(pNextNode->pT)==t)
{
pNode->pNext=pNextNode->pNext;
deletepNextNode;
return;
}
}
else
return;//没有相同的
pNode=pNode->pNext;
}
}
T*Find(T&t)//查找含有特定值的结点
{
Node*pNode=pFirst;
while(pNode!=NULL)
{
if(*(pNode->pT)==t)
{
returnpNode->pT;
}
pNode=pNode->pNext;
}
returnNULL;
}
voidPrintList()//打印输出整个链表
{
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;
deletepNode;
pNode=pNextNode;
}
}
protected:
structNode{
Node*pNext;
T*pT;
Node()
{
pNext=NULL;
pT=newT;
}
~Node()
{
deletepT;
}
};
Node*pFirst;//链首结点指针
};
classStudent
{
public:
charid[20];//学号
charname[20];//姓名
intage;//年龄
Student()
{
}
~Student()
{
}
Student(constchar*pid,constchar*pname,int_age)
{
strcpy(id,pid);
strcpy(name,pname);
age=_age;
}
booloperator==(constStudent&stu)
{
returnstrcmp(id,stu.id)==0&&strcmp(id,stu.id)==0&&age==stu.age;
}
Student&operator=(constStudent&stu)
{
strcpy(id,stu.id);
strcpy(name,stu.name);
age=stu.age;
}
friendostream&operator<<(ostream&out,constStudent&stu);
};
ostream&operator<<(ostream&out,constStudent&stu)
{
out<<"id:"<<stu.id<<" name:"<<stu.name<<" age:"<<stu.age<<endl;
}
intmain()
{
List<Student>stuList;
cout<<"添加学生前:"<<endl;
stuList.PrintList();
Studentstu1("1","张三",18);
Studentstu2("2","李四",18);
Studentstu3("3","王五",18);
Studentstu4("4","至尊宝",18);
Studentstu5("5","猪八戒",18);
Studentstu6("6","唐僧",18);
Studentstu7("7","沙和尚",18);
Studentstu8("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();
Studentstu11("1","张三",18);
Student*pStu=stuList.Find(stu11);
cout<<"查找到的同学是:"<<*pStu;
stuList.Remove(stu11);
cout<<" 删除第一个后:"<<endl;
stuList.PrintList();
return0;
}
5. C语言的形参struct list 和 list有什么区别吗
对于结构体类型变量定义,struct
list和list在C++中是相同的,都是正确的。
但在C语言中,如果没有经过重定义处理,则会编译报错。如:
typedef struct list
{
int data;
struct list *next;
} list ;这样之后,可以使用struct
list
或
list
来定义变量了,如:
struct
list
*
link
;
list
*
link1
;
6. c语言中,int i,j,list(10),这里的list(10)代表什么意思
在C++中才可以写list(10),并且list的值被初始化为10.但是有一个问题:list是一个容器类型,你这样写应该会有问题。就像vector一样。
7. C语言的形参struct list 和 list有什么区别吗
对于结构体类型变量定义,struct list和list在C++中是相同的,都是正确的。
但在C语言中,如果没有经过重定义处理,则会编译报错。如:
typedefstructlist
{
intdata;
structlist*next;
}list;
这样之后,可以使用struct list 或 list 来定义变量了,如:
struct list * link ;
list * link1 ;
8. c语言中的struct list是什么意思,它代表什么东西。怎么使用
struct
friends_list
f;
就是定义简单的结构体变量f
friends[count]
=
f;
就是把结构体变量f赋值给结构体数组friends的第count个元素
9. C语言中链表主要应用于哪些实际问题的解决
链表可以解决很多实际问题,比如数据结构课程上讲的多项式运算、求解约瑟夫问题,操作系统原理中的内存管理器实现等等。举一个在Windows通过链表搜索文件的实例,代码如下(vc6.0中编译通过)。
#include<stdio.h>
#include<windows.h>
structDirList{
chartable[256];
DirList*pNext;
};
DirList*first,*newlist,*last;
//加入文件夹链表
voidAddList(char*list)
{
newlist=newDirList;
strcpy(newlist->table,list);
newlist->pNext=NULL;
//假如文件链表为空,那么第一个和最后一个节点都指向新节点
if(first==NULL)
{
first=newlist;
last=newlist;
}
//不为空,则原来最后一个节点指向新节点
else
{
last->pNext=newlist;
last=newlist;
}
}
//查找文件,并把找到的文件夹加入文件夹链表
voidFindFile(char*pRoad,char*pFile)
{
charFileRoad[256]={0};
charDirRoad[256]={0};
charFindedFile[256]={0};
charFindedDir[256]={0};
strcpy(FileRoad,pRoad);
strcpy(DirRoad,pRoad);
strcat(DirRoad,"\*.*");
WIN32_FIND_DATAfindData;
HANDLEhFindFile;
hFindFile=FindFirstFile(DirRoad,&findData);
if(hFindFile!=INVALID_HANDLE_VALUE)
{
do
{
if(findData.cFileName[0]=='.')
continue;
//假如是文件夹,则假如文件夹列表
if(findData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
strcpy(FindedDir,pRoad);
strcat(FindedDir,"\");
strcat(FindedDir,findData.cFileName);
//加入文件夹列表
AddList(FindedDir);
memset(FindedDir,0x00,256);
}
//继续查找
}while(FindNextFile(hFindFile,&findData));
}
strcat(FileRoad,"\");
strcat(FileRoad,pFile);
//查找要查找的文件
hFindFile=FindFirstFile(FileRoad,&findData);
if(hFindFile!=INVALID_HANDLE_VALUE)
{
do
{
strcpy(FindedFile,pRoad);
strcat(FindedFile,"\");
strcat(FindedFile,findData.cFileName);
//输出查找到的文件
printf("%s ",FindedFile);
memset(FindedFile,0x00,256);
}while(FindNextFile(hFindFile,&findData));
}
}
intSeachFile(char*Directory,char*SeachFile)
{
DirListNewList;
strcpy(NewList.table,Directory);
NewList.pNext=NULL;
last=&NewList;
first=&NewList;
while(true)
{
DirList*Find;
//假如链表不为空,提取链表中的第一个节点,并把第一个节点指向原来第二个
if(first!=NULL)
{
//提取节点
Find=first;
//并把第一个节点指向原来第二个
first=first->pNext;
//在提取的节点的目录下查找文件
FindFile(Find->table,SeachFile);
}
//为空则停止查找
else
{
printf("文件搜索完毕 ");
return0;
}
}
return0;
}
intmain(intargc,char*argv[])
{
if(argc!=3){
printf("程序名文件目录要搜索的文件名 ");
return0;
}
SeachFile(argv[1],argv[2]);
return0;
}
执行效果如下,测试搜索c:windows目录中的记事本程序notepad.exe。