静态链表与动态链表

静态链表

#include<iostream>
using namespace std;

#include<cstring>
#include <stdlib.h>

//链表结点类型定义
struct linknode
{
	int  data;
	struct linknode *next;
 } ;
 
 void text01()
 {
 	//静态链表 
 	struct linknode node1={10,NULL};
 	struct linknode node2={20,NULL};
 	struct linknode node3={30,NULL};
 	struct linknode node4={40,NULL};
 	struct linknode node5={50,NULL};
 	struct linknode node6={60,NULL};
 	node1.next=&node2;
 	node2.next=&node3;
 	node3.next=&node4;
 	node4.next=&node5;
 	node5.next=&node6;
 	//如何遍历链表
 	//先定义一个辅助指针变量
	  struct linknode *pcurrent=&node1;
	  while(pcurrent!=NULL)
	  {
	  	cout<<pcurrent->data<<" ";
	  	//指针移动到下一个元素的地址 
	  	pcurrent=pcurrent->next;
	  }
	  
 	
 }
int main()
{
	text01();

	return 0;	
 } 
 

动态链表

#include<iostream>
using namespace std;
#include<malloc.h>
#include<cstring>
#include <stdlib.h>

//链表结点类型定义

struct linknode
{
	int  data;
	struct linknode* next ;
 };
 
 //初始化链表,即生成表头 
 struct linknode *linklist()
 {
 	struct linknode *header=(struct linknode*)malloc(sizeof(struct linknode) );
 	header->data=-1;
 	header->next=NULL;
 	//尾部指针
	 struct linknode *prear=header;
	 int val=-1;
	 while(true)
	 {
	 	printf("输入插入的数据:\n");
	 	scanf("%d",&val);
	 	if(val==-1)
		 {
		 	break;
		  } 
		  //先创建新节点
		  struct linknode *newnode=(struct linknode*)malloc(sizeof(struct linknode));
		  newnode->data=val;
		  newnode->next=NULL;
		  //新节点插入到链表中
		  prear->next=newnode;
		  prear=newnode; 
		
	  } 
 	
 	
 	return header;
 	
  } 
 //在值为oldval的节点位置插入newval,未找到则插入尾部 
 void  insertbyvalue(struct linknode *header,int oldval,int newval)
 {
 	struct linknode *ppre=header;
	 struct linknode *pcur=header->next;
	 struct linknode *newnode=(struct linknode*)malloc(sizeof(struct linknode));
 	if(NULL==header)
 	{
 	newnode->next=pcur;
	 ppre->next=newnode;
	 newnode->data=newval;
	 }

	 while(pcur->data!=oldval)
	 {
	 	ppre=pcur;
	 	pcur=pcur->next;
	 	if(pcur==NULL)
	 	{
	 		cout<<"no find\n";
	 		break;
		 }
	 }
	 newnode->next=pcur;
	 ppre->next=newnode;
	 newnode->data=newval;
 	
 	
 	
 }
 
 
 
 
 //遍历
 void foreach(struct linknode *header)
 {
 	if(NULL==header)
 	{
 		return;
	 }
	 struct linknode *pcurrent=header->next;
	 while(pcurrent!=NULL)
	 {
	 	cout<<pcurrent->data<<" ";
	 	pcurrent=pcurrent->next;
	 }
	 
  } 
 
 //清空
 void clear(struct linknode* header)
 {
 	if(NULL==header)
 	{
 		return;
	 }
	 //辅助指针变量
	 struct linknode *pcur=header->next;
	 while(pcur!=NULL)
	 {
	 	//先保存下一个结点的地址
		 struct linknode* pnext=pcur->next;
		  //释放当前 结点内存 
		  free(pcur);
		  //pcur指向下一个结点
		  pcur=pnext; 
	  } 
 	header->next=NULL;//注意上面的操作只释放了header后的元素,但header->next指向的地址还在,要该成NULL 
  } 
 
 void removebyval(struct linknode* header,int val)
 {
 	if(NULL==header)
 	{
 		return;
	 }
	 struct linknode* pcur=header->next;
	 struct linknode* ppre=header;
	 while(pcur->data!=val)
	 {
	 	ppre=pcur;
	 	pcur=pcur->next;
	 	if(pcur==NULL)
	 	{
	 		return;
		 }
	 }
	 ppre->next=pcur->next;
	 free(pcur);
 	
  } 
 //销毁
 void destroy(struct linknode *header)
 {
 	if(NULL==header)
 	{
 		return;
	 }
	 //辅助指针变量
	 struct linknode *pcur=header;
	 while(pcur!=NULL)
	 {
	 	//先保存下一个节点位置
		 struct linknode *pnext=pcur->next;
		 //释放当前节点内存
		 cout<<endl<<pcur->data<<"已销毁"<<endl; 
		 free(pcur); 
		 pcur=pnext;
	  } 
  } 
 
int main()
{
	struct linknode *list;
	list=linklist();
	foreach(list);
	cout<<endl;
	insertbyvalue(list,20,222);
	foreach(list);
	cout<<endl<<"清空:";
	/*clear(list);
	foreach(list);*/
	cout<<endl;
	removebyval(list,26);
	foreach(list);
	destroy(list);
	return 0;	
/*
输入插入的数据:
45
输入插入的数据:
42
输入插入的数据:
20
输入插入的数据:
26
输入插入的数据:
2
输入插入的数据:
-1
45 42 20 26 2
45 42 222 20 26 2
清空:
45 42 222 20 2
-1已销毁

45已销毁

42已销毁

222已销毁

20已销毁

2已销毁
*/
 } 
 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linalw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值