zoukankan      html  css  js  c++  java
  • 基本数据结构

    本篇文章中所有数据结构都是后期整理的,如有问题欢迎指正,转载请注明出处http://www.cnblogs.com/a1982467767/p/8893542.html 

      基础数据结构对应的头文件

     

    1.顺序表

      1 //linklist.h
      2 #include<stdio.h>
      3 #include<stdlib.h>
      4 #include<math.h>
      5 #include<string.h>
      6 #define MAXSIZE 100
      7 //顺序表结构体定义
      8 typedef struct node {
      9  int data[MAXSIZE];
     10  int length;
     11 }SeqList, *PSeqList;
     12 //函数申明区
     13 PSeqList Init_SeqList(void);//创建一个顺序表
     14 PSeqList Input_SeqList(PSeqList L, char a[]);//将数据输入到顺序表中
     15 int Length_SeqList(PSeqList L);//获得表长度
     16 int Location_SeqList(PSeqList L, int x);//查找,定位数据的位置
     17 int Insert_SeqList(PSeqList PL, int i, int x);//在顺序表中插入
     18 int Delete_SeqList(PSeqList PL, int i);//删除第i个元素
     19 void Output_SeqList(PSeqList PL);//输出顺序表
     20 //创建一个顺序表,入口参数无,返回一个指向顺序表的指针,指针值为零表示分配空间失败
     21 PSeqList Init_SeqList(void)
     22 {
     23  PSeqList PL;
     24  PL = (PSeqList)malloc(sizeof(SeqList));
     25  if (PL)
     26   PL->length = 0;
     27  else
     28  {
     29   printf("内存分配失败
    ");
     30   exit(-1);
     31  }
     32  return (PL);
     33 }
     34 //数据输入
     35 PSeqList Input_SeqList(PSeqList L, char a[])
     36 {
     37  int i, a_length = 0;
     38  a_length = strlen(a);
     39  for (i = 0; i < a_length; i++)
     40   L->data[i] = (int)a[i] - 48;//将char型整数转换成int型整数
     41  L->length = i;
     42  return L;
     43 }
     44 //读取顺序表的当前长度
     45 int Length_SeqList(PSeqList L)
     46 {
     47  if (!L)
     48  {
     49   printf("无PL指针,退出程序
    ");
     50   exit(-1);
     51  }
     52  return (L->length);
     53 }
     54 //在顺序表上检索,入口参数为顺序表,检索元素,返回元素位置,0表示查找失败
     55 int Location_SeqList(PSeqList L, int x)
     56 {
     57  int i = 0;
     58  while (i < L->length  &&  L->data[i] != x)
     59   i++;
     60  if (i >= L->length)
     61   return 0;
     62  else
     63   return (i + 1);
     64 }
     65 /*在顺序表的第i个元素之前插入x,入口参数为顺序表指针,插入位置,插入元素,
     66 返回标志,1表示成功,0标志插入位置不合法,-1表示溢出,-2表示不存在*/
     67 int Insert_SeqList(PSeqList PL, int i, int x)
     68 {
     69  int j;
     70  if (!PL)
     71  {
     72   printf("表不存在!
    ");
     73   return (-2);
     74  }
     75  if (PL->length >= MAXSIZE)
     76  {
     77   printf("表溢出
    ");
     78   return (-1);
     79  }
     80  if (i < 1 || i >(PL->length + 1))
     81  {
     82   printf("插入位置不合法......
    ");
     83   return (0);
     84  }
     85  for (j = PL->length - 1; j >= i - 1; j--)
     86   PL->data[j + 1] = PL->data[j];
     87  PL->data[i - 1] = x;
     88  PL->length++;
     89  return 1;
     90 }
     91 //删除顺序表第i个元素,入口参数:顺序表指针,删除元素位置,返回标志1表示成功,0表示删除位置不合法,-1表示表不存在
     92 int Delete_SeqList(PSeqList PL, int i)
     93 {
     94  int j;
     95  if (!PL)
     96  {
     97   printf("表不存在!
    ");
     98   return (-1);
     99  }
    100  if (i<1 || i > PL->length)
    101  {
    102   printf("删除位置不合法!
    ");
    103   return (0);
    104  }
    105  for (j = i; j < PL->length; j++)
    106   PL->data[j - 1] = PL->data[j];
    107  PL->length--;
    108  return (1);
    109 }

    2.单链表

      1 //线性表链式结构
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <string.h>
      5 //主要申明区间
      6 void Mum();//菜单函数
      7 LinkList Create_Linklist(void);//创建链表函数申明
      8 LinkList Insert_Linklist(LinkList head);//插入链表函数申明
      9 LinkList Delete_Linklist_Data(LinkList head);//以data进行删除
     10 LinkList Deleterepeat(LinkList head);//删除重复数据
     11 LinkList Search_Linklist_Data(LinkList head);//以data在链表上查找
     12 LinkList Sort_Data(LinkList head);//按照数据从小到大的顺序排序
     13 LinkList Output_Linklist(LinkList head);//链表的遍历,数据输出
     14 LinkList Reverse_Linklist(LinkList head);//链表倒序
     15 void CountNumber_Linklist(LinkList head);//计算链表数据的长度
     16 void Distory(LinkList *h);//线性表的销毁
     17 void Mum()
     18 {
     19  printf("------------------------------------------------------------------------------------------------------");
     20  printf("请输入以下指令操作:
    ");
     21  printf("				1------插入数据	");
     22  printf("4------删除数据
    ");
     23  printf("				2------查找数据	");
     24  printf("5----------排序
    ");
     25  printf("				3----------输出	");
     26  printf("6------计算长度
    ");
     27  printf("				7----------倒序	");
     28  printf("8------删除重复
    ");
     29  printf("----------------------删除重复指令需要在排序指令操作之后,才可以准确的删除---------------------------
    ");
     30  //为考虑删除数据所需的时间,在排序完在执行,更加便捷
     31 }
     32 /*创建一个以data为元素链表,按data升序排列,以-1输入为建立结束*/
     33 LinkList Create_Linklist(void)
     34 {
     35  int x;
     36  LinkList head, s, r;
     37  head = (ListNode*)malloc(sizeof(ListNode)); /*为头结点head申请空间*/
     38  if (!head)//内存分配判断
     39  {
     40   printf("内存申请失败,程序退出......
    ");
     41   exit(-1);
     42  }
     43  r = head;
     44  r->next = NULL;
     45  printf("请按data升序输入,以-1输入为结束
    ");
     46  printf("请输入一个data:");
     47  scanf("%d", &x);
     48  while (x != -1)
     49  {
     50   s = (ListNode*)malloc(sizeof(ListNode));/*为添入结点申请空间*/
     51   if (!s)//内存分配判断
     52   {
     53    printf("内存申请失败,程序退出......
    ");
     54    exit(-1);
     55   }
     56   s->data = x;
     57   s->next = r->next;
     58   r->next = s;/*这两句表示将新创建的s结点连接到r结点的后面,r初次对应的head并没有数据,所以head是含有空头的链表,画图可以更方便理解*/
     59   r = s;/*用r将新定义的结点s取代,这样可以使用s进行反复连接*/
     60   printf("请输入下一个data:");
     61   scanf("%d", &x); /*输入下一个data*/
     62  }
     63  return head;
     64 }
     65 /*在链表head上插入数据*/
     66 LinkList Insert_Linklist(LinkList head)
     67 {
     68  int x;
     69  ListNode *p, *s;
     70  printf("请输入要插入的data:");
     71  scanf("%d", &x); /*输入要插入的data*/
     72  p = head; /*P指向链表的头结点*/
     73  while (p->next && x > p->next->data)/*查找插入位置的前一个位置*/
     74  {
     75   p = p->next;
     76  }
     77  if (p->next != NULL && x == p->next->data) /*数据已,有不能插入*/
     78   printf("重复插入,不允许。
    ");
     79  else/*插入位置正确*/
     80  {
     81   s = (ListNode *)malloc(sizeof(ListNode)); /*为插入结点申请空间。*/
     82   if (!s)//内存分配判断
     83   {
     84    printf("内存申请失败,程序退出......
    ");
     85    exit(-1);
     86   }
     87   s->data = x;
     88   s->next = p->next; /*将插入结点s插到p的后面*/
     89   p->next = s; /*这两句是插入结点的固定模式,先将p的结点域给s的结点域,然后再将s的头与p的结点相接*/
     90   /*这样就实现了链表的插入链接*/
     91  }
     92  return head;
     93 }
     94 LinkList Delete_Linklist_Data(LinkList head)
     95 {
     96  int count = 0;
     97  int x;
     98  ListNode *p, *r;
     99  if (head->next == NULL)//链表数据为空判断
    100  {
    101   printf("链表为空,无法进行删除,请尝试其他指令......
    ");
    102   return head;
    103  }
    104  printf("请输入要删除的data:");
    105  scanf("%d", &x); /*输入要删除的data*/
    106  p = head;
    107  r = head->next;
    108  while (r)//执行r次循环,如果有r个相同的数据,则都可以删除
    109  {
    110   p = head;
    111   r = head->next;
    112   while (r && x != r->data) /*查找要删除的data*/
    113   {
    114    p = r;
    115    r = r->next; /*依次将p,r后移一个结点*/
    116   }
    117   if (r == NULL)
    118   {
    119    if (count == 0)/*没有找到要删除的data*/
    120     printf("没有找到要删除的data......
    ");
    121   }
    122   else /*找到要删除的data,进行删除*/
    123   {
    124    p->next = r->next;/*删除时直接将要删除的前一个结点指向要删除的结点,就可以进行删除*/
    125    /*这里要删除的结点是r*/
    126    free(r);
    127    count++;
    128   }
    129  }
    130  if (count != 0)
    131   printf("找到要删除的data......
    ");
    132  return head;
    133 }
    134 //删除重复数据
    135 LinkList Deleterepeat(LinkList head)
    136 {
    137  /*LinkList p,r;
    138  p = head->next;
    139  r = p->next;
    140  while (1)
    141  {
    142  while (r && p->data != r->data) //查找要删除的data
    143  {
    144  p = r;
    145  r = r->next; //依次将p,r后移一个结点*
    146  }
    147  if(r == NULL)
    148  break;
    149  if(p->data == r->data) //找到要删除的data,进行删除
    150  {
    151  p->next = r->next;//删除时直接将要删除的前一个结点指向要删除的结点,就可以进行删除
    152  //这里要删除的结点是r
    153  free(r);
    154  r = p->next;
    155  }
    156  }*/
    157  LinkList p, r, q;
    158  p = head->next;
    159  while (p->next)
    160  {
    161   q = p;
    162   while (q->next)
    163   {
    164    r = q->next;
    165    if (p->data == r->data)
    166    {
    167     q->next = r->next;
    168     free(r);
    169    }
    170    if (q->next != NULL)
    171     q = q->next;
    172   }
    173   p = p->next;
    174  }
    175  return head;
    176 }
    177 LinkList Search_Linklist_Data(LinkList head)
    178 {
    179  int x;
    180  ListNode *p, *r;/*定义两个链表类型的指针表示相连的两个结点*/
    181  if (head->next == NULL)//链表数据为空判断
    182  {
    183   printf("链表为空,无法进行查找,请尝试其他指令......
    ");
    184   return head;
    185  }
    186  printf("请输入要查找的data:");
    187  scanf("%d", &x);/*输入要查找的data*/
    188  p = head;
    189  r = head->next;
    190  while (r && x != r->data) /*查找要查找的data*/
    191  {
    192   p = r;
    193   r = r->next;/*依次将p,r后移一个结点*/
    194  }
    195  if (r == NULL)
    196   printf("没有找到要查找的data......
    
    "); /*没有找到要查找的data*/
    197  else
    198  {
    199   printf("存在该查找的数据......
    ");
    200   p = head;
    201   r = head->next;
    202   while (r && x != r->data) /*查找要查找的mob_phono*/
    203   {
    204    p = r;
    205    r = r->next;/*依次将p,r后移一个结点*/
    206   }
    207   if (r)
    208   {
    209    printf(" 该数据为: %d 
    ", r->data);
    210    r = r->next;
    211   }
    212  }
    213  return head;
    214 }
    215 //排序
    216 LinkList Sort_Data(LinkList head)
    217 {
    218  int temp;
    219  if (head->next == NULL)//链表为空判断
    220  {
    221   printf("无数据不执行排序......
    ");
    222   return head;
    223  }
    224  int i, j, count = 0;
    225  LinkList p = (LinkList)malloc(sizeof(ListNode));
    226  if (!p)//内存分配判断
    227  {
    228   printf("内存分配失败,程序中断......
    ");
    229   exit(-1);
    230  }
    231  p = head->next;
    232  while (p)
    233  {
    234   count++;
    235   p = p->next;
    236  }
    237  for (j = 0; j<count - 1; j++)
    238  {
    239   p = head->next;
    240   for (i = 0; i<count - j - 1; i++)
    241   {
    242    if (p->data > p->next->data)
    243    {
    244     temp = p->data;
    245     p->data = p->next->data;
    246     p->next->data = temp;
    247    }
    248    p = p->next;
    249   }
    250  }
    251  return head;
    252 }
    253 /*输出链表head中的所有数据元素*/
    254 LinkList Output_Linklist(LinkList head)
    255 {
    256  ListNode *p;
    257  int i = 0;
    258  p = head->next;
    259  if (head->next == NULL)//链表为空判断
    260  {
    261   printf("链表中没有数据......
    ");
    262   return head;
    263  }
    264  while (p)
    265  {
    266   printf("%d  ", p->data);
    267   p = p->next;/*p指向p的下一个结点*/
    268  }
    269  printf("
    ");
    270  return head;
    271 }
    272 //链表倒序
    273 LinkList Reverse_Linklist(LinkList head)
    274 {
    275  LinkList p, q;
    276  p = head->next;
    277  head->next = NULL;
    278  while (p)
    279  {
    280   q = p;
    281   p = p->next;
    282   q->next = head->next;
    283   head->next = q;
    284  }
    285  return head;
    286 }
    287 void CountNumber_Linklist(LinkList head)//显示链表的长度
    288 {
    289  int count = 0;
    290  LinkList s;
    291  s = head->next;
    292  while (s)
    293  {
    294   count++;
    295   s = s->next;
    296  }
    297  printf("count = %d
    ", count);
    298 }
    299 //销毁线性表
    300 void Distory(LinkList *h)
    301 {
    302  LinkList p, q;
    303  p = *h;
    304  while (p)
    305  {
    306   q = p;
    307   p = p->next;
    308   free(q);
    309  }
    310  *h = NULL;
    311  printf("销毁链表成功......
    ");
    312 }

    3.循环链表

      1 //linklist.h
      2 #include<stdio.h>
      3 #include<stdlib.h>
      4 #include<math.h>
      5 #include<string.h>
      6 #define MAXSIZE 100
      7 //顺序表结构体定义
      8 typedef struct node {
      9  int data[MAXSIZE];
     10  int length;
     11 }SeqList, *PSeqList;
     12 //函数申明区
     13 PSeqList Init_SeqList(void);//创建一个顺序表
     14 PSeqList Input_SeqList(PSeqList L, char a[]);//将数据输入到顺序表中
     15 int Length_SeqList(PSeqList L);//获得表长度
     16 int Location_SeqList(PSeqList L, int x);//查找,定位数据的位置
     17 int Insert_SeqList(PSeqList PL, int i, int x);//在顺序表中插入
     18 int Delete_SeqList(PSeqList PL, int i);//删除第i个元素
     19 void Output_SeqList(PSeqList PL);//输出顺序表
     20 //创建一个顺序表,入口参数无,返回一个指向顺序表的指针,指针值为零表示分配空间失败
     21 PSeqList Init_SeqList(void)
     22 {
     23  PSeqList PL;
     24  PL = (PSeqList)malloc(sizeof(SeqList));
     25  if (PL)
     26   PL->length = 0;
     27  else
     28  {
     29   printf("内存分配失败
    ");
     30   exit(-1);
     31  }
     32  return (PL);
     33 }
     34 //数据输入
     35 PSeqList Input_SeqList(PSeqList L, char a[])
     36 {
     37  int i, a_length = 0;
     38  a_length = strlen(a);
     39  for (i = 0; i < a_length; i++)
     40   L->data[i] = (int)a[i] - 48;//将char型整数转换成int型整数
     41  L->length = i;
     42  return L;
     43 }
     44 //读取顺序表的当前长度
     45 int Length_SeqList(PSeqList L)
     46 {
     47  if (!L)
     48  {
     49   printf("无PL指针,退出程序
    ");
     50   exit(-1);
     51  }
     52  return (L->length);
     53 }
     54 //在顺序表上检索,入口参数为顺序表,检索元素,返回元素位置,0表示查找失败
     55 int Location_SeqList(PSeqList L, int x)
     56 {
     57  int i = 0;
     58  while (i < L->length  &&  L->data[i] != x)
     59   i++;
     60  if (i >= L->length)
     61   return 0;
     62  else
     63   return (i + 1);
     64 }
     65 /*在顺序表的第i个元素之前插入x,入口参数为顺序表指针,插入位置,插入元素,
     66 返回标志,1表示成功,0标志插入位置不合法,-1表示溢出,-2表示不存在*/
     67 int Insert_SeqList(PSeqList PL, int i, int x)
     68 {
     69  int j;
     70  if (!PL)
     71  {
     72   printf("表不存在!
    ");
     73   return (-2);
     74  }
     75  if (PL->length >= MAXSIZE)
     76  {
     77   printf("表溢出
    ");
     78   return (-1);
     79  }
     80  if (i < 1 || i >(PL->length + 1))
     81  {
     82   printf("插入位置不合法......
    ");
     83   return (0);
     84  }
     85  for (j = PL->length - 1; j >= i - 1; j--)
     86   PL->data[j + 1] = PL->data[j];
     87  PL->data[i - 1] = x;
     88  PL->length++;
     89  return 1;
     90 }
     91 //删除顺序表第i个元素,入口参数:顺序表指针,删除元素位置,返回标志1表示成功,0表示删除位置不合法,-1表示表不存在
     92 int Delete_SeqList(PSeqList PL, int i)
     93 {
     94  int j;
     95  if (!PL)
     96  {
     97   printf("表不存在!
    ");
     98   return (-1);
     99  }
    100  if (i<1 || i > PL->length)
    101  {
    102   printf("删除位置不合法!
    ");
    103   return (0);
    104  }
    105  for (j = i; j < PL->length; j++)
    106   PL->data[j - 1] = PL->data[j];
    107  PL->length--;
    108  return (1);
    109 }

    4.顺序栈

     1 //seqstack.h
     2 #include<stdio.h>
     3 #include<stdlib.h>
     4 #define MAXSIZE 100
     5 typedef int DataType;
     6 typedef struct {
     7  DataType data[MAXSIZE];
     8  int top;
     9 }SeqStack, * PSeqStack;
    10 PSeqStack Init_SeqStack (void)
    11 {
    12  PSeqStack S;
    13  S = (PSeqStack)malloc(sizeof(SeqStack));
    14  if (S)
    15   S->top = -1;
    16  else
    17   exit(-1);
    18  return S;
    19 }
    20 int Empty_SeqStack(PSeqStack S)
    21 {
    22  //return (S->top==-1);
    23  if (S->top == -1)
    24   return 1;
    25  else
    26   return 0;
    27 }
    28 int Push_SeqStack(PSeqStack S,DataType x)
    29 {
    30  if (S->top == MAXSIZE - 1)
    31  {
    32   printf("栈满不能入栈
    ");
    33   return 0;
    34  }
    35  else 
    36  {
    37   S->top++;
    38   S->data[S->top] = x;
    39   return 1;
    40  }
    41 }
    42 int Pop_SeqStack(PSeqStack S,DataType *x)
    43 {
    44  if(Empty_SeqStack(S))
    45   return 0;
    46  else
    47  {
    48   *x = S->data[S->top];
    49   S->top--;
    50   return 1;
    51  }
    52 }
    53 int GetTop_SeqStack(PSeqStack S ,DataType *x)
    54 {
    55  if(Empty_SeqStack(S))
    56   return 0;
    57  else
    58  {
    59   *x = S->data[S->top];
    60   return 1;
    61  }
    62 }
    63 void Distory_SeqStack(PSeqStack *S)
    64 {
    65  if(*S)
    66   free(*S);
    67  *S = NULL;
    68 }

    5.链式栈

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 typedef int DataType;
     4 typedef struct node{
     5  DataType data;
     6  struct node *next;
     7 }StackNode, * PStackNode;
     8 typedef struct {
     9  PStackNode top;
    10 }LinkStack, * PLinkStack;
    11 PLinkStack Init_LinkStack (void)
    12 {
    13  PLinkStack S;
    14  S = (PLinkStack)malloc(sizeof(LinkStack));
    15  if(S) 
    16   S->top = NULL;
    17  return S;
    18 }
    19 int Empty_LinkStack(PLinkStack S)
    20 {
    21  return (S->top==NULL);
    22 }
    23 int Push_LinkStack(PLinkStack S,DataType x)
    24 {
    25  PStackNode P;
    26  P = (PStackNode)malloc(sizeof(StackNode));
    27  if(!P)
    28  {
    29   printf("内存溢出");
    30   return 0;
    31  }
    32  P->data = x;
    33  P->next = S->top;
    34  S->top = P;
    35  return 1;
    36 }
    37 int Pop_LinkStack(PLinkStack S,DataType *x)
    38 {
    39  PStackNode P;
    40  if(Empty_LinkStack(S))
    41  {
    42   printf("栈空,不能出栈
    ");
    43   return 0;
    44  }
    45  *x = S->top->data;
    46  P = S->top;
    47  S->top = S->top->next;
    48  free(P);
    49  return 1;
    50 }
    51 int GetTop_LinkStack(PLinkStack S,DataType *x)
    52 {
    53  if(Empty_LinkStack(S))
    54  {
    55   printf("栈空,不能出栈
    ");
    56   return 0;
    57  }
    58  *x = S->top->data;
    59  return 1;
    60 }
    61 void Distroy_LInkStack(PLinkStack *S)
    62 {
    63  PStackNode P,Q;
    64  if(*S)
    65  {
    66   P = (*S)->top;
    67   while(P)
    68   {
    69    Q = P;
    70    P = P->next;
    71    free(Q);
    72   }
    73   free(*S);
    74  }
    75  *S = NULL;
    76 }

    6.顺序队列

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #define MAXSIZE 100
     4 struct point{ 
     5  int row, col, predecessor; 
     6 };
     7 typedef point DataType;
     8 typedef struct {
     9  DataType data[MAXSIZE];
    10  int front ,rear;
    11 }SeqQueue,*PSeqQueue;
    12 PSeqQueue Init_SeqQueue()
    13 {
    14  PSeqQueue Q;
    15  Q = (PSeqQueue)malloc(sizeof(SeqQueue));
    16  if(Q)
    17  {
    18   Q->front = 0;
    19   Q->rear = 0;
    20  }
    21  return Q;
    22 }
    23 int Empty_SeqQueue(PSeqQueue Q)
    24 {
    25  if(Q && Q->front == Q->rear)
    26   return 1;
    27  else
    28   return 0;
    29 }
    30 int In_SeqQueue(PSeqQueue Q,DataType x)
    31 {
    32  if((Q->rear + 1) % MAXSIZE == Q->front)
    33  {
    34   printf("队满
    ");
    35   return 0;
    36  }
    37  else
    38  {
    39   Q->rear = (Q->rear + 1) % MAXSIZE;
    40   Q->data[Q->rear] = x;
    41   return 1;
    42  }
    43 }
    44 int Out_SeqQueue(PSeqQueue Q,DataType *x)
    45 {
    46  if(Empty_SeqQueue(Q))
    47  {
    48   printf("队空");
    49   return 0;
    50  }
    51  else
    52  {
    53   Q->front = (Q->front + 1) % MAXSIZE;
    54   *x = Q->data[Q->front];
    55   return 1;
    56  }
    57 }
    58 int Front_SeqQueue(PSeqQueue Q,DataType *x)
    59 {
    60  if(Q->front == Q->rear)
    61  {
    62   printf("队空
    ");
    63   return 0;
    64  }
    65  else
    66  {
    67   *x = Q->data[(Q->front + 1) % MAXSIZE];
    68   return 1;
    69  }
    70 }
    71 void Distroy_SeqQueue(PSeqQueue *Q)
    72 {
    73  if(*Q)
    74   free(*Q);
    75  *Q = NULL;
    76 }

    7.链式队列

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 typedef int DataType;
     4 typedef struct node {
     5  DataType data;
     6  struct node *next;
     7 }QNode , * PQNode;
     8 typedef struct {
     9  PQNode front ,rear;
    10 }LinkQueue, * PLinkQueue;
    11 PLinkQueue Init_LinkQueue()
    12 {
    13  PLinkQueue Q;
    14  Q = (PLinkQueue)malloc(sizeof(LinkQueue));
    15  if(Q)
    16  {
    17   Q->front =NULL;
    18   Q->rear = NULL;
    19  }
    20  return Q;
    21 }
    22 int Empty_LinkQueue(PLinkQueue Q)
    23 {
    24  if(Q && Q->front == NULL && Q->rear == NULL)
    25   return 1;
    26  else 
    27   return 0;
    28 }
    29 int In_LinkQueue (PLinkQueue Q,DataType x)
    30 {
    31  PQNode P;
    32  P = (PQNode)malloc(sizeof(QNode));
    33  if(!P)
    34  {
    35   printf("内存分配失败");
    36   return 0;
    37  }
    38  P->data = x;
    39  P->next = NULL;
    40  if(Empty_LinkQueue(Q))
    41   Q->rear = Q->front = P;
    42  else
    43  {
    44   Q->rear->next = P;
    45   Q->rear = P;
    46  }
    47  return 1;
    48 }
    49 int Out_LinkQueue(PLinkQueue Q,DataType *x)
    50 {
    51  PQNode P;
    52  if(Empty_LinkQueue(Q))
    53  {
    54   printf("队空");
    55   return 0;
    56  }
    57  *x = Q->front->data;
    58  P = Q->front;
    59  Q->front = Q->front->next;
    60  free(P);
    61  if(!Q->front)
    62   Q->rear = NULL;
    63  return 1;
    64 }
    65 int Front_LinkQueue(PLinkQueue Q,DataType *x)
    66 {
    67  if(Empty_LinkQueue(Q))
    68  {
    69   printf("队空");
    70   return 0;
    71  }
    72  *x = Q->front->data;
    73  return 1;
    74 }
    75 void Distroy_LinkQueue(PLinkQueue *Q)
    76 {
    77  PQNode P;
    78  if(*Q)
    79  {
    80   while((*Q)->front)
    81   {
    82    P =(*Q)->front;
    83    (*Q)->front = (*Q)->front->next;
    84    free(P);
    85   }
    86   free(*Q);
    87  }
    88  *Q = NULL;
    89 }

    8.字符串.1

      1 //Hstring.h头文件定义
      2 typedef struct
      3 {
      4  char *p_ch;
      5  int length;
      6 }Hstring;
      7 int strAssign(Hstring *s1, char *s2);
      8 int StrCopy(Hstring *s1, Hstring s2);
      9 int SubString(Hstring *sub, Hstring s, int pos, int len);
     10 int StrContact(Hsring *t, Hstring s1, Hstring s2);
     11 int StrInsert(Hstring *s, int pos, Hstring t);
     12 int Init_String(Hstring *s);
     13 Distory_String(Hstring *s);
     14 
     15 //串常量赋值
     16 int strAssign(Hstring *s1,char *s2)
     17 {
     18  int i;
     19  char *pc;
     20  if(s1->p_ch)
     21   free(s1->p_ch);
     22  for (i = 0, pc = s2; *pc != ''; i++, p++)
     23   ;
     24  if (i == 0)
     25  {
     26   s1->p_ch = 0;
     27   s1->length = 0;
     28   return 0;
     29  }
     30  if (!(s1->p_ch = (char *)malloc(i*sizeof(char))))
     31  {
     32   printf("堆空间不足,赋值失败!
    ");
     33   return 0;
     34  }
     35  for (int j = 0; j < i; j++)
     36  {
     37   s1->p_ch[j] = s2[j];
     38  }
     39  s1->length = i;
     40  return 1;
     41 }
     42 /*将一个字符串的值赋值给一个字符串变量*/
     43 int StrCopy(Hstring *s1, Hstring s2)
     44 {
     45  if (s2.length <= 0)
     46   return 0;
     47  if (!(s1->p_ch = (char*)malloc(sizeof(char)*length)))
     48  {
     49   printf("堆空间不足,赋值失败!
    ");
     50   return 0;
     51  }
     52  for (int i = 0; i < s2.length; i++)
     53   s1->p_ch[i] = s2.p_ch[i];
     54  s1->length = s2.length;
     55  return 1;
     56 }
     57 /*用sub返回串s的第pos 个字符起长度为len的字串;其中,1<=pos<=strLength(s)且0<=StrLength(s)-pos+1*/
     58 int SubString(Hstring *sub, Hstring s, int pos, int len)
     59 {
     60  int i;
     61  if (pos<1 || pos>s.length || len < 0 || s.length - pos + 1)
     62  {
     63   printf("pos参数不正确
    ");
     64   return 0;
     65  }
     66  if (sub->p_ch)
     67   free(sub->p_ch);
     68  if (!len)
     69  {
     70   sub->p_ch = 0;
     71   sub->length = 0;
     72  }
     73  else
     74  {
     75   sub->p_ch = (char *)malloc(len * sizeof(char));
     76   for (i = 0; i < len; i++)
     77   {
     78    sub->p_ch[i] = s.p_ch[pos - 1 + i];
     79   }
     80   sub->length = len;
     81  }
     82 }
     83 //串链接
     84 int StrContact(Hsring *t, Hstring s1, Hstring s2)
     85 {
     86  if (t->p_ch)
     87   free(t->p_ch);
     88  if (!(t->p_ch = (char *)malloc(sizeof(char)*(s1.length + s2.length))));
     89  {
     90   printf("堆空间已满,串连接失败
    ");
     91   return 0;
     92  }
     93  for (int i = 0; i < s1.length; i++)
     94   t->p_ch[i] = s1.p_ch[i];
     95  t->length = s1.length + s2.length;
     96  for (i = s1.length; i < t->length; i++)
     97  {
     98   t->p_ch[i] = s2.p_ch[i - s1.length];
     99  }
    100  return 1;
    101 }
    102 int StrInsert(Hstring *s, int pos, Hstring t)
    103 {
    104  int i;
    105  if (pos<1 || pos>s->lngth + 1)
    106  {
    107   printf("插入参数不正确,插入失败
    ");
    108   return 0;
    109  }
    110  if (t.length == 0)
    111  {
    112   printf("堆空间为0,插入失败
    ");
    113   return 0;
    114  }
    115  if (!s->p_ch = (char *)realloc(s->p_ch, (s->length + t.length)*sizeof(char))
    116  {
    117   printf("堆空间不足,插入失败
    ");
    118   return 0;
    119  }
    120  for (i = s->length - 1; i >= pos - 1; i--)
    121   s->p_ch[i + t.length] = s->p_ch[i];
    122  for (i = pos - 1; i <= pos + t.length - 2; i++)
    123   s->length = s->length + length;
    124  return 1;
    125 }
    126 //置空串
    127 int Init_String(Hstring *s)
    128 {
    129  s->p_ch = 0;
    130  s->length = 0;
    131  return 1;
    132 }
    133 //销毁串
    134 Distory_String(Hstring *s)
    135 {
    136  if (s->length)
    137  {
    138   free(s->p_ch);
    139   s->p_ch = 0;
    140   s->length = 0;
    141  }
    142  return 1;
    143 }

    9.字符串.2

      1 #include<stdio.h>
      2 #include<stdlib.h>
      3 #define MAXSIZE 256
      4 //本文件中包含的函数
      5 int StrLength(char *s);//获取字符串的长度并返回
      6 int StrConcat(char *s1, char *s2, char *s);//连接两个字符串
      7 int StrSub(char *t, char *s, int i, int len);//从s字符串中的第i个位置获取长度为len的字符串放到*t中并返回1
      8 int StrCmp(char *s1, char *s2);//比较两个字符串是否相等
      9 int StrIndex_BF(char *s, char *t);//获取字符串s中的与t相等的子串,并返回位置
     10 int Index_KMP(const char* S, const char* T); 
     11 //获取字符串的长度并返回
     12 int StrLength(char *s)
     13 {
     14  int i = 0;
     15  while (s[i] != '')
     16   i++;
     17  return i;
     18 }
     19 //连接两个字符串
     20 int StrConcat(char *s1, char *s2, char *s)
     21 {
     22  int i = 0, j, len1, len2;
     23  len1 = StrLength(s1);
     24  len2 = StrLength(s2);
     25  if (len1 + len2 > MAXSIZE - 1)
     26  {
     27   printf("长度溢出
    ");
     28   return 0;
     29  }
     30  j = 0;
     31  while (s1[j] != '')
     32  {
     33   s[i] = s1[j];
     34   i++;
     35   j++;
     36  }
     37  j = 0;
     38  while (s2[j] != '')
     39  {
     40   s[i] = s2[j];
     41   i++;
     42   j++;
     43  }
     44  s[i] = '';
     45  return 1;
     46 }
     47 //从s字符串中的第i个位置获取长度为len的字符串放到*t中并返回1
     48 int StrSub(char *t, char *s, int i, int len)
     49 {
     50  int slen;
     51  int j;
     52  slen = StrLength(s);
     53  if (i<1 || i>slen || len<0 || len>slen - i + 1)
     54  {
     55   printf("参数不对");
     56   return 0;
     57  }
     58  for (j = 0; j < len; j++)
     59   t[j] = s[i + j - 1];
     60  t[j] = '';
     61  return 1;
     62 }
     63 //比较两个字符串是否相等
     64 int StrCmp(char *s1, char *s2)
     65 {
     66  int i = 0;
     67  while (s1[i] == s2[i] && s1[i] != '')
     68   i++;
     69  return (s1[i] == s2[i]);
     70  //如果两个字符串相等,则s1[i]=s2[i]='',返回为1
     71 }
     72 //获取字符串s中的与t相等的子串,并返回位置
     73 int StrIndex_BF(char *s, char *t)
     74 {
     75  //如果找到,则返回位置,未找到返回-1,表示不再串中,即查找失败
     76  int i = 0, j = 0;
     77  while (s[i] != '' && t[j] != '')
     78  {
     79   if (s[i] == t[j])
     80   {
     81    i++;
     82    j++;
     83   }
     84   else
     85   {
     86    i = i - j + 1;
     87    j = 0;
     88   }
     89  }
     90  if (j == StrLength(t))
     91   return i - j;
     92  else
     93   return -1;
     94 }
     95 void get_next(const char* T,int* next)
     96 {
     97     int i = 1, j = 0;
     98     next[1] = 0;
     99     while (i<StrLength(T))
    100     {
    101  if(j == 0 || T[i] == T[j])
    102         {
    103       ++i; 
    104       ++j;
    105       next[i] = j;
    106         }
    107         else  
    108    j = next[j];
    109     }
    110 }
    111  /*算法4.8*/
    112 /*void get_nextval(const char* T, int* nextval)
    113 {
    114     int i=1,j=0;
    115     nextval[1] = 0;
    116     while (i<strlen(T))
    117     {
    118  if(j == 0 || T[i] == T[j])
    119         {
    120             ++i; 
    121    ++j;
    122             if (T[i]!=T[j])
    123     nextval[i] = j;
    124             else 
    125     nextval[i] = nextval[j];
    126         }
    127         else   
    128    j = nextval[j];
    129     }
    130 }
    131 */
    132  /*算法4.6*/
    133 int Index_KMP(const char* S, const char* T) 
    134 {
    135    int* next=(int*)malloc(sizeof(int)*strlen(T));
    136    int i=1, j=1, k=1;
    137    get_next(T, next);
    138    printf("the Value of Next is   :");
    139    while (k < strlen(T))
    140    {
    141       printf("%3d",next[k]);
    142       ++k;
    143    }
    144    printf("
    ");
    145    while (i < strlen(S) && j < strlen(T))
    146    {
    147       if (j == 0 || S[i] == T[j])
    148       {
    149   ++i;  
    150   ++j;
    151       }
    152       else
    153     j = next[j];
    154    }
    155   if (j >= strlen(T))
    156    return  i-strlen(T);
    157   else 
    158    return 0;
    159 }
    160  
    161  
    162  
  • 相关阅读:
    codevs 1069关押罪犯
    codevs 1497取余运算
    codevs 3324 新斯诺克
    codevs 3286 火柴排队
    继续畅通工程
    还是畅通工程
    畅通工程(并查集找根节点)
    Eddy's picture(最小生成树)
    Constructing Roads(最小生成树)
    Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan
  • 原文地址:https://www.cnblogs.com/a1982467767/p/8893542.html
Copyright © 2011-2022 走看看