【无标题】1

#include<stdio.h>

#include<stdlib.h>

#define OK 1

#define ERROR 0

typedef int status;

typedef int elemtype;

typedef struct node

{

       int data;

       struct node *next;

}node,*linklist;

status initlist(linklist l)

{//空的单链表

       l=new node;

       l->next=NULL;

       return OK;

}

//头插法

linklist creat(linklist l)

{

       int n;

       linklist p;

       l=(linklist)malloc(sizeof(node));

       l->next=NULL;

       printf("\n请输入线性表中各个元素,以0结束:\n");

       scanf("%d",&n);

       while(n!=0)

       {

              p=(linklist)malloc(sizeof(node));

              p->data=n;

              p->next=l->next;

              l->next=p;

              scanf("%d",&n);

       }

       return l;

}

//查找

int  insert(linklist l,int e)

{

       linklist p;

       p=new node;

       int i=1;

       p=l->next;

       while(p&&(p->data!=e))

       {

              p=p->next;

              i++;

       }

       return i;

}

//插入

status listinsert(linklist &l,int i,elemtype e)

{

       linklist p,s;

       p=new node;

       p=l;

       int j=0;

       while(p&&(j<i-1))

       {

              p=p->next;

              ++j;

       }

       if(!p||j>i+1)

              return ERROR;

       s=new node;

       s->data=e;

       s->next=p->next;

       p->next=s;

       return OK;

}

//删除

status listdelete(linklist &l,int i)

{

       linklist p,q;

       p=new node;

       p=l;

       int j=0;

       while((p->next)&&(j<i-1))

       {

              p=p->next;

              ++j;

       }

       if(!(p->next)||(j>i-1))

              return ERROR;

       q=p->next;

       p->next=q->next;

       delete q;

       return OK;

}

void printf(linklist &l)

{

       linklist p;

       p=l->next;

       while(p)

       {

              printf("%d ",p->data);

              p=p->next;

       }

       printf("\n");

}

int main()

{

       linklist l=NULL;

       int x,y,a,b,i;

       l=creat(l);

       printf("这个单链表是:\n");

       printf(l);

       printf("请求插入的位置\n");

       scanf("%d %d",&i,&x);

       listinsert(l,i,x);

       printf(l);

       printf("请求删除的位置\n");

       scanf("%d",&y);

       listdelete(l,y);

       printf(l);

       printf("请求查找的元素\n");

       scanf("%d",&b);

       a=insert(l,b);

       if(a)

       {

              printf("第%d位置元素是 %d\n",a,b);

       }

       else

       {

              printf("第%d位置的元素不存在\n");

       }

       return 0;

}

#include<stdio.h>

#include<stdlib.h>

#define true 1

#define false 0

typedef int elemtype;

typedef struct stacknode

{

       elemtype data;

       struct stacknode *next;

}linkstacknode,*linkstack;

//头插法创建

linkstack creat()

{

       //0结尾

       linkstack top=(linkstack)malloc(sizeof(linkstacknode));

       top->next=NULL;

       linkstacknode *s;

       int x;

       scanf("%d",&x);

       while(x!=0)

       {

              s=(linkstack)malloc(sizeof(linkstacknode));

              s->data=x;

              s->next=top->next;

              top->next=s;

              scanf("%d",&x);

       }

       return top;

}

//入栈

int push(linkstack top,elemtype x)

{

       linkstack p=(linkstack)malloc(sizeof(linkstacknode));

       if(p==NULL)

              return false;

       p->data=x;

       p->next=top->next;

       top->next=p;

       return true;

}

//出栈

int pop(linkstack top)

{

       linkstack p;

       elemtype x;

       if(top->next==NULL)

              return NULL;//空栈

       p=top->next;

       top->next=p->next;

       x=p->data;

       free(p);

       return x;

}

void print(linkstack top)

{

       linkstack p=top->next;

       while(p!=NULL)

       {

              printf("%d ",p->data);

              p=p->next;

       }

       printf("\n");

}

int main()

{

       linkstack top;

       elemtype x;

       top=creat();//创建栈

       print(top);

//x

       printf("\n入栈的元素:");

       scanf("%d",&x);

       push(top,x);

       printf("\n入栈后元素为:");

       print(top);

//y

       x=pop(top);

       printf("\n出栈后栈内元素为:");

       print(top);

       printf("\n");

       return 0;

//q

}

#include<stdio.h>

#define ok 1

#define Error 0

#define N 100

typedef struct

{

    int a[N];

    int last;

}seqList;

int locate(seqList L,int e){

    int i=0;

    while((i<=L.last)&&(L.a[i]!=e)) i++;

    if(i<=L.last) return (i+1);

    else return (-1);

}

