链表学习(c语言)

(1)静态链表

#include<stdio.h>
#
 struct Node
 {
 int date;
 struct Node* next;	
};
 int main()
 {
	struct Node Node1={1,NULL};
	struct Node Node2={2,NULL};
	struct Node Node3={3,NULL};
	Node1.next=&Node2;
	Node2.next=&Node3; 
	return 0; 	
 }

(2)插入结点

#include<stdio.h>
#include<stdlib.h>
struct Node
{
	int data;
	struct Node* next;
 };
struct Node* createlist()//创建链表函数 
{
	struct Node* headNode=(struct Node*)malloc(sizeof(struct Node));
	//headnode成为了结构体变量
	//变量使用前必须被初始化
	//headnode->data=1;
	headNode->next=NULL; 
	return headNode;
}
struct Node* createNode(int data)//创建结点,用来插入结点 
{
	struct Node* newNode=(struct Node*)malloc(sizeof(struct Node)); 
	newNode->data=data;//初始化
	newNode->next=NULL;
	return newNode; 
 } 
void printlist(struct Node* headNode)//打印链表函数
{
	struct Node* p=headNode-> next;//指针p,从第二个结点开始打印
	while(p)
	{
		printf("%d",p->data);
		p=p->next;
	 } 
	printf("\n");
 } 
void insertNodehead(struct Node* headNode,int data)//插入结点函数,(插入哪个链表。插入数据)
{
	struct Node* newNode=createNode(data);//创造一个结点
	newNode->next=headNode->next;
	headNode->next=newNode; 	
 } 
int main()
{
	struct Node* list=createlist();//创建一个链表 
	insertNodehead (list,1);
	insertNodehead (list,2);
	insertNodehead (list,3);
	printlist(list);
	system("pause");//不懂 
	return 0;
 } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值