链表的实现

链表,自己看的,本来想第一个节点处没元素,最后一个节点处差不多跟网上其他人的链表差不多,但是发现不能理解,然后就自己瞎写....竟然还写出了一个奇怪的类似于链表一样的东西....还没写完,近期更新,目前仅供自己查看...

现在完善一点了,然后再看看链表的实现,再完善一下功能,离400来行还差一百来行啊!

挖坟*3.。。还是没完。。。十个功能了,

最近突然发现用单向链表没办法找到父节点,因此,现在再次学习一下双向链表,其实第九个功能(反转链表就可以直接用双向链表输出就行了),我在后面的双向链表中只完成了创建,查找(双向查找),其他的都和单向链表一样,就不多写了

挖坟*4;今天再看了一下之前的链表..不堪入目啊!于是重新写了一边,好多了,当时完成这个链表花了大半天,现在仅仅半个小时,果然当时还是菜的很啊.,现在这个代码已经不能说是太误人子弟了,还是可以看着学的,但也没心思再写注释了,就一个重要的地方

一个数组模拟链表,最近学的template试了一下,感觉用的更顺手了:

//#pragma comment(linker, "/STACK:1024000000,1024000000") 
 
#include<stdio.h>
#include<string.h>  
#include<math.h>  
#include<cstdlib>

//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<fstream>
#include<iostream>  
#include<algorithm>  
using namespace std;  
 
#define ll long long  
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
//std::ios::sync_with_stdio(false);
const int MAXN=1e5+10;
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;

template<typename T>
struct node{
	private:
		int size,max;
		T* head;
	public:
		node()
		{
			//cout<<1<<endl;
			head=NULL;
			size=0;
			max=0;
		}
		void push(int i,T data);
		bool Empty();
		void delate(int i);
		void clear();
		int lenth();
		T show_i(int i);
		void show_pre(int i);
		void show_suc(int i);
		void show_linked();
};
template<typename T>
void node<T>::push(int i,T data)
{
	if(i<0)
	{
		cout<<"error!"<<endl;
		return ;
	}
	//cout<<i<<" "<<data<<" "<<max<<endl;
	if(i>=max)
	{
		
		T* p=(T*)realloc(head,(i+5)*sizeof(T));
		//cout<<i<<" "<<data<<" "<<max<<endl;
		if(p==NULL)
		{
			cout<<"error!"<<endl;
			return ;
		}
		head=p;
		max=i+5;
	}
	
	head[i]=data;
	size++;
	cout<<"seccess"<<endl;
}
template<typename T>
void node<T>::delate(int i)
{
	size--;
	for(int j=i;j<size;++i)
		head[i]=head[i+1];
}
template<typename T>
void node<T>::clear()
{
	size=0;
}
template<typename T>
bool node<T>::Empty()
{
	return size?0:1;
}
template<typename T>
int node<T>::lenth()
{
	return size;
}
template<typename T>
T node<T>::show_i(int i)
{
	return head[i];
}
template<typename T>
void node<T>::show_pre(int i)
{
	if(i==0)
		cout<<"Error:无前驱!"<<endl;
	else
		cout<<head[i-1]<<endl;
}
template<typename T>
void node<T>::show_suc(int i)
{
	if(i==size-1)
		cout<<"Error:无后继!"<<endl;
	else
		cout<<head[i+1]<<endl;
}
template<typename T>
void node<T>::show_linked()
{
	for(int i=0;i<size;++i)
		cout<<head[i]<<" ";
	cout<<endl;
}
/*
7
0 1
7
1 2
*/
void help()
{
	cout<<"1----清空线性表"<<endl;
    cout<<"2----判断线性表是否为空"<<endl;
    cout<<"3----求线性表长度"<<endl;
    cout<<"4----获取线性表指定位置元素"<<endl;
    cout<<"5----求前驱"<<endl;
    cout<<"6----求后继"<<endl;
    cout<<"7----在线性表指定位置插入元素"<<endl;
    cout<<"8----删除线性表指定位置元素"<<endl;
    cout<<"9----显示线性表"<<endl;
	cout<<"     退出,输出一个负数!"<<endl;
}
int main()
{
	help();
	node<int> p;
	int n;
	while(cin>>n)
	{//输入下表从0开始 
		if(n<0)
			break;
		else if(n==1)
			p.clear();
		else if(n==2)
		{
			if(p.Empty())
				cout<<"线性表空"<<endl;
			else
				cout<<"线性表非空"<<endl;
		}
		else if(n==3)
			cout<<"len:"<<p.lenth()<<endl;
		else if(n==4)
		{
			int i;
			cin>>i;
			cout<<p.show_i(i)<<endl;
		}
		else if(n==5)
		{
			int i;
			cin>>i;
			p.show_pre(i);
		}
		else if(n==6)
		{
			int i;
			cin>>i;
			p.show_suc(i);
		}
		else if(n==7)
		{
			int i,data;
			cin>>i>>data;
			p.push(i,data);
		}
		else if(n==8)
		{
			int i;
			cin>>i;
			p.delate(i);
		}
		else if(n==9)
			p.show_linked();
		else
			cout<<"输出错误,重新输入"<<endl;
	}
}

