带表头结点单链表的初始化、查找、插入、删除、输出、撤销

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>

#define ERROR 0
#define OK 1
#define Overflow 2
#define Underflow 3
#define NotPresent 4
#define Duplicate 5

typedef int ElemType;
typedef int Status;
//带表头结点单链表的定义
typedef struct node
{
    ElemType element;
    struct node *link;
}node;

typedef struct
{
    struct node *head;
    int n;
}headerList;

//带表头结点单链表的初始化
Status Init(headerList *h)
{
    h->head = (node*)malloc(sizeof(node));
    if(!h->head)
        return ERROR;
    h->head->link = NULL;
    h->n = 0;
    return OK;
}

//带表头结点单链表的插入
Status Insert(headerList *L,int i,ElemType x) //在i后面插入x
{
    if(i<-1||i>L->n-1)
        return ERROR;
    node *p = L->head;
    int j;
    for(j=0;j<=i;j++)
        p = p->link;
    node *q = (node*)malloc(sizeof(node));
    q->element = x;
    q->link = p->link;
    p->link = q;
    L->n++;
    return OK;
}

//带表头结点单链表的删除
Status Delete(headerList *h,int i)
{
    int j;
    node *p,*q;
    if(!h->n)
        return ERROR;
    if(i<0||i>h->n-1)
        return ERROR;
    q = h->head;
    for(j=0;j<i;j++)
        q = q->link;
    p = q->link;
    q->link = p->link;
    free(p);
    h->n--;
    return OK;
}
//带表头结点单链表的输出
Status Output(headerList L)
{
    int i;
    node *p;
    if (!L.head) //判断带表头结点单链表是否为空
        return ERROR;
    p=L.head;
    for(i=0;i<L.n;i++)
    {
    p=p->link;
    printf("%d ",p->element);
    }
    return OK;
}

//带表头结点单链表的撤销
void Destroy(headerList *L)
{   node *p;
    while(L->head)
    { p= L->head->link;
      free(L->head);
      L->head=p;
    }
}

//带表头结点单链表的查找
Status Find(headerList h,int i,ElemType *x)//单链表的查找
{
    node *p;
    int j;
    if(i<0||i>h.n-1)
    return ERROR;
    p=h.head->link;
    for(j=0;j<i;j++) p=p->link;
    *x=p->element;
    return OK;
}
//带表头结点单链表的逆置
Status Invert(headerList *L)
{
    node *p = L->head->link;
	node *q = L->head->link->link;
	node *t = NULL;
	while(q != NULL)
	{
		t = q->link;
		q->link = p;
		p = q;
		q = t;
	}
	L->head->link->link = NULL;   //设置链表尾
	L->head->link = p;			  //修改链表头
	return 0;
}

带表头结点单链表的排序
void sort(headerList *L)
{
    node *p=L->head->link, *q, *r;
    if(p!=NULL)
    {
        /* 把链表分成两条,一条已经排序好了(L),一条待排序(p)*/
        r=p->link;
        p->link=NULL;
        p=r;
        /* 对于所有待排序的元素 */
        while(p!=NULL)
        {
            /*把p链表的第一个元素插入到L,并且将它从p中移除*/
            r=p->link;  //r指向p的第二个元素
            /* 找到合适的插入点 */
            q=L->head;
            while(q->link!=NULL && q->link->element < p->element)
                q=q->link;
            /* 在q后面插入p */
            p->link=q->link;
            q->link=p;
            /* 现在p的第一个元素已经被移到L中合适的位置了 */
            p=r;
        }
    }
}

int main()
{
    printf("******************\n");
    printf("Name: Zeng Jiawei \n");
    printf("ID:B17041130 \n");
    printf("Project:Use of HeaderList\n");
    printf("Date: 2019-3-17\n");
    printf("******************\n");

    int i;
    int x;
    headerList L;
    Init(&L);
    printf("Inited Successfully");
    printf("\n\nInserting...");
    for(i=0;i<9;i++)
        Insert(&L,i-1,i);               //在i-1之后插入i
    printf("\n\nThe headerList after Inserting:");
    Output(L);
    printf("\n\nDeleting the zeroth element...");
    Delete(&L,0);                         //删除第0个元素
    printf("\n\nThe headerList after Deleting the zeroth element:");
    Output(L);
    printf("\n\nInverting...");
    Invert(&L);
    printf("\n\nThe headerList after Inverting:");
    Output(L);
    printf("\n\nLets find the third element (start from the zeroth)!");
    Find(L,3,&x);
    printf("\n\nThe third element is:");
    printf("%d ",x);
    printf("\n\nSorting...");
    sort(&L);
    printf("\n\nThe headerList after Sorting:");
    Output(L);
    printf("\n\n");
    Destroy(&L);
    return 0;
}



已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 终极编程指南 设计师:CSDN官方博客 返回首页