线性表链储存主要部分

#include "stdafx.h"

#include "stdio.h"    
#include "string.h"
#include "ctype.h"      
#include "stdlib.h"   
#include "io.h"  
#include "math.h"  
#include "time.h"

typedef struct Node
{
    int num;
    Node *next;
} ;

typedef struct Node *PNode;

int Inilist(PNode *l)
{
    *l = (PNode)malloc(sizeof(Node));
    (*l)->next=NULL;
    return 1;
}

int Insert(PNode *l,int p,int x)
{
    PNode pn=*l;
    int j=1;
    while(pn && j<p)
    {
        pn=pn->next;
        j++;
    }

    if(!pn)
    {
        return 0;
    }
    PNode pnn=(PNode)malloc(sizeof(Node));
    pnn->next=pn->next;
    pnn->num=x;

    pn->next=pnn;
    

    return 1;
}

int Delete(PNode *l,int p)
{
    PNode pn=*l;
    int j=1;
    while(pn && j<p)
    {
        pn=pn->next;
        j++;
    }

    if(!pn||!(pn->next))
    {
        return 0;
    }
    PNode pnn = pn->next;
    pn->next=pnn->next;
    free(pnn);


    return 1;
}

int main()
{  
    PNode link;
    Inilist(&link);
    Insert(&link,1,1);
    Insert(&link,2,2);
    Insert(&link,3,3);
    Insert(&link,2,4);
    Delete(&link,8);

    return 0;
}

 

posted @ 2013-04-07 21:06  wahgon  阅读(197)  评论(0编辑  收藏  举报