【数据结构】链表实现


#include <stdio.h>
#include <stdlib.h>
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;

struct Node
{
    int X;
    Position next;
};

//======================function=========================
//=======================================================
//=======================================================

Position Last(List L) //return the position of the last node
{
    Position P, previousOne;
    P = L;
    while (P->next!=NULL) {
        previousOne=P;
        P=P->next;
    }
    return P;
}

Position Find(List L, int x)
{
    Position P; P=L;
    while (P!=NULL&&P->X!=x) {
        P=P->next;
    }
    return P;
}

Position FindPrevious(List L, int x)
{
    Position P; P=L;
    while (P->next!=NULL&&P->next->X!=x) {
        P=P->next;
    }
    return P;
}

void append(int x,List L)
{
    Position LastOne=Last(L);
    Position NewNode;
    NewNode=malloc(sizeof(struct Node));
    LastOne->next=NewNode;
    NewNode->X=x;
    NewNode->next=NULL;
}

void printList(List L)
{
    Position P; P=L;
    while (P!=NULL) {
        printf("%d ",P->X);
        P=P->next;
    }
}

void insert(List L, int x, Position P) //插入到P的后面
{
    Position NewNode=malloc(sizeof(struct Node));
    NewNode->next=P->next;
    P->next=NewNode;
    NewNode->X=x;
}

void insertToNum(List L,int x, int num)//插到第numf个数后面
{
    Position P; P=L;
    for (int n=1; n<num; n++) {
        P=P->next;
    }
    insert(L, x, P);
}

void delete(List L,int x) //删除列表中第一个出现的x
{
    Position P=Find(L, x), P_previous=FindPrevious(L, x);
    P_previous->next=P->next;
    free(P);
}

void empty(List L)
{
    Position P;
    Position tmP;
    P=L->next; L->next=NULL; L->X=0;
    while (P!=NULL) {
        tmP=P->next;
        free(P);
        P=tmP;
    }
}

int isEmpty(List L)
{
    return L->next==NULL;
}

int isLast(List L, Position P)
{
    return P->next==NULL;
}

void Allfunc()
{
    printf("==========All functions=========\nLast/Find/Findprevious/append/\nprintList/insert/insertToNum/\ndelete/empty/isLast/isEmpty/\n================================\n");
}

//========================main function========================

int main(int argc, const char * argv[])
{
    List L; struct Node N1; L=&N1; //创建完成含有一个节点的列表
    //====================================
    //
    //     manipulate the List here
    //
    //====================================
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值