zoukankan      html  css  js  c++  java
  • C:单链表的简单实现

    前言

    今天整理资料的时候翻出来的文件,发现是以前学习数据结构的时候写的代码,当初是看郝凯老师的视频学习的C语言的数据结构,下面是对于一个单链表的简单的实现。

    /**
     ****************************************************************************
     *@file     SingleLinker.c
     *@version  V1.00
     *@brief    
     *@date     2015/11/24
     *****************************************************************************
     */
    #include <stdio.h>
    #include <malloc.h>
    
    typedef struct Node{
        int data;       
        struct Node *pNext;  
    }NODE,*pNODE;
    
    pNODE CreateList(void);
    void TraveList(pNODE pHead);
    void InsertNode(pNODE pHead);
    void DeleteNode(pNODE pHead);
    void SearchNode(pNODE pHead);
    
    int main(void)
    {
        char Temp = '0';    
        pNODE pHead;
        pHead = (pNODE)malloc(sizeof(NODE));
    
        if(pHead == NULL){
            printf("Fail to set memory
    "); 
        }
        pHead = CreateList();
        printf("If you want show the list? y/n? 
    ");
        while('Y' == getchar() || 'y' == getchar()){
            TraveList(pHead);
        }
        InsertNode(pHead);
        TraveList(pHead);
    
        DeleteNode(pHead);
        TraveList(pHead);
        SearchNode(pHead);
        printf("Test Linker
    ");
        return;
    }
    
    /**
     ****************************************************************************
     * @brief   CreateList
     * @param   none
     * @return  pNODE
     * @note
     *****************************************************************************
     */
    pNODE CreateList(void)
    {
        int len;
        int val;
        int i;
        pNODE pHead = NULL;
        pNODE pTemp;
        pHead = (pNODE)malloc(sizeof(NODE));        
        if(pHead == NULL){
            printf("Fail to set memory
    "); 
            return;
        }
        pTemp = pHead;
        pTemp->pNext = NULL;
    
        printf("Please input length of the list
    ");    
        scanf("%d",&len);
    
        pHead->data = len;
    
        for(i=1; i<=len; i++){
    
            printf("Please input the %dth value
    ",i);
            scanf("%d",&val);
    
            pNODE pNEW = (pNODE)malloc(sizeof(NODE));
            if(pNEW == NULL){
                printf("Fail to set memory
    "); 
                return;
            }
    
            pNEW->data = val;
            pTemp->pNext = pNEW;
            pNEW->pNext = NULL;
            pTemp = pNEW;       
        }   
    
        return pHead;
    }
    
    /**
     ****************************************************************************
     * @brief   TraveList
     * @param   [in]pHead
     * @return  none
     * @note
     *****************************************************************************
     */
    void TraveList(pNODE pHead)
    {
        int i = 0;
        pNODE pTemp = pHead->pNext;
        while(pTemp != NULL){
            i++;        
            printf("the %dth Value is:%d
    ",i,pTemp->data); 
            pTemp = pTemp->pNext;
        }
        return;
    }
    /**
     ****************************************************************************
     * @brief   InsertNode
     * @param   [in]pPostion
     * @param   [in]value
     * @return  none
     * @note
     *****************************************************************************
     */
    void InsertNode(pNODE pHead)
    {
        int i,n,val;    
        pNODE pTemp = pHead;    
        printf("Which node do you want to insert?1~%d 
    ",pHead->data);
        scanf("%d",&n);
        if(n > pHead->data){
            printf("The number is over range
    ");   
            return;
        }
        printf("Please input the value
    ");
        scanf("%d",&val);
    
        for(i=0; i<n; i++){
            pTemp = pTemp->pNext;   
        }
        pNODE pInsert = (pNODE)malloc(sizeof(NODE));
        if(pInsert == NULL){
            printf("Fail to set memory
    ");
            return;
        }
        pHead->data +=1;
        pInsert->data = val;
        pInsert->pNext = pTemp->pNext;
        pTemp->pNext = pInsert;
        return;
    }
    /**
     ****************************************************************************
     * @brief   DeleteNode
     * @param   [in]pHead
     * @return  none
     * @note
     *****************************************************************************
     */
    void DeleteNode(pNODE pHead)
    {
        int i,n,val;
        pNODE pTemp = pHead;
        pNODE pDel = pTemp->pNext;  
        printf("Which node do you want to delete?1~%d 
    ",pHead->data);
        scanf("%d",&n);
        if(n > pHead->data){
            printf("The number is over range
    ");   
            return;
        }       
    
        for(i=0; i<n-1; i++)
        {
            pTemp = pTemp->pNext;
            pDel = pDel->pNext;
        }
        pHead->data -=1;
        pTemp->pNext = pDel->pNext;
    }
    
    /**
     ****************************************************************************
     * @brief   SearchNode
     * @param[in]   pHead
     * @return  none
     * @note
     *****************************************************************************
     */
    void SearchNode(pNODE pHead)
    {
        int i,n;
        pNODE pTemp = pHead;
        printf("Which node do you want to Search?1~%d 
    ",pHead->data);
        scanf("%d",&n);
        if(n > pHead->data){
            printf("The number is over range
    ");   
            return;
        }       
        for(i=0; i<n; i++)
        {
            pTemp = pTemp->pNext;
        }
        printf("Value of the NODE is %d
    ",pTemp->data);
    }
  • 相关阅读:
    C#应用程序中的输入法
    201871010128杨丽霞《面向对象程序设计(java)》第六七周学习总结
    201871010128杨丽霞《面向对象程序设计(java)》第一周学习总结
    201871010128杨丽霞《面向对象程序设计(java)》第二周学习总结
    201871010128杨丽霞《面向对象程序设计(java)》第四周学习总结
    201871010128杨丽霞《面向对象程序设计(java)》第七周学习总
    201871010128杨丽霞《面向对象程序设计(java)》第十周学习总结
    201871010128杨丽霞《面向对象程序设计(Java)》第十一周学习总结
    201871010128杨丽霞《面向对象程序设计(java)》第八周学习总结
    201871010128杨丽霞《面向对象程序设计(Java)》第十二周学习总结
  • 原文地址:https://www.cnblogs.com/unclemac/p/12783442.html
Copyright © 2011-2022 走看看