数据结构(一)链表

数据结构(一)链表

1、单链表的后插创建和基本操作

#include<iostream>
using namespace std;
struct LinkNode                           //链表结点的定义 
{
	int data;
	LinkNode *next;
 };
 
 void CreateLink(LinkNode *&head)            //后插法创建链表算法 
 {
 	LinkNode *p,*q=head;
 	int data;
 	while(1)
 	{
 		cin>>data;
 		if(data<0)break;
 		p=new LinkNode;
 		p->data=data;
 		p->next=NULL;
 		q->next=p;
 		q=p;
	 }
 	
 }
 
 void Show(LinkNode *head)							//遍历链表算法 
 {
 	for(LinkNode *p=head->next;p;p=p->next)
 	cout<<p->data;
 }
 
 void DeleteLinkNode(LinkNode *&head,int key)				//删除指定结点的算法 
 {
 	LinkNode *q=head;
 	for(LinkNode *p=head->next;p->next->next;p=p->next,q=q->next)
 	if(p->data==key) 
 	{
 		q->next=p->next;	
	 }
  } 
  
  void InSertLinkNode(LinkNode *&head,int key)					//在指定结点后面插入一个结点的算法 
  {
  	LinkNode *q=head,*p=new LinkNode;
  	p->data=key;
  	while(q->next!=NULL)
  	{
  		q=q->next;
	  }
	  q->next=p;
	  p->next=NULL;	
  }
 
 int main()
 {
 	LinkNode *head=new LinkNode;
 	head->next=NULL;
 	CreateLink(head);
 	Show(head);
 	DeleteLinkNode(head,5);
 	Show(head);
 	InSertLinkNode(head,9);
 	Show(head);
 	return 0;
 	
 }

2、双向链表的创建与基本操作

 #include<iostream>
 using namespace std;
 struct LinkNode
 {
 	int data;
 	LinkNode *prior,*next;
  } ;
  void Create(LinkNode *&head,int n)                        //创建双向链表 
  {
  	LinkNode *p,*q=head;
  	int data;
  	for(int i=0;i<n;i++)
  	{
  		cin>>data;
  		p=new LinkNode;
  		p->data=data;
  		q->next=p;
  		p->prior=q;
  		q=p;
	  }
	  head->prior=p;
	  p->next=head;
  }
  
  void NextShow(LinkNode *head)										//双向链表后遍历 
  {
  	for(LinkNode *p=head->next;p!=head;p=p->next)
  	cout<<p->data<<endl;
	//cout<<head->prior->data;
  	
  }
  
  void InSert(LinkNode *&head,int key)								//双向链表插入操作 
  {
  	LinkNode *p=new LinkNode;
  	p->data=key;
  	p->next=head;
  	p->prior=head->prior;
  	head->prior->next=p;
  	head->prior=p;
  }
  
  void Delete(LinkNode *&head,int key)								//双向链表删除结点操作 
  {
  	LinkNode *p=head->next;
  	for(p;p!=head;p=p->next)
  	if(p->data==key)
  	{
  		p->next->prior=p->prior;
		p->prior->next=p->next;	
	}	
  }
  
  int main()
  {
  	LinkNode *head=new LinkNode;
  	Create(head,5);
  	NextShow(head);
  	InSert(head,8);
  	cout<<endl;
  	NextShow(head);
  	Delete(head,3);
  	NextShow(head);
  	return 0;
  }

3、循环链表的创建与基本操作

#include<iostream>
 using namespace std;
 struct LinkNode
 {
 	int data;
 	LinkNode *next;
  } ;
  
  void Create(LinkNode *&head,int n)						//创建循环链表 
  {
  	LinkNode *p,*q=head;
  	int data;
  	for(int i=0;i<n;i++)
  	{
  		p=new LinkNode;
  		cin>>data;
  		p->data=data;
  		q->next=p;
  		p->next=NULL;
  		q=p;
	  }
	  p->next=head;
  }
  
  void Show(LinkNode *head)										//遍历循环链表 
  {
  	LinkNode *p=head->next;
  	for(p;p!=head;p=p->next)
  	cout<<p->data<<endl;	
  }
  
  int main()
  {
  	LinkNode *head=new LinkNode;
  	Create(head,5);
  	Show(head);
  	return 0;
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值