实验

1.array

#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<iostream>
#include<string.h>
using namespace std;

#define MAX 100
struct Array
{
    int *head;//头结点
    int len;//长度
    int num;//有效位数
};
void Creatarray(struct Array &A,int length)
{
    A.head=(int *)malloc(sizeof(int)*length);
    if(A.head==NULL)
    {
        cout<<"wrong!";
        exit(-1);
    }
    A.num=0;
    A.len=length;
}
int seek(struct Array &A,int x)
{
    for(int i=0;i<A.num;i++)
    {
        if(A.head[i]==x)
        {
            cout<<x<<"is in the "<<i;
            return i;
        }
    }
    cout<<"not in the array";
    return 0;
}
void Initarray(struct Array &A,int x)
{
    if(A.num<A.len)
    {
        A.head[A.num]=x;
    A.num++;
    }
    else
        cout<<"can't initarray";
}
void Delearray(struct Array &A,int pos)
{
    if(A.num==0)
        cout<<"it is empty";
    else
    {
        for(int i=pos;i<A.num;i++)
    {
        A.head[i]=A.head[i+1];
    }
    A.num--;
    }

}
void Showarray(struct Array A)
{
    cout<<"\n";
    if(A.num==0)
        cout<<"it is empty";
    else
        for(int i=0;i<A.num;i++)
        cout<<A.head[i]<<"  ";

}
void Isempty(struct Array A)
{
    if(A.num==0)
        cout<<"it is empty";
    else
        cout<<"it is not empty";
}
void Isfull(struct Array A)
{
    if(A.len==A.num)
        cout<<"it is full";
    else
        cout<<"it is not full";
}
bool compare(struct Array A,struct Array B)
{
    for(int i=0;i<min(A.num,B.num);i++)
    {
        if(A.head[i]==B.head[i])
            continue;
        else
            {
                cout<<"A and B is not equal;";
                return false;
            }
    }
    cout<<"A and B is equal;";
    return true;
}

void Union(struct Array A,struct Array B,struct Array &C)
{

    for(int i=0;i<A.num;i++)
    Initarray(C,A.head[i]);
    for(int j=0;j<B.num;j++)
    {
        int flag=0;
        int b=B.head[j];
        for(int i=0;i<C.num;i++)
        {
            if(b!=C.head[i])
                continue;
            else
                {
                    flag=1;
                    break;
                }
        }
            if(flag==0)
                Initarray(C,b);
            else
                continue;
    }
}
void Mix(struct Array A,struct Array B)
{
    for(int i=0;i<A.num;i++)
    {
        for(int j=0;j<B.num;j++)
        {
            if(A.head[i]==B.head[j])
                cout<<A.head[i]<<"  ";
            else
                continue;
        }
    }
}
int main()
{
    Array A;
    Creatarray(A,10);
    Array B;
    Creatarray(B,10);
    cout<<"how many numbers in A:";
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cout<<"input the number:";
        int a;
        cin>>a;
        Initarray(A,a);
    }
    Showarray(A);
    cout<<"\n";
    cout<<"how many number in B:";
    int m;
    cin>>m;
    for(int j=0;j<m;j++)
    {
        cout<<"input the number:";
        int b;
        cin>>b;
        Initarray(B,b);
    }
    Showarray(B);
    cout<<"\n";
    cout<<"compare A and B:"<<endl;
    compare(A,B);
    cout<<"\n";
    cout<<"A and B union:";
    Array C;
    Creatarray(C,20);
    Union(A,B,C);
    Showarray(C);
    cout<<"\n";
    cout<<"A and B mix:"<<endl;
    Mix(A,B);
    return 0;
}

2.sqlist

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;

struct Lnode
{
    char data;
    struct Lnode *next;
};

struct Linklist
{
    Lnode *head;
    int num;//元素个数
};

void Creatlist(struct Linklist &L)
{
    L.head=NULL;
    L.num=0;
}

void Insertlist(struct Linklist &L,char x)
{
    Lnode *node=new Lnode;
    node->data=x;
    node->next=L.head;
    L.head=node;
    L.num++;
}

