C++单项链表的构造及简单功能的实现

C++单项链表的构造及简单功能的实现_第1张图片


/*
1.带头结点
2.遍历单项链表
3.删除链表E
4.判断链表是否有序 
5.合成2个有序链表 
*/
#include "iostream"
using namespace std;

//typedef int ElemType;
typedef struct LNode{
	int data;
	LNode* next;	
}LNode,*LinkList;




int createLink(LinkList &myList);
int searchNode(LinkList &myList,int e);
int traverseLink(LinkList &mylist);
int delNode(LinkList &myList,int e);
int up_order(LinkList &mylist);
int down_order(LinkList &mylist);
int connect_link(LinkList &mylist,LinkList &A,LinkList &B);

int main(){

	//构造链表 
	cout<<"..................构造单向链表.................."<<endl;
	LinkList mylist=new LNode();
	createLink(mylist);
	
	//打印链表 
	cout<<"..................打印链表.................."<<endl;
	traverseLink(mylist);
	
	/*
	//查找链表 
	cout<<"查找元素E"<<endl;
	char ch;
	int e=ch-'0';
	cout<<"输入查找元素e"<<endl;
	cin>>e; 
	if(searchNode(mylist,e)==-1)
		cout<<"元素e不存在" <<endl;
		*/
	
	//删除元素E 
	cout<<"..................删除元素E.................."<<endl;
	char ch;
	cout<<"输入删除元素e"<<endl;
	cin>>ch; 
	int e=ch-'0';
	if(delNode(mylist,e)==-1)
		cout<<"元素e不存在" <<endl;
	traverseLink(mylist);

	
	//判断是否有序
	cout<<"..................判断是否有序.................."<<endl; 
	if(up_order(mylist)==-1&&down_order(mylist)==-1)
		cout<<"乱序"<<endl; 
	
	//连接2个链表
	//A链表 1,3
	//B链表 0,2,4,5,6
	//合成C链表是0,1,2,3,4,5,6 
	cout<<"..................合成链表.................."<<endl; 
	LinkList A,B,C;
	A=new LNode();
	B=new LNode();
	C=new LNode();
	int a[]={1,3};
	int b[]={0,2,4,5,6};
	
	LNode* p=NULL;	
	LNode* q=NULL; 
	//构造链表A 
	q=A;
	q->next=p;		
	for(int i=0;i<2;i++){
		p=new LNode();
		p->data=a[i];
		q->next=p; 
		q=p;
		q->next=NULL; 
		
	}

	
	//构造链表A
	q=B;
	q->next=p;
	for(int i=0;i<5;i++){
		p=new LNode();
		p->data=b[i];
		q->next=p; 
		q=p;
		q->next=NULL; 
		
	}
	
	//合成链表C 
	connect_link(C,A,B);
	traverseLink(C);
	
	return 0;	
}

int createLink(LinkList &myList){
	//p创建下一个节点,q保存当前节点 

	LNode* p=NULL;	
	LNode* q=NULL; 
	q=myList; 	
	q->next=p;//使用这条语句前提是mylist必须存在内存空间 
	char ch;
	cout<<"输入数字0-9创建链表,输入#号结束"<<endl;
	cin>>ch;
	while(ch!='#'){
		int data=ch-'0';
		p=new LNode();
		p->data=data;
		q->next=p; 
		q=p;
		q->next=NULL; 
		cin>>ch;
	}
	
	return 0;
}

/* 
int searchNode(LinkList &mylist,int e){
	LNode* p=mylist->next;
	int count=1;
	while(p!=NULL){
		if(p->data==e){
			cout<<count<<endl;
			return 1;
		}
		p=p->next;
		count++;
	}

	return -1;
}
*/

int traverseLink(LinkList &mylist){

	LNode* p=mylist->next;
	while(p!=NULL){
		cout<<p->data<<"\t";
		p=p->next;
	}
		
 	cout<<endl;
 	return 0;
}

int delNode(LinkList &mylist,int e){
	LNode* q=mylist;
	LNode* p=q->next;
	
	while(p!=NULL){
		if(p->data==e){
			q->next=p->next;
			delete p;
			return 1;
		}
		q=p; 
		p=p->next;
		
	}

	return -1;
}

int up_order(LinkList &mylist){
	int number=-1;
	LNode* p=mylist->next;
	while(p!=NULL){
		if(p->data<number){
			return -1;
		}
		number=p->data; 
		p=p->next;
		
	}
	cout<<"有增序" <<endl;
	return 1;
	
}


int down_order(LinkList &mylist){
	int number=10;
	LNode* p=mylist->next;
	while(p!=NULL){
		if(p->data>number){
			return -1;
		}
		number=p->data; 
		p=p->next;
		
	}
	cout<<"有降序" <<endl;
	return 1;
}

int connect_link(LinkList &mylist,LinkList &A,LinkList &B){
	
	mylist->next=NULL;
	LNode* p=mylist;
	LNode* q=NULL;
	LNode* pA=A->next;
	LNode* pB=B->next;
	int number;
	while(pA!=NULL&&pB!=NULL){
		if(pA->data<=pB->data){
			number=pA->data;
			pA=pA->next;
		}else{
			
			number=pB->data;
			pB=pB->next;
		}
		q=new LNode();
		q->data=number;
		q->next=NULL;
		p->next=q;
		p=q;
		
	}
	while(pA!=NULL){
		number=pA->data;
		pA=pA->next;
		
		q=new LNode();
		q->data=number;
		q->next=NULL;
		p->next=q;
		p->next=q;
		p=q;
	}
	while(pB!=NULL){
		number=pB->data;
		pB=pB->next;
		
		q=new LNode();
		q->data=number;
		q->next=NULL;
		p->next=q;
		p=q;
	}
	
		
}


你可能感兴趣的:(C++)