zoukankan      html  css  js  c++  java
  • 严蔚敏版《数据结构 (C语言版)》和《数据结构题集》(一)

    这里用的是严蔚敏版《数据结构 (C语言版)》和《数据结构题集》,深感自己的代码写得又渣又无力,打算把这本书好好琢磨透彻,里面的算法和模板都实现一遍,题目也都做一遍。最终能够做到举一反三、熟之又熟地运用数据结构来解决实际问题。
    题集 2.10 2.11

     1 #include <iostream>
     2 #include <stdlib.h>
     3 #include <stdio.h>
     4 #define LIST_INIT_SIZE 100
     5 #define LISTINCREMENT 10
     6 using namespace std;
     7 //1 从顺序表A中删除第I个元素后面的K个元素
     8 typedef int Elemtype;
     9 typedef struct {
    10 Elemtype *elem;
    11 int length;
    12 int listsize;
    13 } Sqlist;
    14 
    15 int InitList_Sq(Sqlist &l){
    16  l.elem=(Elemtype *)malloc(sizeof(Elemtype)*LIST_INIT_SIZE);//calloc(list_initsize,sizeof(elemtype))??
    17  //calloc and malloc 's diffence;
    18  if (!l.elem) return 0;
    19  l.length=0;
    20  l.listsize=LIST_INIT_SIZE;
    21  return 1;
    22 }
    23 int readSqlist(Sqlist &l){
    24 int x;
    25 scanf ("%d",&x);
    26 int i=0;
    27 while(x!=-999&&i<LIST_INIT_SIZE){
    28 l.elem[i]=x;
    29 i++;
    30 scanf ("%d",&x);
    31 l.length++;
    32 }
    33 return 1;
    34 }
    35 int displaySqlist(Sqlist &l){
    36 int i=0;
    37 while (i<l.length){
    38 printf("%d ",l.elem[i]);
    39 i++;
    40 }
    41 printf("
    ");
    42 return 1;
    43 }
    44 
    45 int DeleteList_Sq(Sqlist &a,int i,int k){
    46 if (i<0||i>a.length||k<0||k>a.length-i)
    47 return 0;
    48 else {
    49 if (a.length-i-k>=k){
    50 for (int j=0;j<k;j++){
    51 a.elem[j+i]=a.elem[k+i+j];
    52 }a.length=a.length-k;
    53 }else {
    54 for (int j=0;j<a.length-i-k;j++){
    55 a.elem[j+i]=a.elem[k+i+j];
    56 }a.length=a.length-k;
    57 }
    58 return 1;
    59 }
    60 }
    61 int InsertOrderedList_Sq(Sqlist &va,int x){
    62 int i=0;
    63 while (va.elem[i]<x&&i<va.length){
    64 i++;
    65 }
    66 if (i==va.length){
    67 va.elem[va.length]=x;
    68 va.length++;
    69 }
    70 if (va.elem[i]>=x){
    71 for (int j=va.length;j>i;j--){
    72 va.elem[j]=va.elem[j-1];
    73 }
    74 va.elem[i]=x;
    75 va.length++;
    76 //一种更简便的方法
    77 //if (va.length==va.listsize)
    78 //return 0;//overflow
    79 //for (int j=va.length;j>0,x<va.elem[j-1];j--)
    80 //va.elem[j]=va.elem[j-1];
    81 //va.elem[j]=x;
    82 //va.length++;
    83 //}
    84 return 1;
    85 }
    86 
    87 int main()
    88 {
    89    Sqlist l;
    90    InitList_Sq(l);
    91    readSqlist(l);
    92    displaySqlist(l);
    93    DeleteList_Sq(l,2,5);
    94    displaySqlist(l);
    95 
    96     return 0;
    97 }
  • 相关阅读:
    洛谷⑨月月赛Round2 官方比赛 OI
    3243 区间翻转
    3279 奶牛健美操
    1959 拔河比赛
    2144 砝码称重 2
    BZOJ1999 树网的核[数据加强版]
    U4704 函数
    U4687 不无聊的序列
    U4699 鸡蛋
    UVA 11212 Editing a Book
  • 原文地址:https://www.cnblogs.com/twomeng/p/9476514.html
Copyright © 2011-2022 走看看