代码:

//#pragma comment(linker, "/STACK:1024000000,1024000000") 
 
#include<stdio.h>
#include<string.h>  
#include<math.h>  
#include<stdlib.h>
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<fstream>
#include<iostream>  
#include<algorithm>  
using namespace std;  
 
#define ll long long  
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
//std::ios::sync_with_stdio(false);
const int MAXN=1e5+10;
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;
 
struct node{
	int data;					//数据域 
	node *nxt;					//指针域 
};
 
void createlinked(node *&head)//修改实参,注意加&
{
	head=(node*)malloc(sizeof(node));
	if(head==NULL)
	{
		cout<<"error"<<endl;
		return ;
	}
	node *p,*q=head;
	int data;
	while(cin>>data)
	{
		if(data<0)
			break;
		p=(node*)malloc(sizeof(node));
		p->data=data;
		q->nxt=p;
		q=q->nxt;
	}
	q->nxt=NULL;
}
 
void outputlinked(node *head)
{
	node *p=head->nxt;
	while(p!=NULL)
	{
		cout<<p->data<<" ";
		p=p->nxt;
	}
	cout<<endl;
}
 
int lenthlinked(node *head)
{
	int len=-1;
	node *p=head;
	while(p!=NULL)
	{
		len++;
		p=p->nxt;
	}
	return len;
}
 
int findlinked(node *head,int i)
{
	node *p=head;
	int u=-1;
	while(p!=NULL)
	{
		if(u==i)
			return p->data;
		u++;
		p=p->nxt;
	}
	cout<<"NO Find!"<<endl;
	return 0;
}
 
void replacelinked(node *head,int i,int now)
{
	int u=-1;
	node *p=head;
	while(p!=NULL)
	{
		if(u==i)
		{
			p->data=now;
			return ;
		}
		u++;
		p=p->nxt;
	}
	cout<<"NO Find!"<<endl;
}
 
void removelinked(node *head,int i)
{
	int u=0;
	node* p=head->nxt,*q=head;
	while(p!=NULL)
	{
		if(u==i)
		{
			q->nxt=p->nxt;
			free(p);
			return ;
		}
		u++;
		p=p->nxt;
		q=q->nxt;
	}
	cout<<"flase"<<endl;
}
 
void insertlinked(node *head,int i,int key)
{
	int u=-1;
	node *p=head,*q;
	while(p!=NULL)
	{
		if(u==i)
		{
			q=(node*)malloc(sizeof(node));
			q->data=key;
			q->nxt=p->nxt;
			p->nxt=q;
			return ;
		}
		u++;
		p=p->nxt;
	}
	cout<<"NO Find!"<<endl;
}

node* reverselinked(node *&head)
{
	if(head==NULL)//链表反转||链表为空 
	{
		head=(node*)malloc(sizeof(node));
		head->nxt=NULL;
		return head;
	}
	node *p=reverselinked(head->nxt);
	if(head->nxt==NULL)
		p->nxt=head;
	else
		head->nxt->nxt=head;
	head->nxt=NULL;
	return p;
}

