list函数c语言 list在c语言中的作用( 二 )


*(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) == 0strcmp(id, stu.id) == 0age==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);
};
ostreamoperator (ostream out,const Student stu)
{
out"id:"stu.id"\tname:"stu.name"\tage:"stu.ageendl;
}
int main()
{
ListStudent stuList;
cout"添加学生前list函数c语言:"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;
}
initlist函数用c语言怎么写#includestdio.h
voidAddNumOne(intnum)
{
num+=5;
}
voidAddNumTwo(int*num)
{
*num+=5;
}
intmain()
{
intnum=0;//这里和定义链表是一样的
AddNumOne(num);
printf("%-5d\r\n",num);
AddNumTwo(num);
printf("%-5d\r\n",num);
return0;
}
扩展资料
InitList_L()初始化函数
StatusInitList_L(LinkList*L)//初始化线性表
{
*L=(LinkList)malloc(sizeof(Lnode));
if(!(*L))returnERROR;
(*L)-next=NULL;

推荐阅读