数据结构之线性表——浙大数据结构公开课代码整理

浙江大学数据结构陈越老师视频课程

线性表代码整理

  1. 线性结构头文件
//structList.h
#include <iostream>

//define maxsize of list
#define MAXSIZE 100

//define structure of list
typedef struct LNode *List;
struct LNode{
    double Data[MAXSIZE];
    int Last;
};

//initialize of list
List MakeEmpty()
{
    List PtrL;
    PtrL = (List)malloc(sizeof(struct LNode));
    PtrL->Last=-1;
    return PtrL;
}

//find X from list
int Find(double X, List PtrL)
{
    int i=0;
    while(i <= PtrL->Last && PtrL->Data[i] != X)
         i++;
    if(i > PtrL->Last) 
    {
        printf("There is no such element in the list.\n");
        return -1;
    }
    else
    {
        printf("The position of %.2f is %d.\n", X, i);
        return i;
    }
}

//insert x in list
void Insert(double X, int i, List PtrL)
{
    int j;
    if(PtrL->Last == MAXSIZE-1)
    {
        printf("List full!");
        return;
    }
    if(i < 1 || i>PtrL->Last + 2)
    {
        printf("Location illegal.");
        return;
    }
    for(j = PtrL->Last; j >= i-1; j--)
        PtrL->Data[j+1] = PtrL->Data[j];
    PtrL->Data[i-1] = X;
    PtrL->Last++;
    return;
}

//delete element at position i
void Delete(int i, List PtrL)
{
    int j;
    if(i < 1 || i > PtrL->Last+1)
    {
        printf("No %d element", i);
        return;
    }
    for(j = i; j <= PtrL->Last; j++)
        PtrL->Data[j-1]=PtrL->Data[j];
    PtrL->Last--;
    return;
}


  1. 主函数,调用线性结构头文件,实现线性表一系列具体操作。
//main.cpp
#include "structList.h"
using namespace std;

int main()
{
    List mylist = MakeEmpty();
    cout<<"Insert elements into the list。"<<endl;
    for(int i=1;i<10;i++)
    {
        Insert(3.0*i-1.4,i,mylist);
    }

    cout<<endl;
    cout<<"After insertation:"<<endl;
    for(int i=0;i<mylist->Last;i++)
    {
        cout<<"Position "<<i<<" of list is "<<mylist->Data[i]<<endl;
    }
    cout<<"Find the position of "<<4.6<<endl;
    Find(4.6, mylist);
    cout<<endl;
    cout<<"Find the position of "<<3.5<<endl;
    Find(3.5, mylist);
    cout<<endl;

    cout<<"Delete element at position "<<4<<endl;
    Delete(4,mylist);
    cout<<endl;
    cout<<"After deletion:"<<endl;
    for(int i=0;i<mylist->Last;i++)
    {
        cout<<"Position "<<i<<" of list is "<<mylist->Data[i]<<endl;
    }
    return 0;
}
  1. 结果:
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
内容如下: ├─程序例子 │ │ Gauss.cpp │ │ MOVE.C │ │ RUIYUE_3.C │ │ RUIYUE_4.C │ │ │ ├─1概述 │ │ bb_sort.cpp │ │ ex1-9.cpp │ │ ex1-91.cpp │ │ │ ├─2线性表 │ │ ex2-11.cpp │ │ ex2-11new.cpp │ │ linklist.cpp │ │ linklist.h │ │ LINKQUEU.C │ │ linkqueue.cpp │ │ linkqueue.h │ │ list.c │ │ LIST2.C │ │ LIST_S15.C │ │ LIST_SL.C │ │ LIST_SQ.C │ │ LIST_SQ5.C │ │ new2-11.cpp │ │ old2-11.cpp │ │ SQLIST.C │ │ Sqlist.cpp │ │ UNION.CPP │ │ │ ├─3堆栈与队列 │ │ bank_simulation.cpp │ │ conversion.cpp │ │ DMXSTACK.C │ │ hanoi.cpp │ │ MAZE.CPP │ │ QUEUE09.C │ │ SQQUEUE.C │ │ Sqqueue.cpp │ │ sqstack.cpp │ │ stack.c │ │ STK_9.C │ │ 表达式求值.cpp │ │ │ ├─5数组 │ │ array.c │ │ array_ex.cpp │ │ array_test.cpp │ │ DK1.c │ │ DK2.c │ │ SANYANZU.C │ │ │ └─6树 │ bitree.cpp │ BITREE11.C │ bitree2.cpp │ BITREE23.C │ BITREE9.C │ Create_bitree.c │ inorder_thr_tree.cpp │ intreading.cpp │ TING6.C │ TRAVERSE.C │ └─课件 sjjg答疑.txt 数据结构1概述.ppt 数据结构2线性表1.ppt 数据结构3线性表2.ppt 数据结构4堆栈与队列1.ppt 数据结构5堆栈与队列2.ppt 数据结构6串.ppt 数据结构7数组1.ppt 数据结构8数组2.ppt 数据结构9树1.ppt 数据结构A树2.ppt 数据结构B树3.ppt 数据结构C图1.ppt 数据结构D图2.ppt 数据结构E查找.ppt 数据结构F排序.ppt 数据结构文稿14.ppt

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值