SqList

#include <iostream>using namespace std;#include <malloc.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2typedef  int Status; /* int是函数的类型,其值是函数结果状态代码,如OK等 */#define LIST_INIT_SIZE 10 /* 线性表存储空间的初始分配量 */#define LISTINCREMENT 2 /* 线性表存储空间的分配增量 */typedef int ElemType;typedef struct{ElemType *elem;int length;int listsize;}SqList;Status InitList(SqList  &L) /* 算法2.3 */{L.elem=(int*)malloc(LIST_INIT_SIZE*sizeof(ElemType));if(!L.elem)exit(OVERFLOW);L.length=0;L.listsize=LIST_INIT_SIZE;return OK;}Status ListInsert(SqList &L, int i ,ElemType e) /* 算法2.4 */{ int *newbase,*q,*p;if(i<1||i>L.length+1) /* i值不合法 */return ERROR; if(L.length>=L.listsize) /* 当前存储空间已满,增加分配 */{newbase=(int *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));if(!newbase)exit(OVERFLOW); /* 存储分配失败 */L.elem=newbase; /* 新基址 */L.listsize+=LISTINCREMENT; /* 增加存储容量 */}q=L.elem+i-1; /* q为插入位置 */for(p=L.elem+L.length-1;p>=q;--p) /* 插入位置及之后的元素右移 */*(p+1)=*p;*q=e; /* 插入e */++L.length; /* 表长增1 */return OK;}Status ListDelete(SqList &L,int i,ElemType &e) /* 算法2.5 */{int *p,*q;if(i<1||i>L.length) /* i值不合法 */return ERROR;p=L.elem+i-1; /* p为被删除元素的位置 */e=*p; /* 被删除元素的值赋给e */q=L.elem+L.length-1; /* 表尾元素的位置 */for(++p;p<=q;++p) /* 被删除元素之后的元素左移 */*(p-1)=*p;L.length--; /* 表长减1 */return OK;}int LocateElem(SqList L, ElemType e)        /* 算法2.6*/{int *p;int i=1;p=L.elem; /* p的初值为第1个元素的存储位置 */while(i<=L.length&&(*p++!=e))++i;if(i<=L.length)return i;elsereturn 0;}void print(SqList L){int i;for(i=0;i<L.length;i++)printf("%d ", L.elem[i]);printf("\n");} int main(){SqList L;ElemType e;int i,n,t;InitList(L);cout<<"您要在表中插入几个元素:";cin>>n;for(i=1;i<=n;i++){cin>>e;ListInsert(L,i,e);}cout<<"目前表中元素为:";print(L);cout<<"请输入您要查询的值:";cin>>e;i=LocateElem(L,e);if(i==0)cout<<"查找失败"<<endl;else cout<<e<<"在表中的位置为:"<<i<<endl;cout<<"请输入您要删除元素的位置:";cin>>i;t=ListDelete(L,i,e); /* 删除第j个数据 */if(t==ERROR)cout<<"删除第"<<i<<"个数据失败"<<endl;elsecout<<"删除第"<<i<<"个的元素值为"<<e<<endl;cout<<"目前表中元素为:";print(L);system("PAUSE");return 1;}

LinkList

#include <iostream>using namespace std;#include <malloc.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2typedef  int Status;typedef int ElemType;typedef  struct  LNode {ElemType  data ;struct  LNode *next ;
} LNode, *LinkList ;
Status GetElem_L ( LinkList L, int i, ElemType &e )
{LinkList p;int j;p=L->next;j=1;while(p && j<i){p=p->next;j++;}if ( !p || j>i )return ERROR;
}
Status ListInsert_L ( LinkList &L, int i , ElemType e ) {// 在带头结点的单链表L中第i个数据元素之前插入数据元素eLinkList p,s;int j=0;p=L;while(p && j<i-1){p=p->next;j++;}if ( !p || j>i-1 )return ERROR;s=(LinkList)malloc(sizeof(LNode));s->data=e; s->next= p->next ;p->next =s ;return OK;
}Status ListDelete_L ( LinkList &L, int i , ElemType &e ) {// 在带头结点的单链表L中,删除第i个元素,并由e返回其值LinkList p=L,q;int j=0;while(p && j<i-1){p=p->next;j++;}if ( !p || j>i-1 )return ERROR;q=p->next;p->next=q->next;e=q->data;free(q);return OK;
}void  CreateList_L (LinkList &L, int n) {//逆位序输入n个元素的值,建立带头结点的单链表L。LinkList p;L=(LinkList) malloc(sizeof(LNode)) ;L->next=NULL;for( int i=n; i>0; --i){p= (LinkList) malloc(sizeof(LNode)) ;cin>>p->data;p->next=L->next;L->next=p;}
}void print(LinkList L)
{LinkList p;p=L->next;while(p){cout<<p->data;p=p->next;}
}
int main(){LinkList L;ElemType e;int i,n,t;cout<<"您要在表中插入几个元素:";cin>>n;CreateList_L(L,n);cout<<"目前表中元素为:";print(L);cout<<"请输入您要查询元素的位置:";cin>>i;GetElem_L(L,i,e);cout<<"元素值为:"<<e<<endl;cout<<"请输入您要删除元素的位置:";cin>>i;t=ListDelete_L(L,i,e);if(t==ERROR)cout<<"删除第"<<i<<"个数据失败"<<endl;elsecout<<"删除第"<<i<<"个的元素值为"<<e<<endl;cout<<"目前表中元素为:";print(L);system("PAUSE");return 1;}

