• 单链表


           遇到的问题:主要还是指针问题,还有算法思想要清晰。

    在删除时对于相同的数只能删除一次。不知什么原因。在GCC上运行却可以删除相同的数多次。    

    代码:

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    typedef struct ListNode
    {
    	int data;
    	struct ListNode *next;
    }*List;
    
    void createList(List &list,int arr[],int n)
    {
    	 List head,node;
    	 list=(List)malloc(sizeof(List));
    	 list->next=NULL;
    	 head=list;
    	 for(int i=0;i<n;i++)
    	 {
             node=(List)malloc(sizeof(List));
    		 if(!node)
    			 cout<<"内存分配失败."<<endl;
    		 else
    		 {
    			 node->data=arr[i];
    			 node->next=head->next;
    			 head->next=node;
    		 }
    	 }
    
    }
    
    void insertListNode(List list,int elem)
    {
    	 List node,head,pre;
    	 node=(List)malloc(sizeof(List));
    	 node->data=elem;
    	 node->next=NULL;
         head=list->next;
    	 pre=list;
    	 while(head)
    	 {
    		 if(head->data>elem)
    		 {  
    			 pre=head;
    			 head=head->next;
    			 if(head==NULL)
    			 {
    				 pre->next=node;
    				 break;
    			 }
    		 }
    		 else
    		 {
    			 node->next=pre->next;
    			 pre->next=node;
    			 break;
    		 }
    	 }
    
    }
    
    void deleteListNode(List list,int elem)
    {
    	List head,pre,node;
    	head=list->next;
    	pre=list;
    	while(head)
    	{
    		if(head->data==elem)
    		{
    			node=head;
    			pre->next=head->next;
    	        break;
    		}
    		else
    		{
    			pre=head;
    			head=head->next;
    			if(head->next==NULL)
    			{   
    				if(head->data==elem)
    				pre->next=NULL;
    				free(head);
    				break;
    			}
    		}
    	}
    }
    void merge(List list1,List list2)
    {
    	 List head=list1->next;
    	 while(head)
    	 {
    		head=head->next;
    		 if(head->next==NULL)
    			break;
    	 }
    	 head->next=list2->next;
    }
    
    void display(List list)
    {
    	List head;
    	head=list->next;
    	while(head)
    	{
    		cout<<head->data<<"->";
    		head=head->next;
    	}
    	cout<<endl;
    
    }
    
    int main()
    {
    	List list,list2;
    	int arr[]={1,5,74,3,5,8};
    	int arr2[]={4,38,45,2};
    	createList(list,arr,6);
    	createList(list2,arr2,4);
    	cout<<"display list:";
    	display(list);
    
    	insertListNode(list,23);
    	cout<<"display list:";
    	display(list);
    	insertListNode(list,6);
    	cout<<"display list:";
    	display(list);
    
    	deleteListNode(list,5);
    	cout<<"display list:";
    	display(list);
    
    	merge(list,list2);
    	cout<<"display list:";
    	display(list);
    	return 0;
    }
    

    运行结果:

              

  • 相关阅读:
    数据中心基础
    云计算基础
    C++ 沉思录 ——句柄——智能指针改写
    Linux 网卡驱动相关——02
    读书笔记(三):【SQL Server 2005 Performance Tuning性能调校】(1):【性能调校概观】
    SQL Server 索引中include的魅力(具有包含性列的索引)
    Flex编程
    ZedGraph使用经验
    Firefox 扩展插件推荐
    读书笔记(三):【SQL Server 2005 Performance Tuning性能调校】(0):【开篇】
  • 原文地址:https://www.cnblogs.com/xshang/p/3022547.html
Copyright © 2020-2023  润新知