1:average case O(n) 2:No usused memory extra memory for pointer variables (memory may be available as multiple small blocus) 3: (1)at begining O(1) (2)at endO(n) 要先全部遍历一遍,所以花费时间和链表元素成正比 (3)at ith O(n) 4:n
//string reversal using stack #include<bits/stdc++.h> #include<stack>//stack from standard template libraty (stl) usingnamespace std ; voidReverse(char *C,int n){ stack<char>S;//C++中的标准模板库(STL)组件,定义了一个名为S的栈(stack),其中S存储char类型的元素 for(int i=0;i<n;i++){//loop for push 这种情况下时间复杂度为O(n) S.push(C[i]); } for(int i=0;i<n;i++){//loop for pop 这种情况下时间复杂度为O(n) C[i]=S.top();//overwrite the character at index i S.pop(); //perform pop } } intmain(){ char C[51]; printf("Enter a string: "); gets(C); Reverse(C,strlen(C)); printf("Output: %s",C); }
scanf from left to right .if opening symbol,add it to a list;if closeing symbols, remove last opening symbol in list.should end witn an empty list.
伪代码逻辑思路
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Check Balanced Parebthesis(exp){ n<-length(exp) Create a stack:-S for i<-0 to n-1 { ifexp[i] is '('or'{'or'[' push(exp[i]) elseifexp[i]is ')'or'}'or']' { if (S is empty)|| { top dose not pair with exp[i], return flase } else pop() } } return S is empty? true or false }//扫描结束栈为空
intmain() { /*Code to test the function AreParanthesesBalanced*/ string expression; cout<<"Enter an expression: "; // input expression from STDIN/Console来自 STDIN/Console 的输入表达式 cin>>expression; if(AreParanthesesBalanced(expression)) cout<<"Balanced\n"; else cout<<"Not Balanced\n"; }
#include<iostream> using namespace std; #define MAX_SIZE 101 //maximum size of the array that will store Queue.
// Creating a class named Queue. classQueue { private: int A[MAX_SIZE]; int front, rear; public: // Constructor - set front and rear as -1. // We are assuming that for an empty Queue, both front and rear will be -1. Queue() { front = -1; rear = -1; }
// To check wheter Queue is empty or not boolIsEmpty() { return (front == -1 && rear == -1); }
// To check whether Queue is full or not boolIsFull() { return (rear+1)%MAX_SIZE == front ? true : false; }
// Inserts an element in queue at rear end voidEnqueue(int x) { cout<<"Enqueuing "<<x<<" \n"; if(IsFull()) { cout<<"Error: Queue is Full\n"; return; } if (IsEmpty()) { front = rear = 0; } else { rear = (rear+1)%MAX_SIZE; } A[rear] = x; }
// Removes an element in Queue from front end. voidDequeue() { cout<<"Dequeuing \n"; if(IsEmpty()) { cout<<"Error: Queue is Empty\n"; return; } elseif(front == rear ) { rear = front = -1; } else { front = (front+1)%MAX_SIZE; } } // Returns element at front of queue. intFront() { if(front == -1) { cout<<"Error: cannot return front from empty queue\n"; return-1; } return A[front]; } /* Printing the elements in queue from front to rear. This function is only to test the code. This is not a standard function for Queue implementation. */ voidPrint() { // Finding number of elements in queue int count = (rear+MAX_SIZE-front)%MAX_SIZE + 1; cout<<"Queue : "; for(int i = 0; i <count; i++) { int index = (front+i) % MAX_SIZE; // Index of element while travesing circularly from front cout<<A[index]<<" "; } cout<<"\n\n"; } }; intmain() { /*Driver Code to test the implementation Printing the elements in Queue after each Enqueue or Dequeue */ Queue Q; // creating an instance of Queue. Q.Enqueue(2); Q.Print(); Q.Enqueue(4); Q.Print(); Q.Enqueue(6); Q.Print(); Q.Dequeue(); Q.Print(); Q.Enqueue(8); Q.Print(); }
#include<stdio.h> #include<stdlib.h> structNode { int data; structNode* next; }; // Two glboal variables to store address of front and rear nodes. structNode* front =NULL; structNode* rear =NULL;
intmain(){ /* Drive code to test the implementation. */ // Printing elements in Queue after each Enqueue or Dequeue Enqueue(2); Print(); Enqueue(4); Print(); Enqueue(6); Print(); Dequeue(); Print(); Enqueue(8); Print(); }
// Function to print Nodes in a binary tree in Level order voidLevelOrder(Node *root){ if(root == NULL) return; queue<Node*> Q; Q.push(root); //while there is at least one discovered node while(!Q.empty()) { Node* current = Q.front(); Q.pop(); // removing the element at front cout<<current->data<<" "; if(current->left != NULL) Q.push(current->left); if(current->right != NULL) Q.push(current->right); } } // Function to Insert Node in a Binary Search Tree Node* Insert(Node *root,char data){ if(root == NULL) { root = newNode(); root->data = data; root->left = root->right = NULL; } elseif(data <= root->data) root->left = Insert(root->left,data); else root->right = Insert(root->right,data); return root; }
intmain(){ /*Code To Test the logic Creating an example tree M / \ B Q / \ \ A C Z */ Node* root = NULL; root = Insert(root,'M'); root = Insert(root,'B'); root = Insert(root,'Q'); root = Insert(root,'Z'); root = Insert(root,'A'); root = Insert(root,'C'); //Print Nodes in Level Order. LevelOrder(root); }
//Function to visit nodes in Preorder voidPreorder(struct Node *root){ // base condition for recursion // if tree/sub-tree is empty, return and exit if(root == NULL) return;
printf("%c ",root->data); // Print data Preorder(root->left); // Visit left subtree Preorder(root->right); // Visit right subtree }
//Function to visit nodes in Inorder voidInorder(Node *root){ if(root == NULL) return;
Inorder(root->left); //Visit left subtree printf("%c ",root->data); //Print data Inorder(root->right); // Visit right subtree }
//Function to visit nodes in Postorder voidPostorder(Node *root){ if(root == NULL) return;
Postorder(root->left); // Visit left subtree Postorder(root->right); // Visit right subtree printf("%c ",root->data); // Print data }
// Function to Insert Node in a Binary Search Tree Node* Insert(Node *root,char data){ if(root == NULL) { root = newNode(); root->data = data; root->left = root->right = NULL; } elseif(data <= root->data) root->left = Insert(root->left,data); else root->right = Insert(root->right,data); return root; }
intmain(){ /*Code To Test the logic Creating an example tree M / \ B Q / \ \ A C Z */ Node* root = NULL; root = Insert(root,'M'); root = Insert(root,'B'); root = Insert(root,'Q'); root = Insert(root,'Z'); root = Insert(root,'A'); root = Insert(root,'C'); //Print Nodes in Preorder. cout<<"Preorder: "; Preorder(root); cout<<"\n"; //Print Nodes in Inorder cout<<"Inorder: "; Inorder(root); cout<<"\n"; //Print Nodes in Postorder cout<<"Postorder: "; Postorder(root); cout<<"\n"; }
/* C++ program to find Inorder successor in a BST */ #include<iostream> usingnamespace std; structNode { int data; structNode *left; structNode *right; };
//Function to find some data in the tree Node* Find(Node*root, int data){ if(root == NULL) returnNULL; elseif(root->data == data) return root; elseif(root->data < data) returnFind(root->right,data); elsereturnFind(root->left,data); }
//Function to find Node with minimum value in a BST structNode* FindMin(struct Node* root) { if(root == NULL) returnNULL; while(root->left != NULL) root = root->left; return root; }
//Function to find Inorder Successor in a BST structNode* Getsuccessor(struct Node* root,int data) { // Search the Node - O(h) structNode* current = Find(root,data); if(current == NULL) returnNULL; if(current->right != NULL) { //Case 1: Node has right subtree returnFindMin(current->right); // O(h) } else { //Case 2: No right subtree - O(h) struct Node* successor = NULL; structNode* ancestor = root; while(ancestor != current) { if(current->data < ancestor->data) { successor = ancestor; // so far this is the deepest node for which current node is in left ancestor = ancestor->left; } else ancestor = ancestor->right; } return successor; } }
//Function to visit nodes in Inorder voidInorder(Node *root){ if(root == NULL) return;
Inorder(root->left); //Visit left subtree printf("%d ",root->data); //Print data Inorder(root->right); // Visit right subtree }
// Function to Insert Node in a Binary Search Tree Node* Insert(Node *root,char data){ if(root == NULL) { root = newNode(); root->data = data; root->left = root->right = NULL; } elseif(data <= root->data) root->left = Insert(root->left,data); else root->right = Insert(root->right,data); return root; }
intmain(){ /*Code To Test the logic Creating an example tree 5 / \ 3 10 / \ \ 1 4 11 */ Node* root = NULL; root = Insert(root,5); root = Insert(root,10); root = Insert(root,3); root = Insert(root,4); root = Insert(root,1); root = Insert(root,11);
//Print Nodes in Inorder cout<<"Inorder Traversal: "; Inorder(root); cout<<"\n";
// Find Inorder successor of some node. structNode* successor = Getsuccessor(root,1); if(successor == NULL) cout<<"No successor Found\n"; else cout<<"Successor is "<<successor->data<<"\n"; }