Stack

#include <iostream>using namespace std;#include <stdio.h>#include <malloc.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define OVERFLOW -2typedef  int Status;#define  STACK_INIT_SIZE   100#define  STACKINCREMENT  10typedef int SElemType;typedef   struct {SElemType   *base ;SElemType   *top ;int       stacksize ;} SqStack;Status InitStack(SqStack &s ){s.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof( SElemType));if  (!s.base) exit (OVERFLOW) ;s.top=s.base ;s.stacksize=STACK_INIT_SIZE ;return  OK;}Status Push (SqStack &s, SElemType e){if (s.top-s.base >= s.stacksize){s.base=(SElemType*)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(SElemType));if (!s.base) exit (OVERFLOW);s.top=s.base+ s.stacksize;s.stacksize+= STACKINCREMENT ;}*s.top++=e ;return OK;
}Status Pop (SqStack &s, SElemType &e){if (s.top==s.base) return ERROR ;e=*--s.top ;return OK ;}Status StackEmpty(SqStack s ){if (s.top==s.base)  return  1;else  return  0;}void conversion(int N,int d){SqStack  s;int  x;InitStack(s);while( N )  {Push (s,N % d );N=N / d ;}cout<<"转换后的数值为:";while(!StackEmpty (s)){Pop (s,x ) ;if(d==16){if(x>=10)printf("%c",x-10+'A');elsecout<<x;}else cout<<x ;}
}int main(){int n,d;cout<<"您要把哪个整数转化为几进制:";cin>>n>>d;conversion(n,d);system("PAUSE");return 1;}

Queue

#include <iostream>using namespace std;#include <malloc.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2typedef  int Status;typedef int QElemType;typedef  struct  QNode {QElemType  data ;struct  QNode *next ;
} QNode, *Queueptr ;
typedef struct  {Queueptr   front;Queueptr   rear ;} Lqueue ;Status InitQueue(Lqueue &Q ){Q.front=Q.rear=(Queueptr)malloc(sizeof(QNode));if (!Q.front) exit (OVERFLOW) ;Q.front->next=NULL ;return OK;}Status Enqueue(Lqueue &Q,QElemType e ){Queueptr p;p=(Queueptr)malloc(sizeof(QNode));if (!p) exit (OVERFLOW) ;p->data=e ; p->next=NULL ;Q.rear->next=p; Q.rear=p;return OK;}Status Dequeue(Lqueue &Q,QElemType &e ){Queueptr p;if (Q.front==Q.rear) return ERROR;p=Q .front->next ;   e=p->data;Q.front->next=p->next;if (Q.rear==p)  Q.rear=Q.front;free(p);return OK;}int Locate (Lqueue Q, QElemType e )
{Queueptr p;int i;p=Q.front->next;i=1;while(p && p->data!=e){p=p->next;i++;}if ( p )return i;elsereturn 0;
}void print(Lqueue Q)
{Queueptr p;p=Q.front->next;while(p){cout<<p->data<<"  ";p=p->next;}cout<<endl;
}
int main(){Lqueue Q;QElemType e;int i,n,t;InitQueue(Q);cout<<"您要在队列中插入几个元素:";cin>>n;for(i=1;i<=n;i++){cin>>e;Enqueue(Q,e);}cout<<"目前队列中元素为:";print(Q);cout<<"请输入您要查询的元素";cin>>e;t=Locate(Q,e);if(t==0)cout<<e<<"不在队列中"<<endl;elsecout<<e<<"是队列中的第"<<t<<"个元素"<<endl;Dequeue(Q,e);cout<<e<<"出队"<<endl;cout<<"目前队列中元素为:";print(Q);system("PAUSE");return 1;}

