C语言实现单链表首尾相连

这是单链表:

C语言实现单链表首尾相连_第1张图片

单链表首尾相连后,形成了循环链表

C语言实现单链表首尾相连_第2张图片

 注意:白色方框表示数据域,非白色方框为指针域,非白色方框的不同颜色表示指向的下一个结点的信息的不同

代码如下:

#include 
#include 
typedef struct
{
	char id[300];
	char password[300];
}Elemtype;
struct LNode
{
	Elemtype data;
	struct LNode *next;
};
int main(int argc, char *argv[])
{
	struct LNode boy;
	strcpy(boy.data.id,"password");
	strcpy(boy.data.password,"1");
	struct LNode boy2;
	strcpy(boy2.data.id,"password2");
	strcpy(boy2.data.password,"2");
	struct LNode boy3;
	strcpy(boy3.data.id,"password3");
	strcpy(boy3.data.password,"3");
	struct LNode boy4;
	strcpy(boy4.data.id,"password4");
	strcpy(boy4.data.password,"4");//*结点信息 
	boy.next=&boy2;
	boy2.next=&boy3;
	boy3.next=&boy4;//*将每个结点连接
	boy4.next=NULL;//*尾节点的指针域为空
	struct LNode *boy5;
	boy5=&boy;
	while(boy5->next)
	{
		boy5=boy5->next;//*直到最后一个结点 
	}
	boy5->next=&boy;//*指向第一个节点,首尾相连
	struct LNode *boy80;
	boy80=&boy;
	int a=0;
	for(a=0;a<8;a++)
	{
		printf("%s\t%s\n",boy80->data.id,boy80->data.password);//*打印出结点的信息 
		boy80=boy80->next;//*指向下一个结点 
	}
	system("pause");
	return 0;
}

程序运行如下:

C语言实现单链表首尾相连_第3张图片

你可能感兴趣的:(编程,科学技术,计算机科学,c语言)