void Seeklist(Linklist L,char x)
{
    if(L.num==0)
        cout<<"it is empty";
    else
    {
        Lnode *p=L.head;
        for(int i=0;i<L.num;i++)
        if(p->data==x)
            cout<<x<<"is in the"<<i;
        else
        {
             p=p->next;
        }

    }
}
void Delelist(Linklist &L,int pos)
{
    if(pos==0)
        L.head=L.head->next;
    else
    {
        Lnode *p,*q;
        p=L.head;
        for(int i=0;i<pos-1;i++)
           p=p->next;
           q=p->next;
           p->next=q->next;
           L.num--;
    }
}

void Showlist(struct Linklist L)
{
    Lnode *p;
    p=L.head;
    for(int i=0;i<L.num;i++)
        {
            cout<<i<<"->"<<p->data<<endl;
            p=p->next;
        }
}

void Isempty(struct Linklist L)
{
    if(L.num==0)
        cout<<"it is empty";
    else
        cout<<"it is not empty";
}


int main()
{
    Linklist L;
    Creatlist(L);
    cout<<"**************************************"<<endl;
    cout<<"    +   ->  Insert"<<endl;
    cout<<"    -   ->  Delelist"<<endl;
    cout<<"    C   ->  seek"<<endl;
    cout<<"    S   ->  showlist"<<endl;
    cout<<"    E   ->  isempty"<<endl;

    char str[20];
    cin>>str;
    int s=strlen(str);
    for(int i=0;i<s;i++)
    {
        switch(str[i])
            {
            case '+':{Insertlist(L,str[i+1]);i=i+1;break;}
            case '-':{Delelist(L,(int)(str[i+1]-'0'));i=i+1;break;}
            case 'C':{Seeklist(L,str[i+1]);i=i+1;break;}
            case 'S':{Showlist(L);break;}
            case 'E':{Isempty(L);break;}
            }
    }
    return 0;
}

3.stack

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>

using namespace std;
struct Stack
{
    int *elem;
    int top;
    int stacksize;
};

void Initstack(struct Stack &S,int maxsize)
{
    S.elem=new int[maxsize];
    S.top=-1;
    S.stacksize=maxsize;
}

void Push(struct Stack &S,int e)
{
    S.elem[++S.top]=e;
}

void Pop(struct Stack &S,int &e)
{
    if(S.top==-1)
        cout<<"it is empty";
    else
    {
        e=S.elem[S.top--];
    }
}

void Showstack(struct Stack S)
{
    if(S.top==-1)
        cout<<"it is empty";
    else
    {
        while(S.top!=-1)
        {
            cout<<S.elem[S.top--]<<"  ";
        }
    }
}

struct Queuenode
{
    int data;
    struct Queuenode* next;
};
struct Queue
{
    struct Queuenode*front;
    struct Queuenode*rear;
    int num;
};

void Initqueue(struct Queue &Q)
{
    Q.front=Q.rear=new Queuenode;
    Q.front->next=NULL;
    Q.num=0;
}

void Enqueue(struct Queue &Q,int x)
{
    Queuenode *p=new Queuenode;
    p->data=x;
    Q.rear->next=p;
    Q.rear=p;
}

void Dequeue(struct Queue &Q,int &e)
{
    if(Q.front==Q.rear)
        cout<<"it is empty";
    else{
        Queuenode *p;
        p=Q.front->next;
        e=p->data;
        Q.front->next=p->next;
        if(Q.rear==p)
            Q.rear=Q.front;
        delete(p);
    }
}

void Showqueue(struct Queue Q)
{
    if(Q.front==Q.rear)
        cout<<"it is empty";
    else
    {
        Queuenode *p=Q.front->next;
        while(p!=Q.rear)
        {
            cout<<p->data<<"  ";
            p=p->next;
        }
        cout<<Q.rear->data;
    }
}

int main()
{
    Stack S;
    Initstack(S,10);
    cout<<"****** stack******"<<endl;
    cout<<"how many numbers?";
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
         cout<<"input the number:";
         int d;
         cin>>d;
         Push(S,d);
    }
    cout<<"show the stack:"<<endl;
    Showstack(S);
    int e;
    Pop(S,e);
    cout<<"\n";
    cout<<"the top of stack:"<<e<<endl;
    cout<<"input the number enter stack:";
    int r;
    cin>>r;
    Push(S,r);
    Showstack(S);
    cout<<"\n";

    cout<<"******queue******"<<endl;
    Queue Q;
    Initqueue(Q);
    cout<<"how many numbers?";
    int k;
    cin>>k;
    for(int i=0;i<k;i++)
    {
        cout<<"input the number:";
        int a;
        cin>>a;
        Enqueue(Q,a);
    }
    cout<<"show the queue:"<<endl;
    Showqueue(Q);
    cout<<"\n";
    int f;
    Dequeue(Q,f);
    cout<<"the head of the queue:"<<f;
    cout<<"input the number enter queue:";
    int l;
    cin>>l;
    Enqueue(Q,l);
    Showqueue(Q);
    return 0;
}

