向未来的约定brig...吧 关注:25贴子:482

【闪亮~『约定』~闪爱】寒假数据结构学习

只看楼主收藏回复

寒假数据结构学习,从皮毛开始
本帖子留作记录,方便写科研日志


IP属地:广东1楼2015-01-20 18:19回复
    线性表的创建,插入与删除(⊙o⊙)哦
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #define MaxSize 10
    typedef struct{
    int *elem;
    int len;
    int listsize;
    }Sqlist;
    void InitSqlist(Sqlist *L)
    {
    L->elem=(int*)malloc(MaxSize*sizeof(int));
    if(!L->elem)
    exit(0);
    L->len=0;
    L->listsize=MaxSize;
    }
    void InsertItem(Sqlist *L,int i,int item)
    {
    int *base,*insertPtr,*p;
    if(i<1||i>L->len+1)exit(0);
    if(L->len>=L->listsize)
    {
    base=(int*)realloc(L->elem,(L->listsize+10)*sizeof(int));
    L->elem=base;
    L->listsize=L->listsize+10;
    }
    insertPtr=&(L->elem[i-1]);
    p=&(L->elem[L->len-1]);
    for(;p>=insertPtr;p--)
    *(p+1)=*p;
    *insertPtr=item;
    L->len++;
    }
    void DelItem(Sqlist *L,int i)
    {
    int *delItem,*p;
    if(i<1||i>L->len)exit(0);
    delItem=&(L->elem[i-1]);
    p=L->elem+L->len-1;
    for(++delItem;delItem<=p;++delItem)
    *(delItem-1)=*delItem;
    L->len--;
    }
    int main()
    {
    Sqlist L;
    int i;
    int index;
    int n;
    int num;
    InitSqlist(&L);
    printf("请输入线性表中元素的个数:\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    printf("第%d个元素的值为:",i+1);
    scanf("%d",&num);
    InsertItem(&L,i+1,num);
    }
    printf("输出:");
    for(i=0;i<n;i++)
    printf("%d ",L.elem[i]);
    putchar('\n');
    printf("请输入你要插入的位置及元素:\n");
    scanf("%d %d",&index,&num);
    InsertItem(&L,index,num);
    printf("输出:\n");
    for(i=0;i<L.len;i++)
    printf("%d ",L.elem[i]);
    putchar('\n');
    printf("请输入你要删除的位置:\n");
    scanf("%d",&index);
    DelItem(&L,index);
    printf("输出:\n");
    for(i=0;i<L.len;i++)
    printf("%d ",L.elem[i]);
    putchar('\n');
    getch();
    }


    IP属地:广东2楼2015-01-20 18:20
    回复
      2025-08-07 15:47:10
      广告
      不感兴趣
      开通SVIP免广告
      二叉树的创建,遍历二叉树,访问二叉树,弄n久才勉强懂~
      #include<stdio.h>
      #include<conio.h>
      #include<malloc.h>
      typedef struct BiTNode{
      char data;
      struct BiTNode *lchild,*rchild;
      }BiTNode,*BiTree;
      void visit(char c,int level)
      {
      printf("%c位于二叉树的%d层.\n",c,level);
      }
      void CreatBitree(BiTree *T)
      {
      char c;
      scanf("%c",&c);
      if(c==' ')
      *T=NULL;
      else
      {
      *T=(BiTNode*)malloc(sizeof(BiTNode));
      (*T)->data=c;
      CreatBitree(&((*T)->lchild));
      CreatBitree(&((*T)->rchild));
      }
      }
      void PreOrderTraverse(BiTree T,int level)
      {
      if(T)
      {
      visit(T->data,level);
      PreOrderTraverse(T->lchild,level+1);
      PreOrderTraverse(T->rchild,level+1);
      }
      }
      int main()
      {
      BiTree T=NULL;
      int level=1;
      CreatBitree(&T);
      PreOrderTraverse(T,level);
      getch();
      }


      IP属地:广东3楼2015-01-22 21:52
      回复
        是不是得添加一点离散数学的内容~~~


        IP属地:广东4楼2015-01-22 21:54
        回复
          昨天还在看二叉树,想了想还是把前面的基础打牢吧,今天先完成栈的第一部分内容,还是出了不少问题,看了n久~竟然错在构造空栈~~
          #include<stdio.h>
          #include<conio.h>
          #include<math.h>
          #include<stdlib.h>
          #define STACK_INIT_SIZE 10
          #define STACKINCREMENT 10
          typedef struct{
          char *base;
          char *top;
          int stacksize;
          }SqStack;
          void initSqStack(SqStack *s)
          {
          s->base=(char*)malloc(STACK_INIT_SIZE*sizeof(char));//居然在这里犯错误,两次了!!
          if(!s->base)
          exit(0);
          s->top=s->base;
          s->stacksize=STACK_INIT_SIZE;
          }
          void Push(SqStack *s,char e)
          {
          if(s->top-s->base>=s->stacksize)
          {
          s->base=(char*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(char));
          if(!s->base)
          exit(0);
          s->top=s->base+s->stacksize;
          s->stacksize=s->stacksize+STACKINCREMENT;
          }
          *(s->top++)=e;
          }
          void Pop(SqStack *s,char *e)
          {
          if(s->base==s->top)
          return;
          *e=*--(s->top);
          }
          int Stacklen(SqStack *s)
          {
          return (s->top-s->base);
          }
          int main()
          {
          SqStack s;
          int len;
          char c;
          int i;
          int sum=0;
          initSqStack(&s);
          printf("入栈操作:请依次输入入栈的元素(01字串):");
          scanf("%c",&c);
          while(c!='#')
          {
          Push(&s,c);
          scanf("%c",&c);
          }
          getchar();//仅仅是在这个地方回车了,注意吸收掉!
          len=Stacklen(&s);
          printf("栈的当前容量为:%d\n",len);
          printf("出栈操作:");
          for(i=0;i<len;i++)
          {
          Pop(&s,&c);
          printf("%c",c);
          sum+=(c-'0')*(int)(pow(2,i));
          }
          printf("\n栈中的01字串转化为十进制的结果:%d.\n",sum);
          getch();
          }


          IP属地:广东5楼2015-01-23 22:29
          回复


            来自Android客户端7楼2015-01-25 10:53
            收起回复
              这学霸模式好可怕


              来自iPhone客户端10楼2015-01-27 22:16
              收起回复
                我同学也学c语言


                来自iPhone客户端11楼2015-01-27 22:16
                收起回复
                  2025-08-07 15:41:10
                  广告
                  不感兴趣
                  开通SVIP免广告
                  队列的创建,入队,出队,销毁队列,不多说~
                  #include<stdio.h>
                  #include<conio.h>
                  #include<stdlib.h>
                  typedef struct QNode{
                  char data;
                  struct QNode *next;
                  }QNode,*QueuePtr;//链队列的结点类
                  typedef struct{
                  QueuePtr front;
                  QueuePtr rear;
                  }LinkQueue;//链队列类
                  void InitQueue(LinkQueue *q)
                  {
                  q->front=q->rear=(QueuePtr)malloc(sizeof(QNode));
                  if(!q->front)
                  exit(0);
                  q->front->next=NULL;
                  }
                  void InsertQueue(LinkQueue *q,char e)
                  {
                  QueuePtr p;
                  p=(QueuePtr)malloc(sizeof(QNode));
                  if(p==NULL)
                  exit(0);
                  p->data=e;
                  p->next=NULL;
                  q->rear->next=p;
                  q->rear=p;
                  }
                  void DeQueue(LinkQueue *q,char *e)
                  {
                  QueuePtr p;
                  if(q->front==q->rear)
                  return;
                  p=q->front->next;
                  *e=p->data;
                  q->front->next=p->next;
                  if(q->rear==p)
                  q->rear=q->front;
                  free(p);
                  }
                  void DestroyQueue(LinkQueue *q)
                  {
                  while(q->front)
                  {
                  q->rear=q->front->next;
                  free(q->front);
                  q->front=q->rear;
                  }
                  }
                  int main()
                  {
                  char c;
                  int count=0,i;
                  LinkQueue q;
                  InitQueue(&q);
                  printf("请输入进入队列的元素,以#作为结束标志:");
                  scanf("%c",&c);
                  while(c!='#')
                  {
                  InsertQueue(&q,c);
                  count++;
                  scanf("%c",&c);
                  }
                  getchar();
                  printf("队列中的元素依次出队列:");
                  for(i=0;i<count;i++)
                  {
                  DeQueue(&q,&c);
                  printf("%c",c);
                  }
                  getch();
                  return 0;
                  }


                  IP属地:广东12楼2015-01-28 18:12
                  回复
                    居然在初始化之后妄图对数组整体赋值,醉了~~图的邻接矩阵表示
                    #include<stdio.h>
                    #include<conio.h>
                    #define MaxVertexNum 100
                    typedef char VertexType;
                    typedef int EdgeType;
                    typedef struct Graph{
                    VertexType Vertex[MaxVertexNum];
                    EdgeType Edge[MaxVertexNum][MaxVertexNum];
                    int vexnum;
                    int arcnum;
                    }Graph;
                    void CreatGraph(Graph *G)
                    {
                    int i;
                    int j;
                    int k;
                    printf("请输入将要创建的无向图的顶点数和边数:");
                    scanf("%d %d",&G->vexnum,&G->arcnum);
                    getchar();//不要忘记,不然总是少输出一个字符
                    printf("请输入将要创建的无向图的顶点信息:");
                    for(i=0;i<G->vexnum;i++)
                    {
                    scanf("%c",&G->Vertex[i]);
                    }
                    for(i=0;i<G->vexnum;i++)
                    for(j=0;j<G->vexnum;j++)
                    {
                    G->Edge[i][j]=0;
                    }
                    printf("请输入连接的两顶点的下标:\n");
                    for(k=0;k<(2*G->arcnum);k++)
                    {
                    scanf("%d %d",&i,&j);
                    G->Edge[i][j]=1;
                    }
                    }
                    void PrintGraph(Graph *G)
                    {
                    int i,j;
                    printf("输出无向图的顶点:");
                    for(i=0;i<G->vexnum;i++)
                    {
                    printf("%c",G->Vertex[i]);
                    }
                    printf("\n");
                    printf("输出无向图的邻接矩阵:\n");
                    for(i=0;i<G->vexnum;i++)
                    {
                    for(j=0;j<G->vexnum;j++)
                    {
                    printf("%d ",G->Edge[i][j]);
                    }
                    printf("\n");
                    }
                    }
                    int main()
                    {
                    Graph G;
                    CreatGraph(&G);
                    PrintGraph(&G);
                    getch();
                    return 0;
                    }


                    IP属地:广东14楼2015-02-13 22:18
                    回复
                      你在干什么…


                      IP属地:浙江来自Android客户端15楼2015-02-16 00:04
                      收起回复
                        图的邻接表存储,花了两天来看。。。
                        #include<stdio.h>
                        #include<conio.h>
                        #include<stdlib.h>
                        #define MaxVertexNum 100
                        typedef int EdgeType;
                        typedef char VertexType;
                        typedef struct EdgeNode{
                        EdgeType adjvex;
                        struct EdgeNode *nextarc;
                        }EdgeNode;
                        typedef struct VertexNode{
                        VertexType data;
                        EdgeNode *firstedge;
                        }VertexNode;
                        typedef VertexNode Adjlist[MaxVertexNum];
                        typedef struct AlGraph{
                        Adjlist adjlist;
                        int n;
                        int e;
                        }AlGraph;
                        void CreatGraph(AlGraph *G)
                        {
                        int i,j,k;
                        EdgeNode *s;
                        printf("请输入图的顶点数和边数:");
                        scanf("%d %d",&G->n,&G->e);
                        getchar();
                        printf("请输入图的各顶点信息:");
                        for(i=0;i<G->n;++i)
                        {
                        scanf("%c",&G->adjlist[i].data);
                        G->adjlist[i].firstedge=NULL;
                        }
                        for(k=0;k<G->e;++k)
                        {
                        printf("请输入用于表示边的顶点对(vi-vj):");
                        scanf("%d %d",&i,&j);
                        s=(EdgeNode*)malloc(sizeof(EdgeNode));
                        s->adjvex=j;
                        s->nextarc=G->adjlist[i].firstedge;
                        G->adjlist[i].firstedge=s;
                        s=(EdgeNode*)malloc(sizeof(EdgeNode));
                        s->adjvex=i;
                        s->nextarc=G->adjlist[j].firstedge;
                        G->adjlist[j].firstedge=s;
                        }
                        }
                        int main()
                        {
                        AlGraph G;
                        int i;
                        CreatGraph(&G);
                        printf("图的邻接表存储如下图:\n");
                        for(i=0;i<G.n;++i)
                        {
                        printf("%c->",G.adjlist[i].data);
                        while(G.adjlist[i].firstedge)
                        {
                        printf("%d->",G.adjlist[i].firstedge->adjvex);
                        G.adjlist[i].firstedge=G.adjlist[i].firstedge->nextarc;
                        }
                        printf("^\n");
                        }
                        getch();
                        return 0;
                        }


                        IP属地:广东16楼2015-02-16 21:49
                        回复
                          图的存储之十字链表,好难。。。。
                          #include<stdio.h>
                          #include<conio.h>
                          #include<stdlib.h>
                          #define MaxVertexNum 100
                          typedef char VertexType;
                          typedef int ArcType;
                          typedef struct ArcBox{
                          ArcType tailvex;
                          ArcType headvex;
                          struct ArcBox *tlink;
                          struct ArcBox *hlink;
                          }ArcBox;
                          typedef struct VertexNode{
                          VertexType data;
                          ArcBox *firstin;
                          ArcBox *firstout;
                          }VertexNode;
                          typedef VertexNode OLlist[MaxVertexNum];
                          typedef struct OlGraph{
                          OLlist oLlist;
                          int arcnum;
                          int vexnum;
                          }OlGraph;
                          int LocateVertex(OlGraph *G,VertexType v)
                          {
                          int i,k;
                          for(i=0;i<G->vexnum;++i)
                          {
                          if(G->oLlist[i].data==v)
                          {
                          k=i;
                          break;
                          }
                          }
                          return k;
                          }
                          void CreatOlGraph(OlGraph *G)
                          {
                          int i,j,k;
                          VertexType vh,vt;
                          ArcBox *p;
                          printf("请输入图的顶点数和弧数:");
                          scanf("%d %d",&G->vexnum,&G->arcnum);
                          getchar();
                          printf("请输入图的顶点信息:");
                          for(i=0;i<G->vexnum;++i)
                          {
                          scanf("%c",&G->oLlist[i].data);
                          G->oLlist[i].firstin=NULL;
                          G->oLlist[i].firstout=NULL;
                          }
                          getchar();
                          for(k=0;k<G->arcnum;++k)
                          {
                          printf("请输入图中的每一条弧的弧尾和弧头的信息:");
                          scanf("%c %c",&vt,&vh);
                          getchar();
                          i=LocateVertex(G,vt);
                          j=LocateVertex(G,vh);
                          p=(ArcBox *)malloc(sizeof(ArcBox));
                          if(!p)
                          exit(0);
                          p->tailvex=i;
                          p->headvex=j;
                          p->tlink=G->oLlist[i].firstout;
                          G->oLlist[i].firstout=p;
                          p->hlink=G->oLlist[j].firstin;
                          G->oLlist[j].firstin=p;
                          }
                          }
                          int main()
                          {
                          OlGraph G;
                          int i;
                          CreatOlGraph(&G);
                          printf("输出图:\n顶点的出度:\n");
                          for(i=0;i<G.vexnum;++i)
                          {
                          printf("vertex%c:",G.oLlist[i].data);
                          while(G.oLlist[i].firstout)
                          {
                          printf("<%d %d>",G.oLlist[i].firstout->tailvex,G.oLlist[i].firstout->headvex);
                          G.oLlist[i].firstout=G.oLlist[i].firstout->tlink;
                          }
                          printf("\n");
                          }
                          printf("顶点的入度:\n");
                          for(i=0;i<G.vexnum;++i)
                          {
                          printf("vertex%c:",G.oLlist[i].data);
                          while(G.oLlist[i].firstin)
                          {
                          printf("<%d %d>",G.oLlist[i].firstin->tailvex,G.oLlist[i].firstin->headvex);
                          G.oLlist[i].firstin=G.oLlist[i].firstin->hlink;
                          }
                          printf("\n");
                          }
                          return 0;
                          }


                          IP属地:广东17楼2015-02-18 14:25
                          回复
                            回学校后第一段代码,图(邻接矩阵)的深度优先遍历无视文件名属性。。。
                            #include<stdio.h>
                            #include<conio.h>
                            #define MaxVertexNum 50
                            typedef int VertexType;
                            typedef int ArcType;
                            int visit[MaxVertexNum]={0};
                            typedef struct Graph{
                            VertexType Vertex[MaxVertexNum];
                            ArcType Arc[MaxVertexNum][MaxVertexNum];
                            int n,e;
                            }Graph;
                            void CreatGraph(Graph *G)
                            {
                            int i,j,k;
                            printf("请输入有向图的顶点数和弧数:");
                            scanf("%d %d",&G->n,&G->e);
                            for(i=0;i<G->n;++i)
                            for(j=0;j<G->n;++j)
                            G->Arc[i][j]=0;
                            printf("请输入有向图的顶点信息:");
                            for(i=0;i<G->n;++i)
                            scanf("%d",&G->Vertex[i]);
                            for(k=0;k<G->e;++k)
                            {
                            printf("请输入有向图的第%d条弧(顶点对):",k);
                            scanf("%d %d",&i,&j);
                            G->Arc[i][j]=1;
                            }
                            }
                            void DFS_visit(Graph *G,int u)
                            {
                            int v;
                            visit[u]=1;
                            printf("%d ",G->Vertex[u]);
                            for(v=0;v<G->n;++v)
                            {
                            if(G->Arc[u][v]==1&&visit[v]!=1)//访问过第u个顶点之后在第u个和第v个顶点之间有边的情况下
                            DFS_visit(G,v); //若第v个顶点没访问过,继续深度优先遍历的第v个顶点。
                            }
                            }
                            void DFS(Graph *G)
                            {
                            int u;
                            for(u=0;u<G->n;u++)
                            {
                            if(visit[u]!=1)
                            DFS_visit(G,u);
                            }
                            }
                            int main()
                            {
                            Graph G;
                            CreatGraph(&G);
                            printf("深度优先遍历的结果为:\n");
                            DFS(&G);
                            getch();
                            return 0;
                            }


                            IP属地:广东18楼2015-02-28 20:34
                            回复
                              2025-08-07 15:35:10
                              广告
                              不感兴趣
                              开通SVIP免广告
                              图(邻接表)的深度优先遍历
                              #include<stdio.h>
                              #include<conio.h>
                              #include<stdlib.h>
                              #define MaxVertexNum 50
                              typedef int VertexType;
                              typedef int ArcType;
                              int visited[MaxVertexNum]={0};
                              typedef struct ArcNode{
                              int adjvex;
                              struct ArcNode *nextarc;
                              }ArcNode;
                              typedef struct VertexNode{
                              VertexType data;
                              ArcNode *firstarc;
                              }VertexNode;
                              typedef VertexNode Adjlist[MaxVertexNum];
                              typedef struct AlGraph{
                              Adjlist adjlist;
                              int vexnum;
                              int arcnum;
                              }AlGraph;
                              int LocateVertex(AlGraph *G,VertexType v)
                              {
                              int i,k;
                              for(i=0;i<G->vexnum;++i)
                              {
                              if(G->adjlist[i].data==v)
                              {
                              k=i;
                              break;
                              }
                              }
                              return k;
                              }
                              void CreatGraph(AlGraph *G)
                              {
                              int i,j,k;
                              int v1,v2;
                              ArcNode *p;
                              printf("请输入有向图的顶点数和边数:");
                              scanf("%d %d",&G->vexnum,&G->arcnum);
                              printf("请输入有向图的顶点信息:");
                              for(i=0;i<G->vexnum;++i)
                              {
                              scanf("%d",&G->adjlist[i].data);
                              G->adjlist[i].firstarc=NULL;
                              }
                              for(k=0;k<G->arcnum;++k)
                              {
                              printf("请输入有向图的第%d条弧(顶点对):",k+1);
                              scanf("%d %d",&v1,&v2);
                              i=LocateVertex(G,v1);
                              j=LocateVertex(G,v2);
                              p=(ArcNode *)malloc(sizeof(ArcNode));
                              if(!p)
                              exit(0);
                              p->adjvex=j;
                              p->nextarc=G->adjlist[i].firstarc;
                              G->adjlist[i].firstarc=p;
                              }
                              }
                              void DFS_visit(AlGraph *G,int u)
                              {
                              ArcNode *p;
                              visited[u]=1;
                              printf("%d ",G->adjlist[u].data);
                              p=G->adjlist[u].firstarc;
                              while(p)
                              {
                              if(visited[p->adjvex]==0)//此处理解很重要
                              DFS_visit(G,p->adjvex);
                              p=p->nextarc;
                              }
                              }
                              void DFS(AlGraph *G)
                              {
                              int u;
                              for(u=0;u<G->vexnum;++u)
                              if(!visited[u])
                              DFS_visit(G,u);
                              }
                              int main()
                              {
                              AlGraph G;
                              CreatGraph(&G);
                              printf("\n图的深度优先遍历的结果为:\n");
                              DFS(&G);
                              getch();
                              return 0;
                              }


                              IP属地:广东19楼2015-02-28 22:40
                              回复