顺序表的简单实现

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

#define MAXSIZE 100
typedef struct List
{
    int data[MAXSIZE];
    int length;
} List;

//初始化
void InitList(List *L)
{
    L->length = 0;
    return ;
}
//清空
void ClearList(List *L)
{
    L->length = 0;
    return ;
}

//插入
//插入时下标是从0~length-1,而不是1~length
void InsertList(List *L,int p,int e)
{
    if(L->length-1 == MAXSIZE)
    {
        printf("The List is full.\n");
        return ;
    }
    if(p<0 || p>L->length)
    {
        printf("Please input a new position.\n");
        return ;
    }

    //如果插入位置p刚好在原List最后一个元素的后面
    //则不需要进行for循环
    if(p<=L->length-1)
        for(int i = L->length-1; i>=p; i--)
        {
            L->data[i+1] = L->data[i];
        }

    L->data[p] = e;
    L->length++;

    return;
}

//删除
void DeleteList(List *L,int p,int *e)
{
    if(p<0 || p>L->length-1)
    {
        printf("Please input a new position.\n");
        return ;
    }

    *e = L->data[p];
    for(int i=p; i<L->length-1; i++)
        L->data[i] = L->data[i+1];

    L->length--;

}
//打印
void PrintList(List L)
{
    if(L.length == 0)
    {
        printf("The List is empty.\n");
        return ;
    }
    for(int i=0; i<L.length; i++)
        printf("%d ",L.data[i]);

    printf("\n\n");
}
int main()
{
    List L;
    InitList(&L);
    printf("L.length == %d\n",L.length);

    InsertList(&L,0,12);
    InsertList(&L,0,23);
    InsertList(&L,1,78);
    InsertList(&L,3,99);
    printf("After Insert.\nPrint the List: ");
    printf("L.length == %d\n",L.length);
    PrintList(L);

    int e = 0;
    DeleteList(&L,0,&e);
    printf("e = %d\n",e);
    DeleteList(&L,2,&e);
    printf("e = %d\n",e);
    printf("After Delete.\nPrint the List: ");
    printf("L.length == %d\n",L.length);
    PrintList(L);

    ClearList(&L);
    printf("After Clear.\nPrint the List: ");
    printf("L.length == %d\n",L.length);
    PrintList(L);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据结构基于C语言实现顺序表. #include <stdio.h> #include <stdlib.h> #define MAXSIZE 100/* 定义二叉树节点类型 */ typedef struct node { char data; struct node *lchild, *rchild; }BTNode; BTNode* CreatBitTree()/* 递归前序建立二叉树 */ { char c; BTNode *T; scanf("%c", &c); if (c == ' ') /* 遇到空节点停止递归 */ { T = NULL; } else { T = (BTNode*) malloc(sizeof(BTNode)); T->data = c;/* 建立根节点 */ T->lchild = CreatBitTree();/* 递归先序建立左子树 */ T->rchild = CreatBitTree();/* 递归先序建立右子树 */ } return T; } void PreOrder(BTNode* T)/* 非递归前序遍历二叉树 */ { BTNode *stack[MAXSIZE], *p; int top = 0; if (T != NULL) { //top++;/* 根节点入栈 */ stack[top] = T; while (top > -1)/* 栈不空时循环 */ { p = stack[top];/* 出栈并访问该节点 */ top--; printf("%c ", p->data); if (p->rchild != NULL)/* 右孩子入栈 */ { top++; stack[top] = p->rchild; } if (p->lchild != NULL)/* 左孩子入栈 */ { top++; stack[top] = p->lchild; } } printf("\n"); } } void PrintTree(BTNode* T,int nLayer) //按竖向树状打印的二叉树 { int i; if(T==NULL) return ; PrintTree(T->rchild,nLayer+1); for(i=0;i<nLayer;i++) printf(" "); printf("%c\n",T->data); PrintTree(T->lchild,nLayer+1); } int main()/* 主函数 */ { BTNode *r = NULL; printf("请先序输入二叉树:(如:AB 三个空格表示A为根结点,B为左子树的二叉树)\n"); r = CreatBitTree(); printf("按竖向树状打印的二叉树:\n"); PrintTree( r,0); printf("先序非递归遍历二叉树:"); PreOrder(r); return 0; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值