链表的实现

#include<iostream>

#include<stdlib.h>

using namespace std;

typedef int ElemType;

typedef struct Node

{

    ElemType data;

    struct Node* next;

}*LinkList,Node;



LinkList LinkListCreate()

{

    int data;

    Node *L=(Node*)malloc(sizeof(Node));

    if(L==NULL) cout<<"Error"<<endl;

    L->next=NULL;

    Node *r;

    r=L;

    while(cin>>data)

    {

        if(data<0) break;

        Node *p=(Node*)malloc(sizeof(Node));

        if(p==NULL) cout<<"Error"<<endl;

        p->next=NULL;

        p->data=data;

        r->next=p;

        r=r->next;

    }

    return L;

}



LinkList LinkListDelete(LinkList L,int data)

{

    Node *pre,*p;

    pre=L;

    p=L->next;

    while(p && p->data!=data)

    {

        pre=p;

        p=p->next;

    }

    if(pre->next) pre->next=p->next;

    free(p);

    return L;

}



LinkList LinkListPrint(LinkList L)

{

   Node *p;

   p=L->next;

   while(p)

   {

       cout<<p->data<<endl;

       p=p->next;

   }

   return L;

}



int main()

{

    int x;

    LinkList L=LinkListCreate();

    cin>>x;

    LinkListDelete(L,x);

    LinkListPrint(L);

    return 0;

}

  

你可能感兴趣的:(链表)