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\n"); 
    }
    pHead = CreateList();
    printf("If you want show the list? y/n? \n");
    while('Y' == getchar() || 'y' == getchar()){
        TraveList(pHead);
    }
    InsertNode(pHead);
    TraveList(pHead);

    DeleteNode(pHead);
    TraveList(pHead);
    SearchNode(pHead);
    printf("Test Linker\n");
    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\n"); 
        return;
    }
    pTemp = pHead;
    pTemp->pNext = NULL;

    printf("Please input length of the list\n");    
    scanf("%d",&len);

    pHead->data = len;

    for(i=1; i<=len; i++){

        printf("Please input the %dth value\n",i);
        scanf("%d",&val);

        pNODE pNEW = (pNODE)malloc(sizeof(NODE));
        if(pNEW == NULL){
            printf("Fail to set memory\n"); 
            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\n",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 \n",pHead->data);
    scanf("%d",&n);
    if(n > pHead->data){
        printf("The number is over range\n");   
        return;
    }
    printf("Please input the value\n");
    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\n");
        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 \n",pHead->data);
    scanf("%d",&n);
    if(n > pHead->data){
        printf("The number is over range\n");   
        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 \n",pHead->data);
    scanf("%d",&n);
    if(n > pHead->data){
        printf("The number is over range\n");   
        return;
    }       
    for(i=0; i<n; i++)
    {
        pTemp = pTemp->pNext;
    }
    printf("Value of the NODE is %d\n",pTemp->data);
}
posted @ 2017-04-19 14:03  小麦大叔  阅读(161)  评论(0编辑  收藏  举报