(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;
}