链表

链表

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
List MakeEmpty(List L);
int IsEmpty(List L);
int IsLast(Position P, List L);
Position Find(int x, List L);
void Delete(int x, List L);
Position Findprevious(int x, List L);
void Insert(int x,Position P);
void DeleteList(List L);
void Output(List L);
struct Node {
    int element;
    Position next;
};

List MakeEmpty(List L)
{
    return List();
}

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

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

Position Find(int x, List L)
{
    return Position();
}

void Delete(int x, List L)
{
    Position P,temp;
    P = Findprevious(x, L);
    if (!IsLast(P, L))
    {
        temp = P->next;
        P->next = P->next->next;
        free(temp);
    }
}

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

void Insert(int x,Position P)
{
    Position temp;                          //定义一个插入变量
    temp = new(struct Node);                //为该插入变量申请内存
    if (temp == NULL) cout << "Error!";     //判断是否申请成功
    else                                    //成功以后对该变量赋值以及建立连接
    {
        temp->element = x;
        temp->next = P->next;
        P->next = temp;
    }
}

void DeleteList(List L)
{
    Position P,temp;
    P = L->next;
    L->next = NULL;
    while (P)
    {
        temp = P->next;
        free(P);
        P = temp;
    } 
}

void Output(List L)
{
    while (L)
    {
        cout << L->element << endl;
        L = L->next;
    }
}

int main()
{
    List l = new(struct Node);
    l->next = NULL;
    for (int i = 1; i <= 9; i++)
    {
        Insert(i, l);
    }
    l = l->next;
    Output(l);

    cout << "After delete 5:"<<endl;
    Delete(5, l);
    Output(l);

    DeleteList(l);
    cout << "After Delete List:";
    Output(l);
    system("pause");
}

结果输出:

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值