我最近想写连通域标记的程序,有没有会用C语言链表的大神?我想问下链表怎么用啊,我查了很多资料还是不懂,希望大神出来指点啊,xiexiele
http://blog.csdn.net/jiangxinyu/article/details/7999102
可以去知乎上面或者github上面有
链表就是一个结构,结构里面的元素存储数据,结构可以使用动态或者静态数组存储
#include
#include
typedef struct LIST
{
char szName[20];
int iAge;
LIST *pNext;
}List;
void EstablishList(List *&pList)//创建链表(头插法)
{
int iNumber = 0;//链表的初始长度
int icnt = 0;//循环计数
List *pFoot = NULL;//链表中间变量
//声明变量
List *pHand = new List();
pHand->iAge = -1;
pHand->pNext = NULL;
//创建头结点
printf("输入你需要输入的数据个数:");
scanf("%d",&iNumber);
//设置链表的初始长度
for(icnt = 0;icnt {
pFoot = new List();
printf("输入姓名:");
scanf("%s",pFoot->szName );
printf("输入年龄:");
scanf("%d",&pFoot->iAge );
pFoot->pNext = NULL;
//结构体的声明并赋值
if(pHand->pNext == NULL)
{
pHand->pNext = pFoot;
}
else
{
pFoot->pNext = pHand->pNext ;
pHand->pNext = pFoot;
}
}
pList = pHand;
}
void InsertList(List *&pList,const List &lList)//插入链表
{
List *pHand = new List();
pHand->pNext = NULL;
strcpy(pHand->szName ,lList.szName);
pHand->iAge = lList.iAge ;
//新生成结构体并赋值
if(pList->pNext == NULL)
{
pList->pNext = pHand;
}
else
{
pHand->pNext = pList->pNext ;
pList->pNext = pHand;
}
}
void ModifyList(List *pList,int iAge,int iAged)//修改链表(链表指针,原始的数据,修改后的数据)
{
List *pHand = pList;
while(pHand)
{
if(pHand->iAge == iAge)
{
pHand->iAge = iAged;
}
pHand = pHand->pNext ;
}
//循环查找相同信息,并且修改
}
void ModifyList(List *pList,char *szName,char *szNamed)//修改链表(链表指针,原始的数据,修改后的数据)
{
List *pHand = pList;
while(pHand)
{
if(strcmp(pHand->szName ,szName) == 0)
{
strcpy(pHand->szName ,szNamed);
}
pHand = pHand->pNext ;
}
//循环查找相同信息,并且修改
}
void DeleteList(List *pList,const List &lList)//删除链表
{
List *pHand = pList;
List *pModify = pHand;
while(pHand)
{
if(pHand->iAge == lList.iAge || strcmp(pHand->szName ,lList.szName ) == 0)
{
pModify->pNext = pHand->pNext ;
delete(pHand);
pHand = pModify;
}
pModify = pHand;
pHand = pHand->pNext ;
}
}
void DestructionList(List *&pList)//销毁链表
{
List *pHand = pList;
while(pHand)
{
pHand = pHand->pNext ;
delete(pList);
pList = pHand;
pHand = pHand->pNext ;
}
delete(pList);
}
void PrintList(List *pList)//打印链表
{
pList = pList->pNext ;
while(pList)
{
printf("\n姓名:%s\n年龄:%d\n",pList->szName ,pList->iAge );
pList = pList->pNext ;
}
}
void main()
{
List *pList = NULL;//链表
List lList;
int iAge = 0;
int iAged = 0;
char szName[20];
char szNamed[20];
printf("调用创建链表函数:\n");
EstablishList(pList);//创建链表
PrintList(pList);//打印链表
printf("调用增添函数:\n");
printf("输入姓名:");
scanf("%s",lList.szName );
printf("输入年龄:");
scanf("%d",&lList.iAge );
InsertList(pList,lList);
PrintList(pList);//打印链表
printf("调用修改函数:\n");
printf("使用年龄修改:\n");
printf("输入你想修改的年龄:\n");
scanf("%d",&iAge);
printf("输入修改后的年龄:\n");
scanf("%d",&iAged);
ModifyList(pList,iAge,iAged);
PrintList(pList);//打印链表
printf("使用姓名修改:\n");
printf("输入你想修改的姓名:\n");
scanf("%s",szName);
printf("输入修改后的年龄:\n");
scanf("%s",szNamed);
ModifyList(pList,szName,szNamed);
PrintList(pList);//打印链表
printf("调用删除函数:\n");
printf("输入姓名:");
scanf("%s",lList.szName );
printf("输入年龄:");
scanf("%d",&lList.iAge );
DeleteList(pList,lList);
PrintList(pList);//打印链表
DestructionList(pList);//销毁链表
}
简单的单链表操作:
#include
#include
struct datatype
{
int data ;
} ;
typedef struct Node_st
{
struct datatype ;
struct Node_st* next ;
} node_st, * LinkList ;
// 创建带头结点的链表
LinkList Create_l ()
{
LinkList L ;
L = (LinkList) malloc (sizeof (node_st)) ;
if (!L)
{
printf ("分配内存失败!\n") ;
exit (0) ;
}
L->next = NULL ;
return L ;
}
// 插入结点内容
int Insert_l (LinkList &L)
{
LinkList q, p ;
q = L ;
p = (LinkList) malloc (sizeof (node_st)) ;
if (!p)
{
printf ("分配内存失败!\n") ;
exit (0) ;
}
p->next = NULL ;
}
// 统计链表结点的个数
int Travel_l (LinkList L)
{
int num = 0 ;
LinkList p ;
p = L->next ;
while (p)
{
num++ ;
p = p->next ;
}
return num ;
}
// 判断链表是否为空
int Empty_l (LinkList L)
{
return (NULL == L->next) ;
}
int main (void)
{
LinkList L ;
if ( L = Create_l ())
printf ("\t\t\t/*-----Create_l successfully!-----*/\n") ;
return 0 ;
}