链表的插入删除

#include <stdlib.h>
#inlcude <malloc.h>
#include <stdio.h>

typedef struct node
{
    int data;
    strutc node * pNext;
}Node,*pNode;

//函数说明
pNOde CreatList();
int AddList();
int DelList();
int TraveList();
int LoopList();


/*这个创建链表的数据都是通过手动输入进取得到的*/
pNode CreatList()
{
    int i;
    int length;
    int val;
    scanf("%d",&length);
    printf("length of the list is %d",length);
    pNode phead = (pNode)malloc(sizeof(Node));
    pNode ptail = phead;
    ptail->pNext = NULL;
    for (i=0;i<length;i++ )
    {
        scanf("%d",&val);
        pNode pNew = (pNode)malloc(sizeof(Node));
        pNew->data = val;
        ptail->pNext = pNew;
        pNew->pNext = NULL;
        ptail = pNew;
    }
    return phead;
}

int AddList(pNode pHead, int front,int data)
{
    pNode _node = phead;
    //判断用户输入的数据是否大于等于1,及_node是否为空
     if ((front < 1) && (NULL != _node))        
    {
        return false;
    }
    
    for(int i=0; i < front-1;++i)
        _node = _node->pNext;
    
    
    pNode pswap;
    pswap = _node->pNext;
    pNode pNew =malloc(sizeof(Node));
    pNew->pNext = _pswap;
    _node->pNext = pNew;
    pNew->member = data ;    
}

int DelList(pNode pHead, int front)
{
    pNode _node = phead;
    //判断用户输入的数据是否大于等于1,及_node是否为空
    if ((front < 1) && (NULL != _node))        
    {
    return false;
    }

    for(int i=0; i < front-1;++i)
    _node = _node->pNext;
    
    pNode pswap;
    int m = _node->pNext->member;
    pswap = _node->pNext;
    _node->pNext = pswap->pNext;
    free(pswap);
    return m;
}

int TraveList()
{
    pNode p = pHead->pNext;                            //将头节点的指针给予临时节点p
    while(NULL != p)                                //节点p不为空,循环    
    {
    printf("%d ",p->member);                    
    p = p->pNext;                                
    }
    printf("\n");
    return ;
}

 

posted @ 2017-05-15 21:07  sheldon_lee  阅读(179)  评论(0编辑  收藏  举报