Tree

#include <iostream>using namespace std;#include <malloc.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2typedef  int Status; typedef char TElemType;typedef struct BiTNode {TElemType  data;struct BiTNode   *lchild, *rchild;
} BiTNode,  *BiTree;void Preorder(BiTree T){ // 先序遍历二叉树 if (T) {cout<<T->data; // 访问结点Preorder(T->lchild); // 遍历左子树Preorder(T->rchild); // 遍历右子树}}
void Inorder(BiTree T){ // 中序遍历二叉树 if (T) {Inorder(T->lchild); // 遍历左子树cout<<T->data; // 访问结点Inorder(T->rchild); // 遍历右子树}}
void CountLeaf (BiTree T, int   &count){  if ( T ) {if ((!T->lchild)&& (!T->rchild))     count++;CountLeaf( T->lchild, count); // 统计左子树中叶子结点个数CountLeaf( T->rchild, count); // 统计右子树中叶子结点个数}}int Depth (BiTree T ){  int depthval,depthLeft,depthRight;if ( !T )       depthval = 0;else{ depthLeft = Depth( T->lchild );depthRight= Depth( T->rchild );depthval = 1 +(depthLeft>depthRight?depthLeft:depthRight);}return depthval;}void CreateBiTree(BiTree &T) {// 按先序次序输入二叉树中结点的值(一个字符),//空格字符表示空树,构造二叉链表表示的二叉树T。char ch;scanf("%c",&ch);if (ch==' ')    T = NULL;else {if (!(T = (BiTNode *)malloc(sizeof(BiTNode))))exit(OVERFLOW);T->data = ch; // 生成根结点CreateBiTree(T->lchild); // 构造左子树CreateBiTree(T->rchild); // 构造右子树}} // CreateBiTree int main()
{BiTree T;int leafnum=0,depth;cout<<"Please input the preorder of the tree:";CreateBiTree(T);cout<<"The preorder of the tree is:";Preorder(T);cout<<endl;cout<<"The inorder of the tree is:";Inorder(T);cout<<endl;depth= Depth(T);cout<<"The depth of the tree is:"<<depth<<endl;CountLeaf(T,leafnum);cout<<"The leaf num of the tree is:"<<leafnum<<endl; system("PAUSE");return 1;
}

邻接矩阵Graph

#include<iostream>
using namespace std;#define MAXSIZE 9 // 存储空间初始分配量
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0typedef int Status;
typedef char VertexType;typedef struct
{VertexType vexs[MAXSIZE];int  arcs[MAXSIZE][MAXSIZE];int vexnum,arcnum;
}Graph;int LocateVex(Graph G,char v)
{for(int i=0;i<G.vexnum;i++)if(G.vexs[i]==v)return i;return -1;
}
// 建立图的邻接矩阵
void  CreateUDG(Graph &G)
{int i,j,k;printf("输入顶点数和边数:\n");cin>>G.vexnum>>G.arcnum;for(i = 0;i < G.vexnum;i++)cin>>G.vexs[i];for(i = 0; i < G.vexnum;i++)for(j = 0; i < G.vexnum;i++)G.arcs[i][j]=0;for(k = 0;k < G.arcnum;k++){char v1,v2;cout<<"输入边上的两个顶点:\n";cin>>v1>>v2;i=LocateVex(G,v1);j=LocateVex(G,v2);G.arcs[i][j]=G.arcs[j][i]=1;}
}int FirstAdjVex(Graph G, int v)
{int j;for(j=0;j<G.vexnum;j++)if(G.arcs[v][j]==1)return j;return -1;
}int NextAdjVex(Graph G, int v, int w)
{int j;for(j=w+1;j<G.vexnum;j++)if(G.arcs[v][j]==1)return j;return -1;
}int  visited[MAXSIZE];// 邻接矩阵的深度优先递归算法
void DFS(Graph G, int v)
{visited[v] = TRUE;cout<<G.vexs[v];for(int w = FirstAdjVex(G, v); w>=0; w = NextAdjVex(G, v, w))if ( !visited[w] ) DFS(G, w);
}//邻接矩阵的深度遍历操作
void DFSTraverse(Graph G)
{int i;for(i = 0; i < G.vexnum; i++)visited[i] = 0;for(i = 0; i < G.vexnum; i++)if(!visited[i])DFS(G, i);
}int main()
{Graph G;CreateUDG(G);cout<<"深度遍历:";DFSTraverse(G);system("PAUSE");return 0;
}

数据结构课程上机参考代码相关推荐

  1. 西工大数据结构实验NOJ参考代码和分析合集

    实验1.1 合并有序数组 //001合并有序数组 #include <bits/stdc++.h> #define MAXSIZE 20 //数组的最大长度为20 typedef stru ...

  2. 数据结构课程设计实验验优参考(附数据结构上机实验、上机考试代码)

    计算机的几个专业的数据结构考试内容有所不一样,好像上机还是什么不太一样.软件工程那边的老师还要求学会C++读取文件,后面我也会附上读取文件的相关代码,其余的实验上机.上机考试代码等等在资料区下载即可. ...

  3. 校园导游系统数据结构课程设计(附完整代码)

    1 问题内容与目的要求 1.1 算法产生的背景: Floyd 算法又称为加点法.插点法,是一种利用动态规划的思想寻找给定的加权图中多源点之间最短路径的算法.该算法名称以创始人之一.1978 年图灵奖获 ...

  4. 数据结构课程设计报告(附代码)

    数据结构课程设计报告 一.实训目的 通过课程设计,学会运用数据结构知识,针对具体应用,自己设计合理数据结构,确定存储结构,并能设计具体操作算法,选择使用具体语言进行实现.掌握C++较复杂程序的组织和设 ...

  5. c语言学生管理系统结点,学生管理系统(数据结构课程设计之完整代码)

    <学生管理系统(数据结构课程设计之完整代码)>由会员分享,可在线阅读,更多相关<学生管理系统(数据结构课程设计之完整代码)(14页珍藏版)>请在人人文库网上搜索. 1.数据结构 ...

  6. 数据结构迷宫代码_数据结构课程设计——迷宫求解(二)

    前言 接上文的介绍,本文将主要介绍如何生成随机迷宫,在网上找到的资源也比较多,这里我选取了随机 Prim 算法生成迷宫,选择这个算法的理由如下: 算法思想简单,易于实现 生成的迷宫比较自然,不会出现明 ...

  7. java初始化三门课程_[Java] 实验4参考代码

    实验3月27日晚截止,实验截止后将在此给出完整的参考代码. 问之前请检查代码缩进,不了解什么是缩进的可以参考什么是代码缩进(code indent), 或与周围同学讨论. 缩进不规范的问题概不回答. ...

  8. 《 Python程序设计项目案例》— 人脸识别考勤可视化分析系统签到打卡记录到Excel表格项目参考代码(课程设计、期末结课大作业、毕业设计)

    声明 本文仅在CSDN发布,其他均为盗版.请支持正版! 正版链接: https://blog.csdn.net/meenr/article/details/107348867 Python课程设计(程 ...

  9. C/C++《数据结构课程设计》任务书[2022-12-27]

    C/C++<数据结构课程设计>任务书[2022-12-27] <数据结构课程设计>任务书 一.任务总体安排: 班级 设计时间 地点 指导老师 21软件开发 17周每周一至周五五 ...

最新文章

  1. 【译】Angular 5 新特性
  2. 如何在gvim中安装autoproto自动显示函数原型
  3. 中文分词的古今中外,你想知道的都在这里
  4. 如何提升测试环境的稳定性?来看看阿里内部的实践总结
  5. Maven集成指令总结
  6. 华为手表广告营销案例和广告策划案例PPT模板
  7. 缠论找日线找第二类买点买入程序
  8. linux查看数据区块大小,Linux显示指定区块大小为1048576字节
  9. Infor和C3 AI建立战略合作伙伴关系,提供可扩展的企业AI行业应用
  10. RoaringBitmap应用场景
  11. SRS:连麦和多个视频号联播
  12. Kinect for Unity3d----KinectManager
  13. 【转】使用matlab软件打开一幅图片并且分别提取其中的RGB分量并显示
  14. 浅谈NAT(网络地址转换)原理 + 个人的思考
  15. 第六届360前端星计划_深入CSS
  16. 小蒟蒻的小生活(持续更新)
  17. DataV轮播表组件dv-scroll-board宽度问题
  18. Linux学习日记4——ftp、lftp、nfs
  19. 如何不碌碌无为的活着?
  20. PyQt Phonon 模块初探

热门文章

  1. SVG.path_不连续的线段
  2. 如何用php实现分页效果
  3. 【D3】transition API
  4. java动态加载配置文件
  5. 电路实验1-电容充放电
  6. C#下载远程文件到本地
  7. 2003網域升級到2008網域以及遷移DNS
  8. Nginx源码分析链接
  9. 到底什么是hash?它起什么作用?
  10. jQuery 表格插件汇总