C语言简单实现单链表的一般操作

/**
 * author:soarHu
   verison:1.0
   date:2016/05/01
   description:"this is a linkedList operation..."
 */
#include "stdio.h"
#include "stdlib.h"

typedef struct Node{
    int value;
    struct Node *pNext;
}Node,*PNode;

PNode createLinkedList(void);
void tranverse(PNode pHead);
int getLength(PNode pHead);
int insertNode(PNode pHead,int positon,int value);
int deleteNode(PNode pHead,int position);
void appendList(PNode pHead,int value);
void getElem(PNode pHead,int position);
void priorElem(PNode pHead,int position);
void nextElem(PNode pHead);

int main(int argc, char const *argv[]){
    PNode pHead = NULL;//define root pointer to point headNode.
    pHead = createLinkedList();
    tranverse(pHead);
    if(insertLinkedList(pHead,3,88)){
        printf("insert success!");
        tranverse(pHead);
    }else{
        printf("%s\n", " insert error");
    }

    printf("what you wanna to deleted value is %d\n", deleteNode(pHead,4));
    tranverse(pHead);
    
    free(pHead);
    return 0;
}

/**
 * [createLinkedList: create the linkedList]
 * @return  [the pointer to the headNode]
 */
PNode createLinkedList(void){
    int length =0;//length of list.
    int value;
    PNode pHead = NULL;//the root pointer.
    PNode pTail = NULL;//the tail pointer or understand as the current node pointer.
    PNode pNew = NULL;//the pointer to point new node.

    printf("%s\n","please input the length you wanna to create the linkedList:");
    scanf("%d",&length);
    
    pHead = malloc( sizeof(Node) );
    pHead->pNext = NULL;
    pTail = pHead;

    if (NULL== pHead){
        printf("%s\n", "out of memory");
        exit(-1);
    }

    printf("%s\n", "please input the value of every node");

    for (int i = 0; i < length; ++i){
        scanf("%d",&value);
        pNew = malloc( sizeof(Node) );
        if (NULL==pNew){
            printf("%s\n", "out of memory");
            exit(-1);
        }else{
            pNew->value = value;//set value to the current node.
            pTail->pNext = pNew;//in order to previous node point next node.
            pTail = pNew;//in order to the tail pointer point new node. 
            pTail->pNext = NULL;//if is the last node,set the pointer null.
        }
    }
    return pHead;
}

/**
 * [tranverse :tranverse the linkedList]
 * @param pHead [the pointer to the headNode*(the root pointer)]
 */
void tranverse(PNode pHead){
    if (NULL == pHead){
        printf("%s\n", "the linkedList is empty");
    }
    PNode pCurrent = pHead->pNext;
    while(NULL!=pCurrent){
        printf("%d ", pCurrent->value);
        pCurrent = pCurrent->pNext;
    }    
    printf("%s\n", "tranverse over!");
}

/**
 * [getLength: get length of list]
 * @param  pHead [the root pointer]
 * @return       [the length]
 */
int getLength(PNode pHead){
    int length = 0;
    if (NULL == pHead || NULL== pHead->pNext){
        return 0;
    }else{
        PNode pCurrent = pHead->pNext;
        while(NULL!=pCurrent){
            length++;
            pCurrent = pCurrent->pNext;
        }
        return length;
    }    
};

/**
 * [insertLinkedLise :insert the value to the linkedList]
 * @param  pHead   [the root pointer]
 * @param  positon [the new node wanna to insert positon]
 * @param  value   [the value wanna to insert]
 * @return         [if success return 1,other return 0]
 */
int insertNode(PNode pHead,int position,int value){
    if (position<=0 || position>getLength(pHead)){
        return 0;
    }
    PNode pCurrent = pHead;
    PNode pPrevious = NULL;
    PNode pNew  = NULL;
    for (int i = 0; i < position; ++i){
        pPrevious = pCurrent;
        pCurrent = pCurrent->pNext;
    }
    /*now the pPrevious has point to the previous node of position node
      and the current pointer point to the positon node.
    */
      pNew = (PNode)malloc( sizeof(Node));
      pNew->value =value;
      pNew->pNext = pPrevious->pNext;
      pPrevious->pNext = pNew;

      return 1;
}

/**
 * [deleteNode :delete the node which locaiton has given]
 * @param  pHead    [root pointer]
 * @param  position [the position you wanna to delete]
 * @return          [return the value of which you deleted]
 */
int deleteNode(PNode pHead,int position){
    int value = 0;
    if (position<=0 || position>getLength(pHead)){
        exit(-1);
    }else{
        PNode pCurrent = pHead;
        PNode pPrevious = NULL;
        for (int i = 0; i < position; ++i){
            pPrevious = pCurrent;
            pCurrent = pCurrent->pNext;
        }
        value = pCurrent->value;
        pPrevious->pNext = pCurrent->pNext;
        pCurrent=NULL;
        return value;
    }
}

 

转载于:https://www.cnblogs.com/alienSmoking/p/5450946.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值