void emptylinked(node *head)
{
	node *p=head->nxt;
	while(p!=NULL)
	{
		removelinked(head,0);
		p=head->nxt;
	}
}
/*
10 9 8 7 6 5 4 -1
0
6 1
0
0 10
*/
int main()
{
	//1,有一个表头 
	node *head=NULL;
	//2。创建一个链表
	cout<<"创建一个链表"<<endl;
	createlinked(head);
	//3.查看链表是否创建成功(看一下里面的数据) 
	cout<<"输出该链表"<<endl;
	outputlinked(head);
	//4.求链表的长度 
	cout<<"求链表长度"<<endl;
	cout<<"该链表长度为:"<<lenthlinked(head)<<endl;//求长度
	//5.在链表中查找一个值 
	cout<<"查找链表中位置为i的元素"<<endl;
	int i;
	cin>>i;
	cout<<findlinked(head,i)<<endl;	
	//6。替换链表中的某值
	cout<<"替换链表中的第i个元素"<<endl;
	int now;
	cin>>i>>now;					
	replacelinked(head,i,now);	
	outputlinked(head);			 
	//7.删除元素; 
	cout<<"删除链表中的第i个值"<<endl;
	cin>>i;					
	removelinked(head,i);		
	outputlinked(head);			
	cout<<"now,the lenth of this linked is :"<<lenthlinked(head)<<endl; 
	//8.插入新节点;
	cout<<"在第i个节点后插入一个元素x"<<endl;
	int x;
	cin>>i>>x;
	insertlinked(head,i,x);
	outputlinked(head);			
	cout<<"当前长度为:"<<lenthlinked(head)<<endl; 
	//9.反转链表 
	cout<<"反转链表"<<endl;
	head=reverselinked(head->nxt);
	outputlinked(head);
	//10.清空链表
	cout<<"清空链表"<<endl; 
	emptylinked(head);
	outputlinked(head);
	cout<<lenthlinked(head)<<endl;
	
}

 

目前运行的结果是这样的:

舒服多了

 

双向链表:(还是垃圾,但是不想更新了,以后随缘更吧)

#include<stdio.h>    
#include<string.h>    
#include<math.h>    
    
//#include<map>     
#include<set>  
#include<deque>    
#include<queue>    
#include<stack>    
#include<string>    
#include<iostream>    
#include<algorithm>    
using namespace std;    
    
#define ll long long    
#define da    0x3f3f3f3f    
#define xiao -0x3f3f3f3f    
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
struct node{
	int data;
	node *fa,*next;
};

node *build_two_way(node *head)
{
	int data;
	node *can=NULL;
	while(cin>>data)
	{
		if(data==0)
			break;
		can=(node*)malloc(sizeof(node));
		can->data=data;
		can->fa=head;
		can->next=NULL;
		head->next=can;
		head=can;
	}
	return can;
}

void positive_output_two_way(node *head)
{
	node *can=head;
	while(1)
	{
		if(can->next==NULL)
		{
			cout<<can->data<<endl;
			break;
		}
		cout<<can->data<<" ";
		can=can->next;
	}
}

void reverse_output_two_way(node *head)
{
	node *can=head;
	while(1)
	{
		if(can->fa==NULL)
		{
			cout<<can->data<<endl;
			break;
		}
		cout<<can->data<<" ";
		can=can->fa;
	}
}

int main()
{
	node *headnode=NULL;//定义一个首节点 
	headnode=(node*)malloc(sizeof(node));
	if(headnode==NULL)
	{
		cout<<"failure"<<endl;
		return 0;
	}
	else
		cout<<"successful"<<endl;
	cin>>headnode->data;
	headnode->fa=NULL;
	headnode->next=NULL;
	node *tailnode=build_two_way(headnode);//找到尾节点 
	positive_output_two_way(headnode);//headnode-》tailnode 
	reverse_output_two_way(tailnode);//tailnode-》headnode 
}

运行结果:

 

暂时先写这么多,如果想看更新直接评论就行了

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值