线性表循环单链表实现
程序员文章站
2022-06-06 20:37:30
...
前言:
链表问题,个人建议,最好画图,在纸上画出来,把链表结构画出来,之后就有思路了。
程序问题,不在于看了多少书,而是敲了多少代码,顺着书的逻辑,懂了,把书合上,不一定能敲出了,缺少思考的过程,所以要多敲多练习
代码用C语言实现,可直接运行看效果,有什么问题可以留言,会回复的!
main方法里的测试代码,最好分开运行,不然怕一次性太多不好消化!
效果图:
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef struct Link{
ElemType data;
struct Link *next;
}Link,*LinkList;
/*创建一个循环单链表*/
LinkList InitLink(Link *lk)
{
lk = (LinkList)malloc(sizeof(Link));
LinkList newnode,oldnode=lk;
oldnode->next=oldnode;
int i = 1;
while(i<=10)
{
newnode = (LinkList)malloc(sizeof(Link));
newnode->data = i;
newnode->next = oldnode->next;
oldnode->next = newnode;
oldnode=newnode;
i++;
}
return lk;
}
/*求循环链表长度*/
int LenghtLink(Link *lk)
{
Link *Head=lk;
int count = 0;
while(Head->next!=lk)
{
Head = Head->next;
count++;
}
return count;
}
/*链表插入数据*/
LinkList InsertLink(Link *lk,int i,ElemType e)
{
if(i<1||i>LenghtLink(lk)+1) //判断插入位置是否合理
return lk;
LinkList newnode,oldnode=lk; //newnode存放生成的新结点
for(int j=1 ; j < i ; j++)
{
oldnode = oldnode->next;
}
newnode = (LinkList)malloc(sizeof(Link)); //创建新结点
newnode->data=e;
newnode->next=oldnode->next; //将老节点指针域的值给新结点
oldnode->next=newnode; //把新结点的地址给老节点的指针域
return lk;
}
/*链表删除操作*/
LinkList DeleteLink(Link *lk,int i)
{
if(i<1||i>LenghtLink(lk)) //判断删除位置是否合理
return lk;
LinkList deletenode,oldnode=lk;
for(int j=1 ; j < i ; j++)
{
oldnode = oldnode->next;
}
deletenode = oldnode->next;
oldnode->next = deletenode->next; //oldnode->next是要删除的结点,再加一个 ->next 就是要删除结点的指针域
free(deletenode);
return lk;
}
int main(void)
{
Link Init,Insert,Delete,*Initlink,*Insertlink,*deletelink,*Headlink;
//初始化操作
Initlink = InitLink(&Init);
int lenght = LenghtLink(Initlink);
printf("循环单链表长度:%d\n",lenght);
Headlink = Initlink;
Initlink = Initlink->next;
while(Initlink->next!=Headlink)
{
printf("链表数据域的值为:%d 链表指针域的值为:%p\n",Initlink->data,Initlink->next);
Initlink=Initlink->next;
if(Initlink->next==Headlink)
printf("链表数据域的值为:%d 链表指针域的值为:%p 头结点地址:%p\n",Initlink->data,Initlink->next,Headlink);
}
printf("\n\n");
//添加操作
Insertlink = InitLink(&Insert);
int address = 6;
Insertlink = InsertLink(Insertlink,address,666);
lenght = LenghtLink(Insertlink);
printf("添加数据后循环单链表长度:%d\n",lenght);
Headlink = Insertlink;
Insertlink = Insertlink->next;
while(Insertlink->next!=Headlink)
{
printf("链表数据域的值为:%d 链表指针域的值为:%p\n",Insertlink->data,Insertlink->next);
Insertlink=Insertlink->next;
if(Insertlink->next==Headlink)
printf("链表数据域的值为:%d 链表指针域的值为:%p 头结点地址:%p\n",Insertlink->data,Insertlink->next,Headlink);
}
printf("\n\n");
//删除操作
deletelink = InitLink(&Delete);
deletelink = InsertLink(deletelink,address,666); //进行添加操作
deletelink = DeleteLink(deletelink,address);
lenght = LenghtLink(deletelink);
printf("添加数据后循环单链表长度:%d\n",lenght);
Headlink = deletelink;
deletelink = deletelink->next;
while(deletelink->next!=Headlink)
{
printf("链表数据域的值为:%d 链表指针域的值为:%p\n",deletelink->data,deletelink->next);
deletelink=deletelink->next;
if(deletelink->next==Headlink)
printf("链表数据域的值为:%d 链表指针域的值为:%p 头结点地址:%p\n",deletelink->data,deletelink->next,Headlink);
}
return 0;
}
上一篇: 花盆种菜应该注意什么?种什么菜比较好?
下一篇: 递归--我自己调我自己