动态链表实现(待完善)

来源:互联网 发布:mac和vb虚拟机如何共享 编辑:程序博客网 时间:2024/06/05 13:07
 #include<iostream>
using namespace std;

struct node///定义节点结构
{
    int data;
    struct node *next;

}node;
typedef struct node *linklist;

void init_list(linklist L)///初始化链表
{
    L.next->next=NULL;
}
int Size(linklist L)///返回链表的长度
{
    linklist q;
    q=L.next;
    int i=0;

    while(q->next!=NULL)
    {
        i++;
        q=q->next;
    }
    return (i);
}
int Back(linklist L)///返回链表最后一个元素值
{
    linklist q;
    q->L.next;
    if(q->next==NULL)
        return (0);
    while(q->next!=NULL)
        q=q->next;

    return q->data;
}
(仔细看)
int Clear(listlist L)///清空链表
{
    linklist q=L->next;
    if(q->next==NULL)
        return (0);

    while(q->next!=NULL)
    {
        q=q->next;
        delete q;
    }
    L->next->next=NULL;
}

bool Empty(linklist L)///判断链表是否为空
{
    if(L->next!=NULL)
        return (1);
    else
        return (0);
}

int Erase(listlist L,int n)///删除链表中的第n个元素
{
    if(n>Size(L))
        return (0);
linklist q,k;
    q=L->next;
    for(int i=1; i<n; i++)
    {
        k=q;
        q=q->next;
    }

    q=q->next;
    delete q;
    k=q;
}

int Front(listlist L)///返回链表的第一个元素值
{
    if(Empty(L))
        return (-1);
    else
        return L->next->next->data;
}




int Insert(linklist L,int n,int e)///在链表的第n个元素后插入元素e
{
    if(n>Size(L))
        return (0);
        linklist q;

    for(int i=1; i<n; i++)
     q=q->next;
     linklist k;
    k=new node;
    k->data=e;
    k->next=q->next;
    q->next=k;
}

int main()
{
    return (0);
}

0 0
原创粉丝点击