• 【数据结构】链表操作示例


    #include<cstdlib>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<iostream>
    #include<algorithm> 
    
    using namespace std;
    
    typedef struct{
    	char key[10];
    	char name[20];
    	int age;
    }Data;
    
    typedef struct Node{
    	Data nodeData;
    	struct Node * nextNode;
    }CLType;
    
    CLType * CLAddEnd(CLType * head, Data nodeData){  	//追加结点 
    	CLType * node, * htemp;
    	if(!(node=(CLType*)malloc(sizeof(CLType)))){
    		cout<<"申请内存失败!
    ";
    		return NULL;
    	}
    	else{
    		node->nodeData=nodeData;
    		node->nextNode=NULL;
    		if(head==NULL){
    			head=node;
    			return node;
    		}
    		htemp=head;
    		while(htemp->nextNode!=NULL){
    			htemp=htemp->nextNode;
    		}
    		htemp->nextNode=node;
    		return head;
    	}
    }
    
    CLType * CLAddFirst(CLType * head, Data nodeData){
    	CLType *node;
    	if(!(node=(CLType*)malloc(sizeof(CLType)))){
    		cout<<"申请内存失败!
    ";
    		return NULL;
    	}
    	else{
    		node->nodeData=nodeData;
    		node->nextNode=head;
    		head=node;
    	}
    } 
    
    CLType * CLFindNode(CLType * head, char *key){
    	CLType *htemp;
    	htemp=head;
    	while(htemp){
    		if(strcmp(htemp->nodeData.key, key)==0){
    			return htemp;
    		}
    		htemp=htemp->nextNode;
    	}
    	return NULL;
    }
    
    CLType * CLInsertNode(CLType * head, char * findkey, Data nodeData){
    	CLType * node, * nodetemp;
    	if(!(node=(CLType *)malloc(sizeof(CLType)))){
    		cout<<"内存申请失败!
    ";
    		return 0;
    	}
    	node->nodeData=nodeData;
    	nodetemp=CLFindNode(head, findkey);
    	if(nodetemp){
    		node->nextNode=nodetemp->nextNode;
    		nodetemp->nextNode=node;
    	}
    	else{
    		cout<<"未找到正确的插入位置!
    ";
    		free(node);
    	}
    	return head;
    }
    
    int CLDeleteNode(CLType * head, char * key){
    	CLType * node, * htemp;
    	htemp=head;
    	node=head;
    	while(htemp){
    		if(strcmp(htemp->nodeData.key, key )==0){
    			node->nextNode=htemp->nextNode;
    			free(htemp);
    			return 1;
    		}
    		else{
    			node= htemp;
    			htemp = htemp -> nextNode;
    		}
    		return 0;
    	}
    }
    
    int CLLength(CLType * head){
    	CLType * htemp;
    	int Len=0;
    	htemp=head;
    	while(htemp){
    		Len++;
    		htemp=htemp->nextNode;
    	}
    	return Len;
    }
    
    void CLAllNode(CLType *  head){
    	CLType * htemp;
    	Data nodeData;
    	htemp = head;
    	cout<<"当前链表共有"<<CLLength(head)<<"个结点。链表所以数据如下:
    ";
    	while(htemp){
    		nodeData=htemp->nodeData;
    		cout<<"结点"<<nodeData.key<<nodeData.name<<nodeData.age<<endl;
    		htemp=htemp->nextNode; 
    	} 
    }
    
    int main(){
    	CLType *node, *head=NULL;
    	Data nodeData;
    	char key[10], findkey[10];
    	cout<<" 链表测试。请输入链表中的数据,格式为:关键字 姓名 年龄
    ";
    	do{
    		fflush(stdin);
    		cin>>nodeData.key;
    		if(strcmp(nodeData.key, "0")==0){
    			break;
    		}
    		else{
    			cin>>nodeData.name>>nodeData.age;
    			head=CLAddEnd(head, nodeData);
    		}
    	}while(1);
    	
    	CLAllNode(head);
    	
    	cout<<"
     演示插入结点,输入插入结点的关键字: ";
    	cin>>findkey;
    	cout<<"输入插入结点的数据(关键字 姓名 年龄):";
    	cin>>nodeData.key>>nodeData.name>>nodeData.age;
    	head=CLInsertNode(head, findkey, nodeData);
    	CLAllNode(head);
    	
    	cout<<"
    演示删除结点,输入要删除的关键字:";
    	fflush(stdin);
    	cin>>key;
    	CLDeleteNode(head, key);
    	CLAllNode(head);
    	
    	cout<<"
     演示在链表中查找,输入查找关键字:";
    	fflush(stdin);
    	cin>>key;
    	node=(CLFindNode(head, key));
    	if(node){
    		nodeData=node->nodeData;
    		cout<<"关键字"<<key<<"对应的结点为:"<<nodeData.key<<nodeData.name<<nodeData.age; 
    	} 
    	else{
    		cout<<"在链表中未找到关键字为"<<key<<"的结点!"<<endl; 
    	}
    } 
    

      

  • 相关阅读:
    关于vue 自定义组件的写法与用法
    常用的几种监控服务器性能的Linux命令
    Web自动化测试入门
    接口测试入门
    Selenium+IDEA(java+maven+testNG)+Jenkins环境搭建
    Jmeter+ant+Jenkins环境搭建
    iframe在移动端的缩放
    CSS3的颜色渐变效果
    Hexo建博小结
    Ajax基本概念和原理
  • 原文地址:https://www.cnblogs.com/dragonir/p/5008198.html
Copyright © 2020-2023  润新知