4.tree

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
using namespace std;

typedef struct Treenode
{
    int data;
    struct Treenode*lchild;
    struct Treenode*rchild;
}*Tree,Treenode;

void Creattree(Tree &T)
{
    T=NULL;
}

bool Searchtree(Tree T,int x,Tree &p,Tree &q)
{
    p=T;
    while(p)
    {
        if(x==p->data)
        {
            return true;
          }
        else if(x<p->data)
        {
            q=p;
            p=p->lchild;
        }
        else
        {
            q=p;p=p->rchild;
        }
    }
    return false;
}

bool Insert(Tree &T,int x)
{
    Tree m,n;
  if(Searchtree(T,x,m,n))
    return false;
  else
  {
      Treenode *s=new Treenode;
      s->data=x;
      s->lchild=NULL;
      s->rchild=NULL;
      if(!T)T=s;
      else if(x<n->data)n->lchild=s;
      else n->rchild=s;
      return true;
  }
}

void Deletree(Tree &T,int x)
{
    Tree m,n;
    if(Searchtree(T,x,m,n))
    {
        if(m->data<n->data)
        {
            if(m->rchild==NULL)
        {
            n->lchild=m->lchild;
            delete(m);
        }
        else
        {
            n->lchild=m->rchild;
            delete(m);
        }
        }
        else
        {
            if(m->rchild==NULL)
        {
            n->rchild=m->lchild;
            delete(m);
        }
            else
        {
            n->rchild=m->rchild;
            delete(m);
        }
        }

    }
    else
        cout<<"can't find";
}
void Showtree1(Tree T)//中序遍历
{
    if(T){
    Showtree1(T->lchild);
    cout<<T->data<<"    ";
    Showtree1(T->rchild);
    }

}
void Showtree2(Tree T)//先序
{
    if(T)
    {
        cout<<T->data<<"    ";
        Showtree2(T->lchild);
        Showtree2(T->rchild);
    }
}
void Showtree3(Tree T)//后序
{
    if(T)
    {
        Showtree3(T->lchild);
        Showtree3(T->rchild);
        cout<<T->data<<"    ";
    }
}
int main()
{
    Tree T;
    Creattree(T);
    cout<<"how many numbers?";
    int n;
    cin>>n;

    for(int i=0;i<n;i++)
    {
        cout<<"input the numbers:";
        int a;
        cin>>a;
        Insert(T,a);
    }
    cout<<"中序遍历:"<<endl;
    Showtree1(T);
    cout<<"\n"<<"先序遍历:"<<endl;
    Showtree2(T);
    cout<<"\n"<<"后序遍历:"<<endl;
    Showtree3(T);
    cout<<"\n";
    cout<<"input the number you want to insert:";
    int q;
    cin>>q;
    Insert(T,q);
    cout<<"中序遍历:"<<endl;
    Showtree1(T);
    cout<<"\n"<<"先序遍历:"<<endl;
    Showtree2(T);
    cout<<"\n"<<"后序遍历:"<<endl;
    Showtree3(T);
    cout<<"\n";
    cout<<"input the number you want to delete:";
    int z;
    cin>>z;
    Deletree(T,z);
    cout<<"中序遍历:"<<endl;
    Showtree1(T);
    cout<<"\n"<<"先序遍历:"<<endl;
    Showtree2(T);
    cout<<"\n"<<"后序遍历:"<<endl;
    Showtree3(T);
    return 0;
}

5.graph

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>

using namespace std;

#define MAX 20
typedef struct ArcCell
{
    int adj;
}Adj[MAX][MAX],ArcCell;
struct Graph
{
    char vex[MAX];//顶点数组
    bool visited[MAX];//是否访问标志
    Adj arcs;//邻接矩阵
    int vexnum,arcnum;//顶点和边的数量
};

