zoukankan      html  css  js  c++  java
  • 链表——新建链表

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct node
    {
        int data;
        struct node *pNext;
    };
    
    //新建node
    struct node * create_node(int data)
    {
        struct node *p = (struct node *) malloc (sizeof(struct node));
    	
        if(NULL == p)
        {
            printf("malloc error!.
    ");
            return NULL;
        }
        memset(p, 0, sizeof(struct node));
        p->data = data;
        p->pNext = NULL;
    
        return p;
    }
    
    void insert_tail(struct node *pH, struct node *newp)
    {
        struct node * p = pH;
        while(NULL != p->pNext)
        {
            p = p->pNext;
        }
        p->pNext = newp;
    }
    
    void insert_head(struct node * pH, struct node *newp)
    {
        newp->pNext = pH->pNext;
        pH->pNext = newp;
    }
    
    //excluding header data
    void list_for_each_1(struct node *pH)
    {
        struct node *p = pH->pNext;
        printf("------------begin------------
    ");
        while(NULL != p->pNext)
        {
            printf("node data : %d.
    ", p->data);
            p = p->pNext;
        }
        printf("node data : %d.
    ", p->data);
        printf("------------end------------
    ");
    }
    
    //excluding header data version 2 simplify
    void list_for_each_3(struct node *pH)
    {
        struct node *p = pH;
        printf("------------begin------------
    ");
        while(NULL != p->pNext)
        {
            p = p->pNext;
            printf("node data : %d.
    ", p->data);
        }
        printf("------------end------------
    ");
    }
    
    //including header data
    int list_for_each_2(struct node *pH)
    {
        struct node *p = pH;
        printf("------------begin------------
    ");
        if(NULL == p)
        {
        	printf("nothing.
    ");
        	return -1;
    	}
        while(NULL != p->pNext)
        {
            printf("node data : %d.
    ", p->data);
            p = p->pNext;
        }
        printf("node data : %d.
    ", p->data);
        printf("------------end------------
    ");
        return 0;
    }
    
    //can not delete header node data
    int delete_node(struct node * pH, int data)
    {
        struct node *p = pH;
        struct node *pPrev = NULL;
        while(NULL != p->pNext)
        {
            pPrev = p;
            p = p->pNext;
            if(p->data == data)
            {
                if(NULL == p->pNext)
                {
                    pPrev->pNext = NULL;
                    free(p);
                }
                else
                {
                    pPrev->pNext = p->pNext;
                    free(p);
                }
    			return 0;
            }
        }
        printf("no node deleted.
    ");
        return -1;
    }
    
    //can delete all node including the tail node and header node
    struct node * delete_node_2(struct node * pH, int data)
    {
        struct node *p = pH;
        struct node *pPrev = NULL;
    	pPrev = p;
        while(NULL != p) //traserval to tail
        {
    		if(p->data == data)
    		{
    			//delete header node
    			if(p->data == pH->data)
    			{
    				pH = p->pNext;
    				free(p);
    				printf("node %d deleted ok.
    ",data);
    				return pH; //delete ok!
    			}
    			else
    			{
    				//delete tail node
    				if(NULL == p->pNext)
    				{
    					pPrev->pNext = NULL;
    					free(p);
    				}
    				else
    				{
    					pPrev->pNext = p->pNext;
    					free(p);
    				}
    				printf("node %d deleted ok.
    ",data);
    				return pH;
    			}
    			printf("node %d deleted ok.
    ",data);
    		}
    		pPrev = p;
    		p = p->pNext;
        }
        return pH;
    }
    
    
    int main()
    {
        struct node * pHeader = create_node(1);
    	printf("Hello world!
    ");
    
        insert_tail(pHeader, create_node(2));
        insert_tail(pHeader, create_node(3));
        insert_head(pHeader, create_node(4));
        insert_head(pHeader, create_node(5));
        list_for_each_2(pHeader);
    	pHeader = delete_node_2(pHeader, 3);
    	pHeader = delete_node_2(pHeader, 2);
    	pHeader = delete_node_2(pHeader, 5);
    	pHeader = delete_node_2(pHeader, 4);
    	pHeader = delete_node_2(pHeader, 1);
    	list_for_each_2(pHeader);
    //    printf("1 = %d
    ",pHeader->data);
    //    printf("2 = %d
    ",pHeader->pNext->data);
    //    printf("3 = %d
    ",pHeader->pNext->pNext->data);
    //    printf("4 = %d
    ",pHeader->pNext->pNext->pNext->data);
    //    printf("5 = %d
    ",pHeader->pNext->pNext->pNext->pNext->data);
    
        return 0;
    }
    
  • 相关阅读:
    win7开启Administrator账户
    二叉树遍历
    使用NAnt提高工作效率(二)
    系统服务的最简单实现
    右键附加启动命令行
    C#开发奇技淫巧二:根据dll文件加载C++或者Delphi插件
    百度原CTO李一男经典语录
    Sql开发技巧
    使用NAnt提高工作效率(一)
    对获取config文件的appSettings节点简单封装
  • 原文地址:https://www.cnblogs.com/CodeWorkerLiMing/p/12007442.html
Copyright © 2011-2022 走看看