数据结构——10.24

2023-10-28 01:30
文章标签 数据结构 10.24

本文主要是介绍数据结构——10.24,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这里写目录标题

  • 回文
  • 双向栈
  • 双向队列1
  • 双向队列2

回文

#include<bits/stdc++.h>
using namespace std;bool is_huiwen(  string s)
{for(int i = 0; i < s.size() / 2; i++){if( s[i] != s[ s.size() - i - 1 ] ){return false;}}return true;
}int main(){string ss;int option;while( 1 ){cout << "请选择你的操作,1判断回文,0退出\n";cin >> option;if( option == 0 ){break;}else{cout << "qing input a string\n"; cin >> ss;if( is_huiwen( ss ) ){cout << "YES!该字符串 是 回文的\n";}else{cout << "NO!该字符串 不是 回文的\n";} }}return 0;
}

双向栈

#include<stdio.h>
#include<stdlib.h>
#define maxsize 10 
#include<bits/stdc++.h>
using namespace std;             
typedef struct {int data[maxsize];int left;                         int right;                         
}DoubleStack,*Double;int Init(Double &L){L=(Double)malloc(sizeof(DoubleStack));if(L==NULL) return -1;L->left=-1;                  L->right=maxsize;           return 1; }
//入栈
int push(Double &L,int status,int x){  if(L->left+1==L->right){printf("栈满!");return -1;} if(status==1){L->left++;            L->data[L->left]=x;   }else if(status==2){L->right--;           L->data[L->right]=x;  }
}
//出栈
int pop(Double &L,int status) {if(status==1){if(L->left<0) {printf("左栈为空!\n"); return -1;}else{printf("出%d ",L->data[L->left]);    L->data[L->left]=0;                   L->left--;}}else if(status==2){if(L->right>maxsize-1){printf("右栈为空!\n"); return -1;}else{printf("出%d ",L->data[L->right]);   L->data[L->right]=0;                 L->right++;}}
}void Print(Double &L) {int i,j;for(i=0;i<=maxsize-1;i++){     if(L->data[i]!=0){                 printf("%d ",L->data[i]);}else{printf(" * ");} }}
int main(){DoubleStack *s;char L,R;if(Init(s)==1){printf("初始化成功!\n");}while (1){int a;int xxx;int x_val;cout << "\n  1左入栈\n  2右入栈\n  3左出栈\n  4右出栈\n  5遍历栈(左到右)  0退出" << endl;cout << "请选择要进行的操作:";cin >> a;switch (a){case 1:cout << "请选择左入栈个数:";cin >> xxx;cout << "请依次输入入栈元素:\n";for(int i = 1; i <= xxx ; i++){cin >> x_val;push(s,1,x_val);  }break;case 2:cout << "请选择右入栈个数:";//int xxx;cin >> xxx;cout << "请依次输入入栈元素:\n";for(int i = 1; i <= xxx ; i++){cin >> x_val;push(s,2,x_val);  }break;case 3:cout << "请选择左出栈个数:";int xxx;cin >> xxx;//cout << "请依次输入入栈元素:\n";for(int i = 1; i <= xxx ; i++){//cin >> x_val;pop(s,1);}break;case 4:cout << "请选择右出栈个数:";//int xxx;cin >> xxx;//cout << "请依次输入入栈元素:\n";for(int i = 1; i <= xxx ; i++){//cin >> x_val;pop(s,2);}break;case 5:printf("此时栈的元素为:");Print(s);break; case 0: return 1;default:return 1;}}} 

双向队列1

#include<iostream>
using namespace std;
#define OK 1
#define MAXQSIZE 5typedef struct
{int* base;  int front;		  int rear;         
}SqQueue;int InitQueue(SqQueue&);	    
int EnQueue(SqQueue&, int);    
int DeQueue(SqQueue&, int &);  
int GetHead(SqQueue);	    
int QueueTraverse(SqQueue);	
//int QueueLength( SqQueue );
void is_full( SqQueue );int flag;int main()
{SqQueue S;int e, a;if (InitQueue(S))cout << "循环队列初始化成功!" << endl;elsecout << "循环队列初始化失败!" << endl;while (1){cout << "\n  1入队\n  2出队\n  3取队头元素\n  4输出队列\n   5求队列状态\n  0退出" << endl;cout << "请选择要进行的操作:";cin >> a;switch (a){case 1:int x, n;cout << "请输入要插入的元素个数:";cin >> n;for (int i = 0; i < n; i++) {//cout << "请输入第" << i + 1 << "元素值:";cin >> x;EnQueue(S, x);}cout << "入队完成!" << endl;break;case 2:cout << "请输入要删除的元素个数:";cin >> n;for (int j = 0; j < n; j++) {if (!DeQueue(S, e))cout << "出队失败!" << endl;elsecout << "第【" << j+1 << "】个元素:" << e << " 出队成功!" << endl;}break;case 3:cout << "队头元素为:" << GetHead(S) << endl;break;case 4:if(!QueueTraverse(S))cout << "队列为空!" << endl;break;
//		case 6:
//			QueueLength(S);
//			break;case 5:is_full( S );if( flag == 0 ){cout << "队列为空!\n"; }else cout << "队列是满的!\n";break; case 0: return OK;default:return OK;}}return 0;
}int InitQueue(SqQueue& Q)
{Q.base = new int[MAXQSIZE];if(!Q.base)return 0;   Q.front = Q.rear = 0;return OK;
}int EnQueue(SqQueue& Q, int e)
{if ((Q.rear + 1) % MAXQSIZE == Q.front)  return 0;    Q.base[Q.rear] = e;Q.rear = (Q.rear + 1) % MAXQSIZE;return OK;
}int DeQueue(SqQueue& Q, int &e) 
{if (Q.front == Q.rear)return 0;e = Q.base[Q.front];Q.front = (Q.front + 1) % MAXQSIZE;return OK;
}int GetHead(SqQueue Q)
{if (Q.front != Q.rear)   return Q.base[Q.front];   
}int QueueTraverse(SqQueue Q)
{cout << "当前队列为:";if (Q.front == Q.rear)return 0;while (Q.front != Q.rear)   {cout << Q.base[Q.front] << " ";Q.front = (Q.front + 1) % MAXQSIZE;}cout << endl;
}/*int QueueLength(SqQueue Q)
{int len = (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;cout << "循环队列的长度为:" << len << endl;return OK;
}
*/
void is_full( SqQueue Q ){if( Q.front == Q.rear ){int len = (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;if( len == MAXQSIZE ){flag = 1;}else flag = 0;}else flag = 1;
}

双向队列2

#include<iostream>
using namespace std;
#define OK 1
#define MAXQSIZE 5typedef struct
{int* base;  int front;		  int rear;         
}SqQueue;int InitQueue(SqQueue&);	    
int EnQueue(SqQueue&, int);    
int DeQueue(SqQueue&, int &);  
int GetHead(SqQueue);	    
int QueueTraverse(SqQueue);	
//int QueueLength( SqQueue );
void is_full( SqQueue );int flag;int main()
{SqQueue S;int e, a;if (InitQueue(S))cout << "循环队列初始化成功!" << endl;elsecout << "循环队列初始化失败!" << endl;S.base[MAXQSIZE ] = 0;while (1){cout << "\n  1入队\n  2出队\n  3取队头元素\n  4输出队列\n  5求队列状态\n  0退出" << endl;cout << "请选择要进行的操作:";cin >> a;switch (a){case 1:int x, n;cout << "请输入要插入的元素个数:";cin >> n;cout << "请依次输入要插入的元素:";for (int i = 0; i < n; i++) {cin >> x;EnQueue(S, x);}cout << "入队完成!" << endl;break;case 2:cout << "请输入要删除的元素个数:";cin >> n;for (int j = 0; j < n; j++) {if (!DeQueue(S, e))cout << "出队失败!" << endl;elsecout << "第" << j+1 << "个元素:" << e << " 出队成功!" << endl;}break;case 3:cout << "队头元素为:" << GetHead(S) << endl;break;case 4:if(!QueueTraverse(S))cout << "队列为空!" << endl;break;
//		case 6:
//			QueueLength(S);
//			break;case 5:is_full( S );break; case 0: return OK;default:return OK;}}return 0;
}int InitQueue(SqQueue& Q)
{Q.base = new int[MAXQSIZE + 1];if(!Q.base)return 0;   Q.front = 10;Q.rear = 0;return OK;
}int EnQueue(SqQueue& Q, int e)
{//cout << "wei " << Q.rear<< endl;if ((Q.rear + 1) % (MAXQSIZE ) == Q.front)  {//cout << "wei " << Q.rear<< endl;cout << "full!\n";return 0;}Q.base[Q.rear] = e;Q.rear = (Q.rear + 1) % (MAXQSIZE - 1 );Q.base[MAXQSIZE]++; //cout << "wei " << Q.rear<< endl;return 1;
}int DeQueue(SqQueue& Q, int &e) 
{if (Q.front == Q.rear)return 0;e = Q.base[Q.front];Q.front = (Q.front + 1) % (MAXQSIZE  );Q.base[MAXQSIZE  ]--;return OK;
}int GetHead(SqQueue Q)
{if (Q.front != Q.rear)   return Q.base[Q.front];   
}int QueueTraverse(SqQueue Q)
{cout << "当前队列为:";if (Q.front == Q.rear)return 0;while (Q.front != Q.rear)   {cout << Q.base[Q.front] << " ";Q.front = (Q.front + 1) % (MAXQSIZE  );}cout << endl;
}/*int QueueLength(SqQueue Q)
{int len = (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;cout << "循环队列的长度为:" << len << endl;return OK;
}
*/
void is_full( SqQueue Q ){if( Q.base[MAXQSIZE  ] == (MAXQSIZE ) ){cout << "队列是满的!\n";}else if( Q.base[MAXQSIZE  ] == 0 ){cout << "队列是空的!\n";}else{cout << "队列长度为:" << Q.base[MAXQSIZE] << endl;;} 
}

这篇关于数据结构——10.24的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/289767

相关文章

C#数据结构之字符串(string)详解

《C#数据结构之字符串(string)详解》:本文主要介绍C#数据结构之字符串(string),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录转义字符序列字符串的创建字符串的声明null字符串与空字符串重复单字符字符串的构造字符串的属性和常用方法属性常用方法总结摘

Go语言中三种容器类型的数据结构详解

《Go语言中三种容器类型的数据结构详解》在Go语言中,有三种主要的容器类型用于存储和操作集合数据:本文主要介绍三者的使用与区别,感兴趣的小伙伴可以跟随小编一起学习一下... 目录基本概念1. 数组(Array)2. 切片(Slice)3. 映射(Map)对比总结注意事项基本概念在 Go 语言中,有三种主要

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

6.1.数据结构-c/c++堆详解下篇(堆排序,TopK问题)

上篇:6.1.数据结构-c/c++模拟实现堆上篇(向下,上调整算法,建堆,增删数据)-CSDN博客 本章重点 1.使用堆来完成堆排序 2.使用堆解决TopK问题 目录 一.堆排序 1.1 思路 1.2 代码 1.3 简单测试 二.TopK问题 2.1 思路(求最小): 2.2 C语言代码(手写堆) 2.3 C++代码(使用优先级队列 priority_queue)

《数据结构(C语言版)第二版》第八章-排序(8.3-交换排序、8.4-选择排序)

8.3 交换排序 8.3.1 冒泡排序 【算法特点】 (1) 稳定排序。 (2) 可用于链式存储结构。 (3) 移动记录次数较多,算法平均时间性能比直接插入排序差。当初始记录无序,n较大时, 此算法不宜采用。 #include <stdio.h>#include <stdlib.h>#define MAXSIZE 26typedef int KeyType;typedef char In

【408数据结构】散列 (哈希)知识点集合复习考点题目

苏泽  “弃工从研”的路上很孤独,于是我记下了些许笔记相伴,希望能够帮助到大家    知识点 1. 散列查找 散列查找是一种高效的查找方法,它通过散列函数将关键字映射到数组的一个位置,从而实现快速查找。这种方法的时间复杂度平均为(

浙大数据结构:树的定义与操作

四种遍历 #include<iostream>#include<queue>using namespace std;typedef struct treenode *BinTree;typedef BinTree position;typedef int ElementType;struct treenode{ElementType data;BinTree left;BinTre

Python 内置的一些数据结构

文章目录 1. 列表 (List)2. 元组 (Tuple)3. 字典 (Dictionary)4. 集合 (Set)5. 字符串 (String) Python 提供了几种内置的数据结构来存储和操作数据,每种都有其独特的特点和用途。下面是一些常用的数据结构及其简要说明: 1. 列表 (List) 列表是一种可变的有序集合,可以存放任意类型的数据。列表中的元素可以通过索

浙大数据结构:04-树7 二叉搜索树的操作集

这道题答案都在PPT上,所以先学会再写的话并不难。 1、BinTree Insert( BinTree BST, ElementType X ) 递归实现,小就进左子树,大就进右子树。 为空就新建结点插入。 BinTree Insert( BinTree BST, ElementType X ){if(!BST){BST=(BinTree)malloc(sizeof(struct TNo

【数据结构入门】排序算法之交换排序与归并排序

前言         在前一篇博客,我们学习了排序算法中的插入排序和选择排序,接下来我们将继续探索交换排序与归并排序,这两个排序都是重头戏,让我们接着往下看。  一、交换排序 1.1 冒泡排序 冒泡排序是一种简单的排序算法。 1.1.1 基本思想 它的基本思想是通过相邻元素的比较和交换,让较大的元素逐渐向右移动,从而将最大的元素移动到最右边。 动画演示: 1.1.2 具体步