一、理论提要:
1. 栈的要点:环形缓冲区。视"栈"为一个环,这样就可以轻易的执行入栈和出栈的操作
2.用数据结构实现队列时,关键在于如何有效利用内存
3.双向链表:在向双向链表中添加元素时,只需要改变几个指针的指向
4.双向链表的特点:插入快但是检索慢
5.用C++ stl实现栈
6.用C++ stl的queue关键字来实现队列
7. stl中的list:(双向链表)
既可以像vector一样通过[]来访问特定的元素,也可以用迭代器逐个进行访问。另外,其元素的插入与删除只需要O(1)即可完成
2.队列:
1. 栈的要点:环形缓冲区。视"栈"为一个环,这样就可以轻易的执行入栈和出栈的操作
2.用数据结构实现队列时,关键在于如何有效利用内存
3.双向链表:在向双向链表中添加元素时,只需要改变几个指针的指向
4.双向链表的特点:插入快但是检索慢
5.用C++ stl实现栈
6.用C++ stl的queue关键字来实现队列
7. stl中的list:(双向链表)
既可以像vector一样通过[]来访问特定的元素,也可以用迭代器逐个进行访问。另外,其元素的插入与删除只需要O(1)即可完成
二、代码:
1.栈:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int top,s[1000];
void push(int x){
s[++top] = x;
}
int pop(){
top --;
return s[top+1];
}
int main(){
int a,b;
top = 0 ;
char s[100];
while (scanf("%s",s) != EOF){
if (s[0] == '+'){
a = pop();
b = pop();
push(a+b);
} else if ( s[0] == '-' ){
b = pop();
a = pop();
push(a-b);
}else if ( s[0] == '*'){
a = pop();
b = pop();
push(a*b);
}else {
push(atoi(s));
}
}
printf("%d\n",pop());
return 0;
}
2.队列:
//
// Created by 叶子 on 2018/1/20.
//
#include <stdio.h>
#include <string>
#define LEN 100005
typedef struct pp {
char name[100];
int t;
} P;
P Q[LEN];
int head,tail,n;
void enquene(P x){
Q[tail] = x;
tail = (tail + 1 )% LEN;
}
P dequene(){
P x = Q[head];
head = (head + 1) % LEN;
return x;
}
int min(int a,int b) { return a < b ? a : b ; }
int main(){
int elaps = 0 ,c ;
int i ,q;
P u;
scanf("%d %d",&n,&q);
for ( i = 1 ; i <= n ; i ++){
scanf("%s",Q[i].name);
scanf("%d",&Q[i].t);
}
head = 1; tail = n + 1;
while ( head != tail){
u = dequene();
c = min(q,u.t);
u.t -= c;
elaps += c;
if ( u.t > 0 ) enquene(u);
else{
printf("%s %d\n",u.name,elaps);
}
}
}
3.双向链表:
//
// Created by 叶子 on 2018/1/21.
//
#include <cstdio>
#include <cstdlib>
#include<cstring>
struct Node{
int key;
Node *next,*prev;
};
Node *nil;
Node* listSearch(int key){
Node *cur = nil->next;
while ( cur!= nil && cur->key !=key){
cur = cur->next;
}
return cur;
}
void init(){
nil = (Node *)malloc(sizeof(Node));
nil->next = nil;
nil->prev = nil;
}
void printList(){
Node *cur = nil->next;
int inf = 0 ;
while (1){
if (cur == nil) break;
if ( inf ++ > 0 ) printf (" ");
printf("%d",cur->key);
cur = cur->next;
}
printf("\n");
}
void deleteNode(Node *t){
if ( t == nil) return;
t->prev->next = t->next;
t->next->prev = t->prev;
}
void deleteFirst(){
deleteNode(nil->next);
}
void deleteLast(){
deleteNode(nil->prev);
}
void deleteKey(int key){
deleteNode(listSearch(key));
}
void insert(int key){
Node *x = (Node *)malloc(sizeof(Node));
x->key = key;
x->next = nil->next;
nil->next->prev = x;
nil->next = x;
x->prev = nil;
}
int main(){
int key,n,i;
int size = 0 ;
char com[20];
int np = 0 ,nd = 0;
scanf("%d",&n);
init();
for ( i = 0 ; i < n ; i ++){
scanf("%s%d",com,&key);
if ( com[0] == 'i' ){
insert(key);np++;size++;
}else if ( com[0] == 'd'){
if (strlen(com) > 6){
if ( com[6] == 'f' ) deleteFirst();
else if (com[6] == 'l' ) deleteLast();
}else{
deleteKey(key);nd++;
}
size --;
}
}
printList();
return 0;
}
4.用C++ stl实现栈
//
// Created by 叶子 on 2018/1/21.
//
#include<iostream>
#include<sstream>
#include<stack>
using namespace std;
int str2num(string s)
{
int num;
stringstream ss(s);
ss>>num;
return num;
}
int main(){
stack<int> S;
int a,b,x;
string s;
while(cin>>s){
if (s[0] == '+'){
a = S.top();S.pop();
b = S.top();S.pop();
S.push(a+b);
}else if( s[0] == '-'){
b = S.top();S.pop();
a = S.top();S.pop();
S.push(a-b);
}else if( s[0] == '*'){
a = S.top();S.pop();
b = S.top();S.pop();
S.push(a*b);
}else{
S.push(str2num(s));
}
}
return 0;
}
5.用C++ stl的queue关键字来实现队列:
//
// Created by 叶子 on 2018/1/21.
//
#include <iostream>
#include <queue>
using namespace std;
int main80(){
queue<string> Q;
Q.push("red");
Q.push("yellow");
Q.push("yellow");
Q.push("blue");
cout<<Q.front()<<" ";
Q.pop();
cout<<Q.front()<<" ";
Q.pop();
cout<<Q.front()<<" ";
Q.pop();
Q.push("green");
cout<<Q.front()<<" ";
Q.pop();
cout<<Q.front()<<" ";
Q.pop();
return 0;
}
6.stl中的list:(双向链表)
//
// Created by 叶子 on 2018/1/21.
//
#include "cstdio"
#include "list"
#include "algorithm"
using namespace std;
int main(){
int q,x;
char com[20];
list<int> v;
scanf("%d",&q);
int i ;
for(i = 0; i < q ; i ++){
scanf("%s",com);
if ( com[0] == 'i') {
scanf("%d",&x);
v.push_front(x);
}else if( com[6] == 'l') {
v.pop_back();
}else if ( com[6] == 'f'){
v.pop_front();
}else if ( com[0] == 'd') {
scanf("%d", &x);
for (list<int>::iterator it = v.begin(); it != v.end(); it++) {
if (*it == x) {
v.erase(it);
break;
}
}
}
}
for ( list<int>::iterator it = v.begin(); it != v.end();it ++){
if ( i ++ ) printf(" ");
printf("%d",*it);
}
printf("\n");
return 0;
}