• C实现单链表


    typedef int DataType;
    typedef struct ListNode
    {
    	DataType data;
    	struct ListNode* next;
    }ListNode;
    
    //初始化链表
    void InitList(ListNode** pphead)
    {
    	*pphead = NULL;
    }
    
    //创建节点
    ListNode* BuyNode(DataType x)
    {
    	ListNode* tmp = (ListNode*)malloc(sizeof(ListNode));
    	assert(tmp);
    	tmp->data = x;
    	tmp->next = NULL;
    	return tmp;
    }
    
    //尾插
    void PushBack(ListNode** phead,DataType x)
    {
    	if(NULL == *phead)
    	{
    		*phead = BuyNode(x);
    	}
    	else
    	{
    		ListNode* tial = *phead;
    		while(tial->next != NULL)
    		{
    			tial = tial->next;
    		}
    		tial->next = BuyNode(x);
    	}
    }
    
    //打印
    void Print(ListNode* phead)
    {
    	ListNode* tmp = phead;
    	while(tmp != NULL)
    	{
    		printf("%d->",tmp->data);
    		tmp = tmp->next;
    	}
    	printf("NULL");
    	printf("
    ");
    }
    
    //前插
    void PushFront(ListNode** phead,DataType x)
    {
    	if(*phead == NULL)
    	{
    		*phead = BuyNode(x);
    	}
    	else
    	{
    		ListNode* tmp = BuyNode(x);
    		tmp ->next = *phead;
    		*phead = tmp;
    	}
    }
    
    //尾删
    void PopBack(ListNode** phead)
    {
    	if(*phead == NULL)
    	{
    		printf("kd");
    		return;
    	}
    	else
    	{
    		ListNode* tmp = *phead;
    		(*phead) = (*phead)->next;
    		free(tmp);
    	}	 
    }
    
    //找节点
    ListNode* Find(ListNode* phead,DataType x)
    {
    	if(NULL == phead)
    	{
    		printf("KONG");
    		return;
    	}
    	else
    	{
    		ListNode* cur = phead;
    		while(cur)
    		{
    			if(cur->data = x)
    			{
    				return cur;
    			}
    			cur = cur->next;
    		}
    		return cur;
    	}
    }
    
    //插入
    void Insert(ListNode* pos,DataType x)
    {
    	ListNode* tmp = BuyNode(x);
    	tmp->next = pos->next;
    	pos->next = tmp;
    }
    
    //翻转单链表
    ListNode* Reverse(ListNode* phead)
    {
    	ListNode* newhead = NULL;
    	ListNode* tmp = phead;
    	while(tmp)
    	{
    		ListNode* cur = tmp;
    		tmp = tmp->next;
    		cur->next = newhead;
    		newhead = cur;
    		/*ListNode* cur = tmp->next;
    		tmp->next = newhead;
    		newhead = tmp;
    		tmp = tmp->next;*/
    	}
    	return newhead;
    }
    
    //从尾到头打印单链表
    void printListFromTailToHead(ListNode* head)
        {
            if(head == NULL)
                {
                	return;           
           	    }
          	ListNode *newhead = NULL;
            ListNode *cur = head;
            while(cur)
                {
             		ListNode *tmp = cur;
                	cur = cur->next;
                	tmp->next = newhead;
                	newhead = tmp;
                }
            ListNode *p = newhead;
            while(p)
                {
                	cout<<p->val<<endl;
                    p = p->next;            
                }
        }

  • 相关阅读:
    GridControl控件绑定RepositoryItemImageComboBox 作为下拉框使用
    ASP.NET MVC 扩展数据验证 转
    ASP.NET MVC Razor HtmlHelper扩展和自定义控件
    Thread锁 Monitor类、Lock关键字和Mutex类
    C# Thread.Join()用法的理解 转
    Redis集群高可用
    .Net Core中GC分析及调优总结-干货
    数据库之MySQL部署MGC方案(四)
    数据库之MySQL部署PXC方案(三)
    数据库之MySQL部署MGR方案(二)
  • 原文地址:https://www.cnblogs.com/melons/p/5791889.html
Copyright © 2020-2023  润新知