int Locate(char a[],char x)
{
    for(int i=0;;i++)
        if(x==a[i])
        return i;
    return -1;
}
void CreatGraph(Graph &g)
{
    cout<<"how many vex:";
    cin>>g.vexnum;
    cout<<"how many arc:";
    cin>>g.arcnum;
    for(int i=0;i<g.vexnum;i++)
    {
        cout<<"input the vex:";
        cin>>g.vex[i];
        g.visited[i]=false;
    }
    for(int i=0;i<g.vexnum;i++)
        for(int j=0;j<g.vexnum;j++)
        g.arcs[i][j].adj=0;
    for(int j=0;j<g.arcnum;j++)
    {
        cout<<"input the arc:";
        char v1,v2;
        cin>>v1>>v2;
        int m,n;
        m=Locate(g.vex,v1);
        n=Locate(g.vex,v2);
        int d;
        cout<<"input the info";
        cin>>d;
        g.arcs[m][n].adj=g.arcs[n][m].adj=d;
    }
}

void Initgraph(Graph &g)
{
    g.arcnum=g.vexnum=0;
}

void Showgraph(Graph g)
{
    for(int i=0;i<g.vexnum;i++)
    {
        for(int j=0;j<g.vexnum;j++)
        cout<<g.arcs[i][j].adj<<"  ";
        cout<<"\n";
    }
}

//深度优先遍历
void DFS(Graph &g,int v)
{
    g.visited[v]=true;
    cout<<"visit->"<<g.vex[v]<<"->";
    for(int i=0;i<g.vexnum;i++)
        if(g.arcs[v][i].adj!=0 && !g.visited[i])
        DFS(g,i);
}
void DFStraverse(Graph g)
{
    for(int v=0;v<g.vexnum;v++)
        if(!g.visited[v])
        DFS(g,v);
}

//广度优先遍历
struct Queuenode
{
    int data;
    struct Queuenode *next;
};
struct Queue
{
    Queuenode *front;
    Queuenode *rear;
    int num;
};
void Initqueue(struct Queue &Q,int n)
{
    Q.front=Q.rear=new Queuenode;
    Q.front->next=NULL;
    Q.num=0;
}
void Enqueue(struct Queue &Q,int x)
{
    Queuenode *p=new Queuenode;
    p->data=x;
    p->next=NULL;
    Q.rear->next=p;
    Q.rear=p;
    Q.num++;
}
bool Dequeue(struct Queue &Q,int &e)
{
    if(Q.front==Q.rear)
        return false;
    else
    {
        Queuenode *p=Q.front->next;
        e=p->data;
        Q.front->next=p->next;
        if(Q.rear==p)
            Q.rear=Q.front;
        delete(p);
        Q.num--;
        return true;
    }
}
void BFS(Graph &g)
{
    Queue Q;
    Initqueue(Q,g.vexnum);
    for(int i=0;i<g.vexnum;i++)
        if(!g.visited[i])
    {
        g.visited[i]=true;
        cout<<"visit->"<<g.vex[i]<<"->";
        Enqueue(Q,i);
        while(!Q.num){
                int e;
            Dequeue(Q,e);
        for(int j=0;j<g.vexnum;j++)
            if(!g.visited[j] && g.arcs[i][j].adj)
        {
            g.visited[j]=true;
            cout<<"visited->"<<g.vex[j]<<"->";
            Enqueue(Q,j);
        }
        }
    }
}
int main()
{
    Graph g;
    Initgraph(g);
    CreatGraph(g);
    Showgraph(g);
    cout<<"DFS:"<<endl;
    DFStraverse(g);
    cout<<"\n";
    cout<<"BFS:"<<endl;
    BFS(g);
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,660评论 0 33
  • Exercise 3 方法:打开终端运行make qemu-gdb,再打开另一个终端运行make gdb,通过b ...
    找不到工作阅读 9,833评论 0 6
  • Introduction 该 lab 主要需要编写操作系统的内存管理部分。内存管理分为两个部分: 内核的物理内存分...
    找不到工作阅读 11,973评论 0 12
  • 你说为啥不能正常点? 对啊,因为我太喜欢你 我怎么做到正常跟你相处 好多话,我不说,你不会懂 我说了,你也不会懂 ...
    卓玛是个大叔控阅读 178评论 0 0
  • 2017年10月8日11时许,献县中医院急诊科传来一阵急促的脚步声,“大夫救命啊!”献县中医院急诊科医护人...
    相偎相依阅读 708评论 0 0