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

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

链表代码整理

  1. 链表结构头文件
//structLinkList.h
#include <iostream>

typedef struct LNode *List;
struct LNode{
    double Data;
    List Next;
};

//list length
int Length(List PtrL)
{
    List p=PtrL;
    int j=0;
    while(p){
        p=p->Next;
        j++;
    }
    return j;
}

//find kth element by its sequence number
List FindKth(int K, List PtrL)
{
    List p=PtrL;
    int i=1;
    while(p!=NULL && i<K){
        p=p->Next;
        i++;
    }
    if(i==K) return p;
    else return NULL;
}
//find element by its value
int Find(double X,List PtrL, List rstL)
{
    List p=PtrL;
    int pos=1;
    while(p!=NULL&&p->Data!=X)
    {
        p=p->Next;
        pos++;
    }
    return pos;
}

//insert new element at position i
List Insert(double X, int i, List &PtrL)
{
    List p,s;
    if(i==1){
        s=(List)malloc(sizeof(struct LNode));
        s->Data=X;
        PtrL=s;
        return PtrL;
    }
    p=FindKth(i-1,PtrL);
    if(p==NULL){
        printf("parameter i wrong\n");
        return NULL;
    }
    else{
        s=(List)malloc(sizeof(struct LNode));
        s->Data=X;
        s->Next=p->Next;
        p->Next=s;
        return PtrL;
    }
}

//delete element at position i
List Delete(int i, List &PtrL)
{
    List p,s;
    if(i==1){
        s=PtrL;
        if(PtrL!=NULL) PtrL=PtrL->Next;
        else return NULL;
        free(s);
        return PtrL;
    }
    p=FindKth(i-1,PtrL);
    if(p==NULL){
        printf("the %d element does not exist.\n",i-1);
        return NULL;
    }else if(p->Next==NULL){
        printf("the %d element does not exist.\n",i);
        return NULL;
    }else{
        s=p->Next;
        p->Next=s->Next;
        free(s);
        return PtrL;
    }
}
  1. 主函数,调用链表结构头文件,实现链表一系列具体操作。
//main.cpp
#include "structLinkList.h"
using namespace std;

int main()
{
    List mylist=(List)malloc(sizeof(struct LNode));
    List tmpList;
    int i;
    cout<<"Insert elements into the list."<<endl;
    for(i=1;i<10;i++)
    {
        double vluEle=3.0*i;
        cout<<"Insert "<<vluEle<<" into the list."<<endl;
        Insert(vluEle,i,mylist);
    }
    cout<<endl;

    int lgtList = Length(mylist);
    cout<<"Length of the list is : "<<lgtList<<endl;

    cout<<"After insertation:"<<endl;
    for(i=1;i<=lgtList;i++)
    {
        tmpList=FindKth(i,mylist);
        cout<<"Position "<<i<<" of list is "<<tmpList->Data<<endl;
    }
    cout<<endl;

    cout<<"Find member by its value"<<endl;
    int position=0;
    position=Find(7,mylist,tmpList);
    if(position>lgtList){
        cout<<"There is no element has value "<<7<<endl;
    }else{
        cout<<"Position of value "<<7<<" in the list is: "<<position<<endl;
    }
    position=Find(6,mylist,tmpList);
    if(position>lgtList){
        cout<<"There is no element has value "<<6<<endl;
    }else{
        cout<<"Position of value "<<6<<" in the list is: "<<position<<endl;
    }

    cout<<"Find member by its sequence number"<<endl;
    tmpList=FindKth(5,mylist);
    if(tmpList==NULL){
        cout<<"Element at position "<<5<<" does not exist."<<endl;
    }else{
        cout<<"Value of position "<<5<<" is: "<<tmpList->Data<<endl;
    }
    tmpList=FindKth(9,mylist);
    if(tmpList==NULL){
        cout<<"Element at position "<<9<<" does not exist."<<endl;
    }else{
        cout<<"Value at position "<<9<<" is : "<<tmpList->Data<<endl;
    }

    cout<<"Delete element at position "<<4<<endl;
    Delete(4,mylist);
    lgtList=Length(mylist);
    cout<<"Length of the list after deletion is : "<<lgtList<<endl;
    cout<<endl;

    cout<<"After deletion:"<<endl;
    for(i=1;i<=lgtList;i++)
    {
        tmpList=FindKth(i,mylist);
        cout<<"Position "<<i<<" of list is "<<tmpList->Data<<endl;
    }
    return 0;
}
  1. 结果:
    在这里插入图片描述
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值