1.单链表的基本操作
#include <iostream>
#include <stack>
using namespace std;
typedef struct ListNode{
int val;
struct ListNode* next;
}ListNode, *LinkedList;
/*struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}//这个构造函数很常用
ListNode(int x, ListNode *next) : val(x), next(next) {}
} *LinkedList;*/
//初始化
LinkedList InitList(){
ListNode* p;
p=new ListNode;//p=(ListNode* )malloc(sizeof(ListNode))
if(p==NULL){
cout<<"申请空间失败!"<<endl;
}
return p;
}
//创建单链表
LinkedList CreateList(int a[],int length){
ListNode* p=new ListNode;
ListNode* temp=p;
p->val=a[0];
p->next=NULL;
for(int i=1;i<length;++i){
int x=a[i];
ListNode* a=new ListNode;
a->val=x;
a->next=NULL;
p->next=a;
p=p->next;
}
return temp;
}
//数据x插在k的位置
LinkedList InsertList (LinkedList L,int x,int k){//需要设置一个前驱节点,
ListNode* a=new ListNode;
a->val=x;
a->next=NULL;
if(k==1){
a->next=L;
return a;
}
ListNode* p=L;
for(int i=1;i<k-1;++i){
p=p->next;
}
ListNode* temp=p->next;
p->next=a;
a->next=temp;
return L;
}
//单链表的删除,在链表中删除x这个元素
LinkedList DeleteList(LinkedList L,int k,int &e){//删除第k个元素,并由e返回其值.没有办法先判断k的大小合理不合理
//寻找第k个节点,并令p指向其前驱;删除位置不合理;删除并释放节点.
ListNode* p=L;
int i=1;
while(p->next!=NULL && i<k-1){//寻找第k个节点,并令p指向其前驱.i应该增加到k-1
p=p->next;
++i;
cout<<"i="<<i<<endl;
}
cout<<"i="<<i<<endl;
if(i>k-1||p->next==NULL){
cout<<"删除位置不合理!"<<endl;
return NULL;
}
ListNode* temp=p->next;
e=temp->val;
p->next=p->next->next;
delete temp;
return L;
}
void Traverse(ListNode* p){
while(p!=NULL){
cout<<p->val<<" ";
p=p->next;
}
cout<<endl;
}
int main()
{
ListNode* L=InitList();
int a[]={1,2,3,4,5,6};
L=CreateList(a,6);
Traverse(L);
//L=InsertList(L,9,3);
//Traverse(L);
int c;
int &e=c;
DeleteList(L,5,e);
Traverse(L);
cout<<"e= "<<e<<endl;
int N;
cout<<"Input N"<<endl;
cin>>N;
stack<int> s;
while(N!=0){
s.push(N%8);
N=N/8;
}
while(!s.empty()){
cout<<s.top();
s.pop();
}
cout << "Hello world!" << endl;
return 0;
}
2.链队列的基本操作
#include <iostream>
using namespace std;
typedef struct Node{
int val;
struct Node* next;
//Node(int x): val(x), next(NULL) {}
} Node;
typedef struct queue{
Node* front;
Node* rear;
} queue;
Node* InitNode(){
Node* n=new Node;//(Node*)sizeof(Node)
if(n==NULL){
//建立失败退出 正常运行程序并退出
exit(0);
}
return n;
}
queue* InitQueue(){
queue* q=new queue;
if(q==NULL){
exit(0);
}
q->front=NULL;
q->rear=NULL;
return q;
}
int empty(queue *q){//判断队列是不是为空
return q->front==NULL;
}
void push(int data,queue* q){
Node* a=new Node;
a->val=data;
a->next=NULL;
if(empty(q)){
q->front=a;
q->rear=a;
}
else{
q->rear->next=a;//a成为当前尾结点的下一结点,实际上这一句就可以把节点连起来
q->rear=a; //让尾指针指向a
}
}
void pop(queue* q){
if(empty(q)){
cout<<"队列为空"<<endl;
return;
}
else{
Node* temp=q->front;
q->front=temp->next;
delete temp;
}
}
void Traverse(queue* q){
if(empty(q)){
cout<<"队列为空"<<endl;
return;
}
else{
Node* p=q->front;
while(p!=NULL){
cout<<p->val<<" ";
p=p->next;
}
cout<<endl;
}
}
int main()
{
queue* q=InitQueue();
push(1,q);
push(2,q);
push(3,q);
Traverse(q);
pop(q);
Traverse(q);
pop(q);
Traverse(q);
pop(q);
Traverse(q);
cout << "Hello world!" << endl;
return 0;
}
3.二叉搜索树的创建,插入和搜索(BST是高频)
查询复杂度是O(logn)到 O(n)
#include <iostream>
using namespace std;
typedef struct TreeNode{
int val;
TreeNode* left;
TreeNode* right;
//TreeNode(int x) :val(x) ,left(nullptr), right(nullptr) {}
}TreeNode;
//树根,能不能不用树根感觉好麻烦
typedef struct{
TreeNode* root;
} Tree;
void insert(Tree* tree,int value){
TreeNode* a=new TreeNode;
a->val=value;
a->left=nullptr;
a->right=nullptr;
if(tree->root==NULL){//判断树是不是空树
tree->root=a;
return;
}
else{//不是空树
TreeNode* p=tree->root;//先从根节点开始
while(p!=NULL){
if(p->val==value){
cout<<"数值不被允许"<<endl;
exit(0);
}
else if(value<p->val){//小于就进左儿子
if(p->left==NULL){
p->left=a;
return;
}
else p=p->left;
}
else if(value>p->val){//大于就进右儿子
if(p->right==NULL){
p->right=a;
return;
}
else p=p->right;
}
}
return;
}
}
void preOrderTraverse(TreeNode* root){
if(root==NULL) return;
else{
cout<<root->val<<" ";
preOrderTraverse(root->left);
preOrderTraverse(root->right);
}
}
void inOrderTraverse(TreeNode* root){
if(root==NULL) return;
else{
inOrderTraverse(root->left);
cout<<root->val<<" ";
inOrderTraverse(root->right);
}
}
TreeNode* search(Tree* tree, int x){//查找
if(tree->root==NULL) return NULL;
TreeNode* p=tree->root;
while(p!=NULL){
if(x==p->val){
return p;
}
else if(x<p->val){
p=p->left;
}
else{
p=p->right;
}
}
return NULL;
}
int main()
{
Tree tree;
tree.root=NULL;
//int N;
//cin>>N;
//int temp;
//for(int i=0;i<N;++i){
//cin>>temp;
//insert(&tree,temp);
//}
int a[8]={8,3,10,1,6,14};
for(int i=0;i<6;++i){
insert(&tree,a[i]);
}
preOrderTraverse(tree.root);
cout<<endl;
inOrderTraverse(tree.root);
cout<<endl;
TreeNode* res=search(&tree,1);
cout<<"Search result: "<<res->val<<endl;
cout << "Hello world!" << endl;
return 0;
}