单链表

来源:互联网 发布:怎么找淘宝模特 编辑:程序博客网 时间:2024/06/03 18:43
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std; #define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2typedef int ElemType;typedef int Status;typedef struct Node{    ElemType data;    struct Node *next;}Node,*LinkedList;LinkedList LinkedList_creatT(){    Node *L;    L=(Node *)malloc(sizeof(Node));    L->next=NULL;    Node*r;    r=L;    ElemType x;    while(scanf("%d",&x)!=EOF)    {        Node *p;        p=(Node*)malloc(sizeof(Node));        p->data =x;        r->next=p;        r=p;    }    r->next=NULL;    return L;}Status GetElem(LinkedList L,int i,ElemType &e) {    Node *p;int j;    p=L->next;j=1;    while(p&&j<i)    {        p=p->next ;++j;    }    if(!p||j>i)    {        return ERROR;    }    e=p->data;    return OK;}Status LinkedList_insert(LinkedList &L,int i,ElemType x){    Node *pre;    pre=L;    int tempi=0;    while(pre&&tempi<i-1)    {        pre=pre->next;        ++tempi;    }    if(!pre||tempi>i-1)return ERROR;    Node *p;    p=(Node *)malloc(sizeof(Node));    p->data =x;    p->next=pre->next;    pre->next=p;    return OK;}LinkedList LinkedList_delet(LinkedList L,ElemType x){    Node *p,*pre;    p=L->next;    while(p->data !=x)    {        pre=p;        p=p->next;    }    pre->next=p->next;    free(p);    return L;}int main(){    LinkedList list,start;    printf("please input number of LinkedList:");    list=LinkedList_creatT();    for(start=list->next;start!=NULL;start=start->next)    printf("%d ",start->data);    printf("\n");    int i;    ElemType x;    printf("input the locate where you want to insert:");    scanf("%d",&i);    printf("input what you want to insert:");    scanf("%d",&x);    LinkedList_insert(list,i,x);    for(start=list->next;start!=NULL;start=start->next)    printf("%d ",start->data);    printf("\n");    printf("input what you want to delete:");    scanf("%d",&x);    LinkedList_delet(list,x);    for(start=list->next;start!=NULL;start=start->next)    printf("%d ",start->data);    printf("\n");    printf("input the number you want search:");    int ss;    scanf("%d",&ss);    GetElem(list,ss,x);    printf("the %dthe is %d\n",ss,x);    return 0;}