int inslist(seqList *L,int i,int e){

    int k;

    if(i<1||i>L->last+2){

        printf("插入位置不合法");

        return Error;

    }

    if(L->last>=N-1){

        printf("表已满,无法插入");

        return Error;

    }

    for(k=L->last;k>=i-1;k--){

        L->a[k+1]=L->a[k];

    }

    L->a[i-1]=e;

    L->last++;

    return ok;

}

int dellist(seqList *L,int i,int *e){

    int k;

    if((i<1)||(i>L->last+1)){

        printf("删除位置不合法");

        return Error;

    }

    *e=L->a[i-1];

    for(k=i;k<=L->last;k++)

        L->a[k-1]=L->a[k];

    L->last--;

    return ok;

}

int main()

{

    int n,i;

    seqList m;

    int k;

    scanf("%d",&n);

    for(i=0;i<n;i++){

        scanf("%d",&m.a[i]);

        m.last++;

    }

    printf("输入查找值:");

    scanf("%d",&k);

    printf("%d\n",locate(m,k));

    printf("输入插入的值:");

    int s,w;

    scanf("%d %d",&s,&w);

    inslist(&m,s,w);

    for(i=0;i<n+1;i++)

    {

        printf("%d ",m.a[i]);

    }

    printf("\n");

    printf("删除输入:");

    int e,q;

    scanf("%d %d",&e,&q);

    dellist(&m,e,&q);

    for(i=0;i<n;i++){

    //dellist(&m,e,&q);

        printf("%d ",m.a[i]);

    }

   // printf("\n");

    return 0;

}

#include<stdio.h>

#define max 100

typedef struct SqStack

{

       int data[max];

       int top;

}SqStack;

//栈空

 bool stackempty(SqStack s)

{

       if(s.top==1)

              return false;

       else

              return true;

}

//初始化栈

void initstack(SqStack &s)

{

       s.top=-1;

}

//进栈

bool push(SqStack &s,int x)

{

       if(s.top==max-1)

              return false;

       s.data[++s.top]=x;

       return true;

}

//出栈

bool pop(SqStack &s,int &x)

{

       if(s.top==-1)

              return false;

       else

       {

              x=s.data[s.top--];

              return true;

       }

}

//读取栈顶元素

int gettop(SqStack s)

{

       if(s.top==1)

              return false;

       int x=s.data[s.top];

       return x;

}

//清空栈

bool clearstack(SqStack &s)

{

       if(s.top==-1)

              return false;

       s.top=-1;

       return true;

}

//输出栈

bool print(SqStack s)

{

       if(s.top==-1)

       {

              printf("栈空");

       return false;

       }

       for(int i=0;i<s.top;i++)

       {

              printf(" %d\n",s.data[s.top-i]);

       }

       return true;

}

int main()

{

       SqStack s;

       initstack(s);

       int c;

       int p,x;

       printf("\n--------------\n");

       printf("请选择功能:\n1:入栈2:出栈 3:判空 4:读取栈顶元 5:遍历 6:清空栈 \n");

       scanf("%d",&c);

       while(c==0||c==1||c==2||c==3||c==4||c==5||c==6)

       {

              switch(c)

              {

              case 0:

                     scanf("%d",&c);continue;

              case 1:

                     printf("请输入入栈数: ");

                     scanf("%d",&p);

                     push(s,p);

                            printf("入栈后为: \n");

                            print(s);

                     c=0;

                     break;

              case 2:

                     pop(s,x);

                     printf("将栈顶元素%d出栈\n",x);

                     printf("出栈后为 \n");

                            print(s);

                     x=NULL;

                     c=0;

                     break;

              case 3:

                     if(stackempty(s))

                            printf("栈空\n");

                     else

                            printf("栈不为空\n");

                            c=0;

                            break;

              case 4:

                     printf("栈顶元素为%d",gettop(s));

                            c=0;

                     break;

              case 5:

                     clearstack(s);

                     printf("清空栈\n");

                     c=0;

                     break;

              case 6:

                     print(s);

                     c=0;

                     break;

       }

       printf("请选择功能:\n1:入栈 2:出栈 3:是否空栈 4:读取栈顶元素 5:清空栈 6:遍历 \n");

}

return 0;

}

#include <stdio.h>

#define MAXSIZE 100

typedef struct {

       int n,e;                               

       char V[MAXSIZE];               

       int E[MAXSIZE][MAXSIZE];   

} Graph;

void InitGraph(Graph *G) {

       int i,j;

       for(i=0; i<G->n; i++)

              for(j=0; j<G->n; j++)

                     G->E[i][j]=0;

}

