静态链表的实现

#include
#include      
#define SIZE 6      
//define struct of static list      
typedef struct stlist      
{      
    int* content;//保存当前节点的内容      
    int* next;    //保存下一节点的下标      
    int* exist;   //标记当前节点是否已经使用过      
} StList;      
//初始化静态链表分配空间大小为SIZE个      
StList* initList(StList* tempPtr)      
{      
    int i;      
    //allocation space      
    tempPtr=(StList*)malloc(sizeof(StList));      
    tempPtr->content=(int*)malloc(sizeof(int)*SIZE);      
    tempPtr->next=(int*)malloc(sizeof(int)*SIZE);      
    tempPtr->exist=(int*)malloc(sizeof(int)*SIZE);      
    for(i=0;i<6;i++)      
    {      
        tempPtr->exist[i]=0;      
        tempPtr->next[i]=0;      
        tempPtr->content[i]=0;      
    }//Of for i      
    //initialize head and tail      
    tempPtr->content[0]=-65535;      
    tempPtr->exist[0]=1;      
    tempPtr->content[1]=65535;      
    tempPtr->exist[1]=1;      
    tempPtr->next[0]=1;      
    tempPtr->next[1]=-1;      
    return tempPtr;      
}//Of initList      
      
//打印静态链表      
void print(StList* tempPtr)      
{      
    int temp=0;      
    int i;      
       //按照数组下标遍历静态链表      
    for(i=0;icontent[i],tempPtr->next[i],tempPtr->exist[i]);      
    }//Of for i      
//按照节点顺序遍历静态链表      
    printf("遍历:\n");      
    while(1)      
    {      
        printf("%d\t%d\t%d\t\n",tempPtr->content[temp],tempPtr->next[temp],tempPtr->exist[temp]);      
        if(tempPtr->next[temp]==-1)      
        {      
            break;      
        }//Of if      
        temp=tempPtr->next[temp];      
    }//Of while      
}//Of print      
      
//把元素插入到静态链表当中      
int insert(StList* tempPtr,int newElement)      
{      
    int i;      
    int temp=0;      
    int insertIndex=-1;      
    int p,q;      
    p=0;      
    q=tempPtr->next[p];      
//如果静态链表中存在元素 则不插入      
    for(i=0;icontent[i]==newElement)      
        {      
            return -1;      
        }//Of if      
    }//Of for i      
    for(i=0;i//把元素插入到静态链表中      
    {      
        //find spare space      
        if(tempPtr->exist[i]==0)      
        {      
            tempPtr->content[i]=newElement;      
            tempPtr->exist[i]=1;      
            insertIndex=i;      
            break;      
        }//Of if      
    }//Of for i      
    if(insertIndex==-1)      
    {      
        printf("no more space.Overflow.\n");      
        return -1;      
    }//Of if      
    for(i=0;icontent[p]content[q]>newElement)      
        {      
            tempPtr->next[p]=insertIndex;      
            tempPtr->next[insertIndex]=q;      
            break;      
        }//of if      
        else      
        {      
            p=q;      
            q=tempPtr->next[q];      
        }//Of else      
    }      
    return 1;      
}      
      
int main()      
{      
    StList* list;      
    list=initList(list);      
//  insert(list,3);      
//  insert(list,7);      
//  insert(list,5);      
//  insert(list,9);      
    insert(list,8);      
    insert(list,8);      
    printf("content\tnext\texist\n");      
    print(list);      
    return 0;      
}//Of main   

静态链表的实现_第1张图片




你可能感兴趣的:(数据结构和算法)