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

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

链表代码整理

  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. 结果:
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值