void CreateGraph(Graph *G) {

       int i,j,k;

       char ch1,ch2,X;

       printf("请输入图的顶点数目:");

       scanf("%d",&G->n);

       printf("请输入图的边的数目:");

       scanf("%d",&G->e);

       printf("请输入各顶点的信息:\n");

       for(i=0; i<G->n; i++) {

              scanf("%c",&X);

              printf("输入第%d个顶点:",i+1);

              scanf("%c",&(G->V[i]));

       }

       for(k=0; k<G->e; k++) {

              scanf("%c",&X);

              printf("建立第%d条边(逗号隔开):",k+1);

              scanf("%c,%c",&ch1,&ch2);

              for(i=0; i<G->n; i++)

                     for(j=0; j<G->n; j++)

                            if(ch1==G->V[i]&&ch2==G->V[j]) {

                                   G->E[i][j]=1;

                                   G->E[j][i]=1;

                            }

       }

}

void PrintGraph(Graph G) {

       int i,j;

       for(i=0; i<G.n; i++) {

              for(j=0; j<G.n; j++)

                     printf("%d  ",G.E[i][j]);

              printf("\n");

       }

}

int main() {

       Graph G;

       InitGraph(&G);                                 

       CreateGraph(&G);                     

       printf("图的邻接矩阵为:\n");

       PrintGraph(G);                          

}

#include <stdio.h>

#define MAXSIZE 100

typedef struct {

       int n,e;                               

       char V[MAXSIZE];               

       int E[MAXSIZE][MAXSIZE];   

} Graph;

void InitGraph(Graph *G) {

       int i,j;

       for(i=0; i<G->n; i++)

              for(j=0; j<G->n; j++)

                     G->E[i][j]=0;

}

void CreateGraph(Graph *G) {

       int i,j,k;

       char ch1,ch2,X;

       printf("请输入图的顶点数目:");

       scanf("%d",&G->n);

       printf("请输入图的边的数目:");

       scanf("%d",&G->e);

       printf("请输入各顶点的信息:\n");

       for(i=0; i<G->n; i++) {

              scanf("%c",&X);

              printf("输入第%d个顶点:",i+1);

              scanf("%c",&(G->V[i]));

       }

       for(k=0; k<G->e; k++) {

              scanf("%c",&X);

              printf("建立第%d条边(逗号隔开):",k+1);

              scanf("%c,%c",&ch1,&ch2);

              for(i=0; i<G->n; i++)

                     for(j=0; j<G->n; j++)

                            if(ch1==G->V[i]&&ch2==G->V[j]) {

                                   G->E[i][j]=1;

                                   //G->E[j][i]=1;

                            }

       }

}

void PrintGraph(Graph G) {

       int i,j;

       for(i=0; i<G.n; i++) {

              for(j=0; j<G.n; j++)

                     printf("%d  ",G.E[i][j]);

              printf("\n");

       }

}

int main() {

       Graph G;

       InitGraph(&G);                                 

       CreateGraph(&G);                     

       printf("图的邻接矩阵为:\n");

       PrintGraph(G);                                 

}

#include<stdio.h>

#include<malloc.h>

#define TRUE 1

#define FALSE 0

#define MAXSIZE 50

typedef struct

{

       int elem[MAXSIZE];

       int front;

       int rear; 

}SeqQueue;

void InitQueue(SeqQueue *Q)

{

       Q->front=Q->rear=0;

}

void CreateQueue(SeqQueue *Q)

{

       int c,flag=1;

       while(flag)

       {

              scanf("%d",&c);

              if(c!=0)

              {

                     Q->elem[Q->rear]=c;

                     Q->rear++;

              }

              else

              {

                     flag=0;

              }

       }

}

int EnterQueue(SeqQueue *Q,int x)

{

       if((Q->rear+1)%MAXSIZE==Q->front)

       return(FALSE);

       Q->elem[Q->rear]=x;

       Q->rear=(Q->rear+1)%MAXSIZE;

       return(TRUE);

}

int DeleteQueue(SeqQueue *Q,int *x)

{

       if(Q->front==Q->rear)

       return(FALSE);

       *x=Q->elem[Q->front];

       Q->front=(Q->front+1)%MAXSIZE;

       return(TRUE);

}

void Display(SeqQueue *Q)

{

       if(Q->front==Q->rear)

       printf("空队列!\n");

       else

       {

              int i;

              for(i=Q->front;i<Q->rear;i++)

              printf("%d ",Q->elem[i]);

              printf("\n");

       }

}

int main()

{

       int x;

       SeqQueue Q;

       InitQueue(&Q);

      

       printf("创建队列(以0结束):");

       CreateQueue(&Q);

       printf("创建的队列元素为:");

       Display(&Q);

      

       EnterQueue(&Q,9);

       printf("入队后队中的元素为:");

       Display(&Q);

      

       DeleteQueue(&Q,&x);

       printf("出队元素为:%d\n",x);

       printf("出队后队中元素为:");

       Display(&Q);

       return 0;

}

  • 22
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值