静态链表
#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已销毁
*/
}