静态链表

来源:互联网 发布:淘宝的返利网 编辑:程序博客网 时间:2024/05/18 23:14
#include<iostream>#include<cstdio>#include<stdlib.h>#include<malloc.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2#define MAXSIZE 10 using namespace std;typedef int ElemType;typedef int Status;typedef struct{    ElemType data;    int cur;}component,SList[MAXSIZE];//生成备用链表 void initSpace(SList& space){    for(int i=0;i<MAXSIZE-1;i++){        space[i].cur=i+1;    }    space[MAXSIZE-1].cur=0;}int mallocLocation(SList& space){    int position=space[0].cur;    if(position){        space[0].cur=space[position].cur;    }    return position; }void freeSL(SList& space,int k){    space[k].cur=space[0].cur;    space[0].cur=k;}void difference(SList& space,int &head){    initSpace(space);    head=mallocLocation(space);//生成头节点     int n,m,r=head;    scanf("%d%d",&n,&m);     for(int i=0;i<n;i++){        int t=mallocLocation(space);        scanf("%d",&space[t].data);        space[r].cur=t;        r=t;    }    space[r].cur=0;    for(int i=0;i<m;i++){        int temp,pre=head;        scanf("%d",&temp);        r=space[head].cur;        while(r){            if(space[r].data==temp)break;            pre=r;            r=space[r].cur;         }        if(!r){            int t=mallocLocation(space);            space[t].data=temp;            space[t].cur=0;            space[pre].cur=t;        }else{            space[pre].cur=space[r].cur;                freeSL(space,r);        }    }}void Reverse(SList& slist,int head){    int p=slist[head].cur;    while(p){        printf("%d ",slist[p].data);        p=slist[p].cur;    }}int main(){    SList space;    int head;    difference(space,head);    Reverse(space,head);    return 0;}
原创粉丝点击