数据结构笔记

顺序表

单链表

双向链表

顺序表

#include <iostream>
#include <cmath>
#include <cstdlib>
#define MAX 100
using namespace std;
typedef struct
{
    int *elem;
    int length;
}sqlist;

bool initiallist(sqlist &l)    //初始化
{
    l.elem=new int[MAX];
    if(!l.elem)
        return false;
    l.length=0;  //初始化为空
    return true;
}
bool searchlist(sqlist l,int e)    //查找表中的数据元素
{
    for(int i=0;i<l.length-1;i++)
    {
        if(l.elem[i]==e)
            return true;
    }
    return false;
}
bool getlist(sqlist l,int i,int &e)  //在表中取第i个数据元素的值
{
    if(i<1||i>l.length)
        return false;
    e=l.elem[i-1];
    return true;
}
bool insertlist(sqlist &l,int i,int e)   //在表中把e插入第i个位置
{
	
    if(i<1||i>l.length)
        return false;
    if(l.length==MAX)     //注意表已经满的情况,不能后移
        return false;
    for(int j=l.length-1;j>=i-1;j--)
    {
        l.elem[j+1]=l.elem[j];
    }  //第i-1个位置也要移动
    l.elem[i-1]=e;  //移动过后原来的i-1位置是空的,要赋值
    l.length++;
    return true;
}
bool deletelist(sqlist &l,int i)  //删除表中的第i个数据元素
{
    if(i<1||i>l.length)
        return false;
    for(int j=i;j<=l.length-1;j++)
    {
        l.elem[j-1]=l.elem[j];
    }
    l.length--;
    return true;
}
int main()
{
    sqlist *l;    //在主函数中定义表
    searchlist(l,0);
    insertlist(l,3,1);
    deletelist(l,3);
    return 0;
}  

单链表

#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef struct lnode
{
    int data;
    struct lnode *next;
}lnode,*linklist;
bool creatlist(linklist &l)
{
    l=new lnode;
    if(!l)
        return false;
    l->next=NULL;
    return true;
}
int getvalue(linklist l,int i)
{
    linklist p;
    int j=0;
    int value;
    p=l;
    while(p&&j<i)
    {
        p=p->next;
        j++;
    }
    if(!p||j>i)
        return 0;
    value=p->data;
    return value;
}
int searchlist(linklist l,int e)
{
    linklist p;
    p=l;
    while(p&&p->data!=e)
    {
        p=p->next;
    }
    if(!p)
        return 0;
    return 1;
}
int insertlist(linklist &l,int i,int e)
{
    linklist p,q;
    int j=0;
    p=l;
    while(p&&j<i-1)
    {
        p=p->next;
        j++;
    }
    if(!p||j>i-1)
        return 0;
    q=new lnode;
    q->next=p->next;
    p->next=q;
    return 1;
}
int deletelist(linklist &l,int i)
{
    linklist p,q;
    int j=0;
    p=l;
    while(p&&j<i-1)
    {
        p=p->next;
        j++;
    }
    if(!p||j>n-1)
        return 0;
    q=p->next;
    p->next=q->next;
    delete q;
    return 1;
}
int inserlisthead(linklist &l)
{
    linklist p;
    int n,num;
    l=new lnode;  //创建头节点
    l->next=NULL;
    cout<<"进行头插法"<<endl;
    cout<<"请输入要插入的数据数:"<<endl;
    cin>>n;
    while (n--)
    {
        cout<<"请输入要插入的数据(逆序):"<<endl;
        cin>>num;
        p=new lnode;  //生成新的节点
        p->data=num;
        p->next=l->next;  
        l->next=p;
    }
    return 1;
}
int insertlisttail(linklist &l)
{
    linklist p,r;
    int n,num;
    l=new lnode;
    l->next=NULL;
    r=l;
    cout<<"进行尾插法"<<endl;
    cout<<"请输入要插入的数据数: "<<endl;
    cin>>n;
    while (n--)
    {
        p=new lnode;   //生成新的节点
        cout<<"请输入要插入的数:"<<endl;
        cin>>num;
        p->data=num;      
        p->next=NULL;
        r->next=p;
        r=p;
    }
    return 1;
}

双向链表

#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef struct doublelnode
{
    int data;
    struct doublelnode *prior,*next; //有两个指针,一个指向前驱,一个指向后继
}doublelnode,*doublelinklist;
bool initialdoublelist(doublelinklist &l)
{
    l=new doublelnode;
    if(!l)
        return false;
    l->next=l->prior=NULL;
    return true;
}
//查找和取值操作和单链表一致,这里不再重复操作
int insertdoublelist(doublelinklist &l,int i,int e)
{
    doublelinklist p,q;
    int j=0;
    p=l;   //把头指针赋给p,不要忘记这一步操作
    while (p&&j<i)
    {
        p=p->next;
        j++;
    }
    if(!p||j>i)
        return 0;
    q=new doublelnode;
    q->data=e;
    q->prior=p->prior;
    p->prior->next=q;
    q->next=p;
    p->prior=q;
    return 1;
}
int deletedoublelist(doublelinklist &l,int e)
{
    doublelinklist p;
    int j=0;
    p=l;
    while (p&&j<i)
    {
        p=p->next;
        j++;
    }
    if(!p||j>i)
        return 0;
    p->prior->next=p->next;
    if(p->next)
        p->next->prior=p->prior;
    delete p;  //删除操作不要忘
    return 1;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值