链表初步-构建静态链表,满足你的好奇心

本篇目的:先构建一个静态链表,给大家以直观印象。引诱你“爱”上链表。

本篇构建的链表不带头结点,第一个结点就是数据结点。

一、单链表的形态

二、单链表结点的C语言表示

struct node
{
	int data;
	struct node *next;	
};

三、静态结点

 

#include<stdio.h>

struct node
{
	int data;
	struct node *next;	
};


int main()
{
	struct node *head, a,b,c,d,e;

}

head为指针变量,a、b、c、d、e为五个静态结点。 

四、为静态结点赋值

head=0;//空指针

a.data=5;a.next=0;
b.data=8;b.next=0;
c.data=1;c.next=0;
d.data=2;d.next=0;
e.data=11;e.next=0;

五、静态结点链起来

        head=&a;
	a.next=&b;
	b.next=&c;
	c.next=&d;
	d.next=&e;

六、静态链表遍历

写成函数形式:

void print_link_list(struct node *head)
{/*//head指向的就是数据,不带头结点 **/
	struct node *p=head;
	while(p!=0)
	{
		printf("%d ",p->data);
		p=p->next;
	}
	printf("\n");	
}

七、完整程序

#include<stdio.h>

struct node
{
	int data;
	struct node *next;	
};

void print_link_list(struct node *head)
{/*//head指向的就是数据,不带头结点 **/
	struct node *p=head;
	while(p!=0)
	{
		printf("%d ",p->data);
		p=p->next;
	}
	printf("\n");	
}

int main(void)
{
	struct node *head, a,b,c,d,e;	
	head=0;//空指针
	a.data=5;a.next=0;
	b.data=8;b.next=0;
	c.data=1;c.next=0;
	d.data=2;d.next=0;
	e.data=11;e.next=0;	
	head=&a;
	a.next=&b;
	b.next=&c;
	c.next=&d;
	d.next=&e;	
	print_link_list(head);
	return 0;
}

程序运行结果: 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章