今天开始学习数据结构,刚开始接触会对新知识产生排斥感,一天下来感觉没啥收获。╮(╯▽╰)╭
不过还是赚了一段代码,嘿嘿。
十进制对任意进制的转换:
#include <iostream> #include <stack> #include <algorithm> using namespace std; void conversion() { int n,m; stack<int>S; cin>>n>>m; while(n) { S.push(n%m); n=n/m; } while(!S.empty()) { cout<<S.top(); S.pop(); } } int main() { conversion(); cout<<endl; return 0; }
栈的C格式输入输出(整型),调用:
#include<iostream> using namespace std; struct mystack { int a[10001]; int pos; }s; void push(int elem) { s.pos++; s.a[s.pos]=elem; } int top() { return s.a[s.pos]; } void pop() { s.pos--; } int main() { s.pos=-1; push(1); push(2); cout<<top()<<endl; pop(); cout<<top()<<endl; }(字符型)调用函数:
#include<stdio.h> struct Stack { char str[10005]; int top; }; void InitStack(Stack& a) { a.top=-1; } void push(Stack& a,char item) { a.top++; a.str[a.top]=item; } void pop(Stack& a) { a.top--; }队列:稍稍了解了下队列,看到书上有道题太经典了,所以就抄下来了:
题目:桌上有一叠牌,从第一张牌(既位于顶面的牌)开始从上往下依次编号为1——n。当至少还剩两张牌事进行以下操作:把第一张牌扔掉,然后把新的第一张放到整叠牌的最后。输出n,输出每次扔掉的牌,以及最后剩下的牌。
样例输入:7
样例输出:1 3 5 7 4 2 6
C++ STL代码:
#include<cstdio> #include<queue> using namespace std; queue<int> q; int main() { int n; scanf("%d",&n); for(int i=0;i<n;i++) q.push(i+1);//**初始化队列**// while(!q.empty())//**当队列非空**// { printf("%d",q.front());//**打印队首元素**// q.pop();//**抛弃队首元素**// q.push(q.front());//**把队首元素加入队尾**// q.pop();//**抛弃队首元素**// } return 0; }