title: 【锐格】数据结构-线性表
tags:

  • 锐格
  • C
  • 链表
    abbrlink: 2eb647a7
    date: 2021-09-21 11:11:21

欢迎加入NEFU计算机编程交流群:523539085

顺序表

4882

  1. #include<iostream>
  2. #include<stdlib.h>
  3. using namespace std;
  4. const int defaultSize = 10;
  5. class SeqList {
  6. protected:
  7. int *data;
  8. int maxSize; //表最大可容纳项数
  9. int last; //当前表大小
  10. public:
  11. SeqList(int sz = defaultSize);
  12. SeqList(SeqList &l);
  13. ~SeqList();
  14. int Length() const; //计算表长度
  15. int getData(int i) const; //取第i歌表项的值,存放在x中
  16. void setData(int i,int x);
  17. bool Insert(int i,int x); //插入x在第i歌表项之后
  18. void Remove(int i);
  19. void output(); // 输出顺序表
  20. };
  21. SeqList::SeqList(int sz)
  22. {
  23. if (sz>0) {
  24. maxSize = sz;
  25. last = -1;
  26. data = new int[maxSize];
  27. if (data == NULL)
  28. exit(1);
  29. }
  30. }
  31. SeqList::~SeqList()
  32. {
  33. delete []data;
  34. }
  35. int SeqList::Length() const
  36. {
  37. return last+1;
  38. }
  39. int SeqList::getData(int i) const
  40. {
  41. return data[i];
  42. }
  43. void SeqList::setData(int i,int x)
  44. {
  45. data[i] = x;
  46. }
  47. bool SeqList::Insert(int i,int x)
  48. {
  49. if (last == maxSize-1)
  50. return false;
  51. if (i<0||i>last+1)
  52. return false;
  53. int j;
  54. for (j=last;j>=i;j--)
  55. data[j+1] = data[j];
  56. data[i] = x;
  57. last++;
  58. return true;
  59. }
  60. void SeqList::Remove(int i)
  61. {
  62. int j;
  63. for (j=i;j<=last-1;j++)
  64. data[j] = data[j+1];
  65. last--;
  66. }
  67. void SeqList::output()
  68. {
  69. int i;
  70. for (i=0;i<=last;i++)
  71. cout << data[i] << " ";
  72. cout << endl;
  73. }
  74. //write your code here
  75. int main()
  76. {
  77. int n,m,i,x;
  78. cin >> n >> m;//输入两个表的长度
  79. SeqList l1(n),l2(m);
  80. for (i=0;i<n;i++) {
  81. cin >> x;
  82. l1.Insert(i,x);//list1
  83. }
  84. for (i=0;i<m;i++) {
  85. cin >> x;
  86. l2.Insert(i,x);//list2
  87. }
  88. //write your code here
  89. int len=min(n, m);
  90. int cnt=0;
  91. for(int i=0;i<len;i++)
  92. {
  93. if(l1.getData(0)==l2.getData(0))
  94. {
  95. cout<<l1.getData(0)<<" ";
  96. cnt++;
  97. l1.Remove(0);
  98. l2.Remove(0);
  99. }
  100. }
  101. cout<<endl;
  102. if(l1.Length()==0 && l2.Length()==0)
  103. {
  104. cout<<"="<<endl;
  105. }
  106. else if(l1.Length()<=0 || l1.getData(0)<l2.getData(0))
  107. {
  108. cout<<"<"<<endl;
  109. }
  110. else
  111. {
  112. cout<<">"<<endl;
  113. }
  114. return 0;
  115. }

4314

这个merge方式用for循环思索了很久还是觉得很难实现,只好用while代替之了

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAXSIZE 100
  4. #define TRUE 1
  5. #define FALSE 0
  6. typedef int Status;
  7. typedef struct SeqList{
  8. int data[MAXSIZE];
  9. int last; //当前表大小
  10. }SeqList, *pSeqList;
  11. const int Length(const pSeqList l)
  12. {
  13. return l->last+1;
  14. }
  15. const int getData(const pSeqList l, int i)
  16. {
  17. return l->data[i];
  18. }
  19. void setData(pSeqList l, int i,int x)
  20. {
  21. if(i >= 0 && i <= l->last)
  22. l->data[i] = x;
  23. }
  24. Status Insert(pSeqList l, int i, int x)
  25. {
  26. // write your code here
  27. for (int j=l->last;j>=i;j--)
  28. l->data[j+1] = l->data[j];
  29. l->data[i] = x;
  30. l->last++;
  31. }
  32. void output(const SeqList l)
  33. {
  34. int i;
  35. for (i=0;i<=l.last;i++)
  36. {
  37. printf("%d ", l.data[i]);
  38. }
  39. printf("\n");
  40. }
  41. void mergeSeqList(const pSeqList l1,const pSeqList l2, pSeqList l)
  42. {
  43. // write your code here
  44. int len, cnt1=0, cnt2=0;
  45. len=Length(l1)+Length(l2);
  46. int l1v=getData(l1, cnt1), l2v=getData(l2, cnt2);
  47. int i=0;
  48. while(cnt1< Length(l1) && cnt2<Length(l2))
  49. {
  50. if(l1v <= l2v)
  51. {
  52. Insert(l, i++, l1v);
  53. l1v=getData(l1, ++cnt1);
  54. }
  55. else
  56. {
  57. Insert(l, i++, l2v);
  58. l2v=getData(l2, ++cnt2);
  59. }
  60. }
  61. if(cnt1<Length(l1))
  62. while(i<len)
  63. {
  64. Insert(l, i++, l1v);
  65. l1v=getData(l1, ++cnt1);
  66. }
  67. else
  68. while(i<len)
  69. {
  70. Insert(l, i++, l2v);
  71. l2v=getData(l2, ++cnt2);
  72. }
  73. }
  74. int main()
  75. {
  76. int n,m,i,x;
  77. scanf("%d", &n);
  78. scanf("%d", &m);
  79. SeqList l1, l2;
  80. l1.last = -1;
  81. l2.last = -1;
  82. for (i=0;i<n;i++) {
  83. scanf("%d", &x);
  84. Insert(&l1, i, x);
  85. }
  86. for (i=0;i<m;i++) {
  87. scanf("%d", &x);
  88. Insert(&l2, i, x);
  89. }
  90. SeqList l;
  91. l.last = -1;
  92. mergeSeqList(&l1,&l2,&l);
  93. output(l);
  94. return 0;
  95. }

4880

正确输出:4 9 6 2 7

我的输出:4 6 2 7 9

但是样例上面写的又和我一致,就很迷惑【锐格】数据结构-线性表 - 图1


破案了,样例给错了,正确样例应该是数据里面的,还有主函数里面调用函数需要自己去调整顺序…这锐格真的让人迷惑


  1. #include<iostream>
  2. #include<stdlib.h>
  3. using namespace std;
  4. const int defaultSize = 10;
  5. class SeqList {
  6. protected:
  7. int *data;
  8. int maxSize; //表最大可容纳项数
  9. int last; //当前表大小
  10. public:
  11. SeqList(int sz = defaultSize);
  12. ~SeqList();
  13. int Length() const; //计算表长度
  14. int getData(int i) const; //取第i歌表项的值,存放在x中
  15. void setData(int i,int x);
  16. bool Insert(int i,int &x); //插入x在第i歌表项之后
  17. void Remove(int i);
  18. void DeleteAllx(int x);
  19. int deleteMin();
  20. void deleteDuplication();
  21. void output(); // 输出顺序表
  22. };
  23. SeqList::SeqList(int sz)
  24. {
  25. if (sz>0) {
  26. maxSize = sz;
  27. last = -1;
  28. data = new int[maxSize];
  29. if (data == NULL)
  30. exit(1);
  31. }
  32. }
  33. SeqList::~SeqList()
  34. {
  35. delete []data;
  36. }
  37. int SeqList::Length() const
  38. {
  39. return last+1;
  40. }
  41. int SeqList::getData(int i) const
  42. {
  43. return data[i];
  44. }
  45. void SeqList::setData(int i,int x)
  46. {
  47. data[i] = x;
  48. }
  49. bool SeqList::Insert(int i,int &x)
  50. {
  51. if (last == maxSize-1)
  52. return false;
  53. if (i<0||i>last+1)
  54. return false;
  55. int j;
  56. for (j=last;j>=i;j--)
  57. data[j+1] = data[j];
  58. data[i] = x;
  59. last++;
  60. return true;
  61. }
  62. void SeqList::Remove(int i)
  63. {
  64. int j;
  65. for (j=i;j<=last;j++)
  66. data[j] = data[j+1];
  67. last--;
  68. }
  69. //write your code here
  70. void SeqList::DeleteAllx(int x)
  71. {
  72. for(int i=0;i<=last;i++)
  73. {
  74. if(getData(i)==x)
  75. Remove(i--);
  76. }
  77. }
  78. void SeqList::deleteDuplication()
  79. {
  80. for(int i=0;i<=last;i++)
  81. {
  82. for(int j=i+1;j<=last;j++)
  83. {
  84. if(getData(i) == getData(j))
  85. {
  86. Remove(j--);
  87. }
  88. }
  89. }
  90. }
  91. int SeqList::deleteMin()
  92. {
  93. int mininum=99999, idx=-1;
  94. for(int i=0;i<=last;i++)
  95. {
  96. if(getData(i)<mininum)
  97. {
  98. mininum=getData(i);
  99. idx=i;
  100. }
  101. }
  102. data[idx]=data[last];
  103. last--;
  104. return mininum;
  105. }
  106. void SeqList::output()
  107. {
  108. for (int i=0;i<=last;i++)
  109. cout << data[i] << " ";
  110. cout << endl;
  111. }
  112. int main()
  113. {
  114. int n,i,x;
  115. cin >> n;
  116. SeqList l(n);
  117. for (i=0;i<n;i++) {
  118. cin >> x;
  119. l.Insert(i,x);
  120. }
  121. l.deleteDuplication();
  122. l.DeleteAllx(3);
  123. l.deleteMin();
  124. l.output();
  125. return 0;
  126. }
  127. //3 4 1 6 4 2 4 7 3 1 2 4 6 6 9

4312

这题思路想出来以后还兴奋了一下

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAXSIZE 100
  4. #define TRUE 1
  5. #define FALSE 0
  6. typedef int Status;
  7. int data[MAXSIZE];
  8. int last = -1; //当前表大小
  9. const int Length()
  10. {
  11. return last+1;
  12. }
  13. const int getData(int i)
  14. {
  15. return data[i];
  16. }
  17. void setData(int i,int x)
  18. {
  19. data[i] = x;
  20. }
  21. Status Insert(int i,int x)
  22. {
  23. // write your code here
  24. for(int j=last;j>=i;j--)
  25. {
  26. data[j+1]=data[j];
  27. }
  28. data[i]=x;
  29. last++;
  30. }
  31. void output()
  32. {
  33. // write your code here
  34. for(int i=0;i<Length();i++)
  35. printf("%d ", data[i]);
  36. printf("\n");
  37. }
  38. void condense()
  39. {
  40. // write your code here
  41. int cnt=0;
  42. for(int i=0;i<Length();i++)
  43. {
  44. if(data[i]!=0)
  45. {
  46. if(i!=cnt)
  47. {
  48. data[cnt]=data[i];
  49. data[i]=0;
  50. }
  51. cnt++;
  52. }
  53. }
  54. }
  55. //2 0 10 9 4 0 2 0 0 1
  56. int main()
  57. {
  58. int n,i,x;
  59. scanf("%d", &n);
  60. for (i=0;i<n;i++) {
  61. scanf("%d", &x);
  62. Insert(i,x);
  63. }
  64. condense();
  65. output();
  66. return 0;
  67. }

4311

双指针的经典应用

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAXSIZE 100
  4. #define TRUE 1
  5. #define FALSE 0
  6. typedef int Status;
  7. int data[MAXSIZE];
  8. int last = -1; //当前表大小
  9. const int Length()
  10. {
  11. return last+1;
  12. }
  13. Status Insert(int i,int x)
  14. {
  15. // write your code here
  16. for(int j=last;j>=i;j--)
  17. {
  18. data[j+1]=data[j];
  19. }
  20. data[i]=x;
  21. last++;
  22. }
  23. void output()
  24. {
  25. // write your code here
  26. for(int i=0;i<Length();i++)
  27. {
  28. printf("%d ", data[i]);
  29. }
  30. printf("\n");
  31. }
  32. void reverse()
  33. {
  34. // write your code here
  35. int i,j;
  36. int tmp;
  37. for(int i=0,j=last; i<j; i++,j--)
  38. {
  39. tmp=data[i];
  40. data[i]=data[j];
  41. data[j]=tmp;
  42. }
  43. }
  44. int main()
  45. {
  46. int n,i,x;
  47. scanf("%d", &n);
  48. for (i=0;i<n;i++) {
  49. scanf("%d", &x);
  50. Insert(i,x);
  51. }
  52. output();
  53. reverse();
  54. output();
  55. return 0;
  56. }

4877

基础的增删查改的练习,但是需要辨别清楚i在0,1起点时不同的边界(debug了好久..)

  1. #include<iostream>
  2. #include<stdlib.h>
  3. using namespace std;
  4. const int defaultSize = 10;
  5. class SeqList {
  6. protected:
  7. int *data;
  8. int maxSize; //表最大可容纳项数
  9. int last; //当前表大小
  10. void reSize(int newSize); //改变data数组空间大小
  11. public:
  12. SeqList(int sz = defaultSize);
  13. SeqList(SeqList &l);
  14. ~SeqList();
  15. int Size() const; //计算表最大可容纳表项个数
  16. int Length() const; //计算表长度
  17. int Search(int &x) const; //搜索x在表中位置,函数返回表项序号
  18. bool getData(int i,int &x) const; //取第i歌表项的值,存放在x中
  19. void setData(int i,int &x); //用x修改第i个表项的值
  20. bool Insert(int i,int &x); //插入x在第i歌表项之后
  21. bool Remove(int i,int &x); //删除第i歌表项,用x返回其值
  22. bool isEmpty(); //判断表是否空
  23. bool isFull(); //判断表是否满
  24. void output(); // 输出顺序表
  25. };
  26. //write your code here
  27. SeqList::SeqList(int sz)
  28. {
  29. if (sz>0) {
  30. maxSize = sz;
  31. last = -1;
  32. data = new int[maxSize];
  33. if (data == NULL)
  34. exit(1);
  35. }
  36. }
  37. SeqList::~SeqList()
  38. {
  39. delete []data;
  40. }
  41. int SeqList::Size() const
  42. {
  43. return maxSize;
  44. }
  45. bool SeqList::isFull()
  46. {
  47. if(last==maxSize)
  48. return true;
  49. else
  50. return false;
  51. }
  52. int SeqList::Length() const
  53. {
  54. return last+1;
  55. }
  56. bool SeqList::getData(int i,int &x) const
  57. {
  58. return x=data[i-1];
  59. }
  60. void SeqList::setData(int i,int &x)
  61. {
  62. data[i-1] = x;
  63. }
  64. bool SeqList::Insert(int i,int &x)
  65. {
  66. if (last == maxSize-1)
  67. return false;
  68. if (i<0||i>last+1)
  69. return false;
  70. int j;
  71. for (j=last;j>=i;j--)
  72. data[j+1] = data[j];
  73. data[i] = x;
  74. last++;
  75. return true;
  76. }
  77. bool SeqList::Remove(int i,int &x)
  78. {
  79. int j;
  80. x=data[--i];
  81. for (j=i;j<=last-1;j++)
  82. data[j] = data[j+1];
  83. last--;
  84. return true;
  85. }
  86. // the end
  87. void SeqList::output()
  88. {
  89. int i;
  90. for (i=0;i<=last;i++)
  91. cout << data[i] << " ";
  92. cout << endl;
  93. }
  94. int main()
  95. {
  96. SeqList l(8);
  97. int n,i,x;
  98. cin >> n;
  99. for (i=0;i<n;i++) {
  100. cin >> x;
  101. if (l.isFull())
  102. break;
  103. l.Insert(i,x);
  104. }
  105. cout << l.Size() << " " << l.Length() << endl;
  106. l.output();
  107. l.Remove(3,n);
  108. l.setData(2,n);
  109. l.getData(1,n);
  110. cout << n << endl;
  111. l.output();
  112. return 0;
  113. }

4866(约瑟夫环问题)

本题比较重要,所以着重理解了一下(过程用注释标注)

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. /*利用链表实现*/
  4. typedef struct link{
  5. int data;
  6. struct link *next;
  7. }link_s, *link_p;
  8. void createList(link_p &head, int num)
  9. {
  10. if (num < 1)
  11. {
  12. head = NULL;
  13. return;
  14. }
  15. head = (link_p)malloc(sizeof(link_s));
  16. head->data = 1;
  17. head->next = NULL;
  18. link_p new_node = head;
  19. for (int i = 2; i < num+1; i++)
  20. {
  21. new_node->next = (link_p)malloc(sizeof(link_s));
  22. new_node = new_node->next;
  23. new_node->data = i;
  24. new_node->next = NULL;
  25. }
  26. new_node->next = head; /*把最后一个节点指向头结点,构成循环链表*/
  27. return;
  28. }
  29. void output(link_p head)
  30. {
  31. int i = 0;
  32. link_p p_node = head;
  33. while (p_node && i < 100)
  34. {
  35. printf("%d->", p_node->data);
  36. p_node = p_node->next;
  37. i++;
  38. }
  39. return;
  40. }
  41. /*实现功能从1至N开始顺序循环数数,每数到M输出该数值*/
  42. void Josephus(link_p head, int num)
  43. {
  44. int i = 1, remove_num = 1;
  45. link_p p_node = head;
  46. link_p p_temp = NULL;
  47. while (p_node)
  48. {
  49. if (num - 1 == i)
  50. {
  51. if (p_node == p_node->next) /*剩下最后一个节点时,直接打印*/
  52. {
  53. printf("%d ", p_node->data);
  54. free(p_node);
  55. p_node = NULL;
  56. break;
  57. }
  58. i = 1;
  59. printf("%d ", p_node->next->data); /*找到第M-1个节点,打印第M个节点*/
  60. p_temp = p_node->next->next; /*保存第M+1个节点地址*/
  61. free(p_node->next); /*释放第M个节点地址*/
  62. p_node->next = p_temp; /*第M-1个节点指向第M+1个节点*/
  63. p_node= p_node->next; /*p_node指向第M+1个节点*/
  64. remove_num++;
  65. }
  66. else
  67. {
  68. i++;
  69. p_node = p_node->next;
  70. }
  71. }
  72. return ;
  73. }
  74. int main(void)
  75. {
  76. int n = 0, m = 0;
  77. scanf("%d %d", &n, &m);
  78. getchar();
  79. link_p lk = NULL;
  80. createList(lk, n);
  81. Josephus(lk, m);
  82. return 0;
  83. }

单链表

4331

一开始太粗心了,没看清题意,原来这是个循环链表…还以为是什么高级算法对着初始化部分研究了半天

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. typedef struct Node
  4. {
  5. int data;
  6. struct Node *next;
  7. }Node;
  8. Node *first;
  9. void List()
  10. {
  11. first = (struct Node *)malloc(sizeof(struct Node));
  12. first->next = first;
  13. }
  14. void Insert(int val)
  15. {
  16. Node *n = (struct Node *)malloc(sizeof(struct Node));
  17. n->data = val;
  18. Node *temp=first;
  19. while (temp->next!=first)
  20. temp = temp->next;
  21. n->next = temp->next;
  22. temp->next = n;
  23. }
  24. // 删除并打印
  25. void DeleteAndPrint()
  26. {
  27. // write your code here
  28. Node *p = first, *minp, *pre, *minpre;
  29. while(first->next!=first)
  30. {
  31. p=first->next;
  32. pre=first;
  33. minp=p;
  34. minpre=pre;
  35. while(p!=first)
  36. {
  37. if(p->data<minp->data)
  38. {
  39. minp=p;
  40. minpre=pre;
  41. }
  42. pre=p;
  43. p=p->next;
  44. }
  45. printf("%d ", minp->data);
  46. minpre->next=minp->next;
  47. free(minp);
  48. }
  49. free(first);
  50. }
  51. int main()
  52. {
  53. List();
  54. int val;
  55. scanf("%d", &val);
  56. while (val!=-1)
  57. {
  58. Insert(val);
  59. scanf("%d", &val);
  60. }
  61. DeleteAndPrint();
  62. return 0;
  63. }

4329

很有意思的一个思路

  1. 定义双指针指针p和q

  2. 先让p进行移动直到正数第K个数

  3. 然后q指针开始跟随p指针一起移动 ;

  4. 直至“”p“”指针遍历到链表末尾 ;

  5. 此时“”q“”指针指的就是倒数第k个数
    其实应当就是当p走过k个数之后,p和q就相对于倒数第k个数对称了,最后得到的就是答案

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #define TRUE 1
  5. #define FALSE 0
  6. typedef int Status;
  7. typedef struct Node {
  8. int data;
  9. struct Node *next;
  10. }Node, *pNode;
  11. pNode first;
  12. void Insert(int x)
  13. {
  14. // rite your code here
  15. Node *p=(Node*)malloc(sizeof(Node));
  16. p->data=x;
  17. Node *tmp=first;
  18. while(tmp->next!=NULL)
  19. tmp=tmp->next;
  20. p->next=tmp->next;
  21. tmp->next=p;
  22. }
  23. Status SearchK(int k,int *val)
  24. {
  25. // write your code here
  26. Node *p=first;
  27. Node *q=first;
  28. int n=0;
  29. while(n<k)//p自己动
  30. {n++;
  31. p=p->next;
  32. }
  33. while(p!=NULL)//p,q一起移动
  34. {
  35. p=p->next;
  36. q=q->next;
  37. }
  38. *val=q->data;
  39. }
  40. int main()
  41. {
  42. first = (struct Node*)malloc(sizeof(struct Node));
  43. first->next = NULL;
  44. int i;
  45. scanf("%d", &i);
  46. while (i!=-1) {
  47. Insert(i);
  48. scanf("%d", &i);
  49. }
  50. int val;
  51. if (SearchK(4, &val))
  52. printf("%d\n", val);
  53. return 0;
  54. }

4320

数据结构的基本练习,相当于读取链表并且进行了一次头插法,大概流程形如

L-NULL

L-1-NULL

L-2-1-NULL

L-5-…-2-1-NULL
以此来达到倒转的目的

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. typedef struct Node {
  5. int data;
  6. struct Node *next;
  7. }Node, *pNode;
  8. pNode first;
  9. int Length(pNode first)
  10. {
  11. int i=0;
  12. Node *temp = first->next;
  13. while (temp!=NULL) {
  14. i++;
  15. temp = temp->next;
  16. }
  17. return i;
  18. }
  19. Node *getHead()
  20. {
  21. return first;
  22. }
  23. void create(int a[], int n)
  24. {
  25. Node *temp = first;
  26. int i;
  27. for (i=0;i<n;i++) {
  28. pNode t = (struct Node *)malloc(sizeof(struct Node));
  29. t->data = a[i];
  30. t->next = NULL;
  31. temp->next = t;
  32. temp = temp->next;
  33. }
  34. }
  35. void output()
  36. {
  37. // write your code here
  38. Node *tmp=first->next;
  39. while(tmp!=NULL)
  40. {
  41. printf("%d ",tmp->data);
  42. tmp=tmp->next;
  43. }
  44. }
  45. void reverseList()
  46. {
  47. // write your code here
  48. Node *tmp=first->next;
  49. Node *p;
  50. first->next=NULL;
  51. while(tmp!=NULL)
  52. {
  53. p=tmp->next;
  54. tmp->next=first->next;
  55. first->next=tmp;
  56. tmp=p;
  57. }
  58. }
  59. int main()
  60. {
  61. first = (struct Node *)malloc(sizeof(struct Node));
  62. int a[5];
  63. int i;
  64. for (i=0;i<5;i++)
  65. scanf("%d", a+i);
  66. create(a, 5);
  67. reverseList();
  68. output();
  69. return 0;
  70. }

4319

属于是前面思维超前了,直接复制前一题的代码就能交…

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. typedef struct Node{
  5. int data;
  6. struct Node *next;
  7. }Node, *pNode;
  8. pNode first;
  9. int Length(pNode first)
  10. {
  11. int i=0;
  12. pNode temp = first->next;
  13. while (temp!=NULL) {
  14. i++;
  15. temp = temp->next;
  16. }
  17. return i;
  18. }
  19. pNode getHead()
  20. {
  21. return first;
  22. }
  23. void create(pNode first, int a[], int n)
  24. {
  25. Node *temp = first;
  26. int i;
  27. for (i=0;i<n;i++) {
  28. Node *t = (struct Node*)malloc(sizeof(struct Node));
  29. t->data = a[i];
  30. t->next = NULL;
  31. temp->next = t;
  32. temp = temp->next;
  33. }
  34. }
  35. void Insert(pNode first, int x)
  36. {
  37. pNode n = (struct Node*)malloc(sizeof(struct Node));
  38. n->data = x;
  39. n->next = first->next;
  40. first->next = n;
  41. }
  42. void output(pNode first, int n)
  43. {
  44. pNode temp = first->next;
  45. while (n--) {
  46. printf("%d ", temp->data);
  47. temp = temp->next;
  48. }
  49. }
  50. void reverseList(pNode first)
  51. {
  52. // write your code here
  53. Node *tmp=first->next;
  54. Node *p;
  55. first->next=NULL;
  56. while(tmp!=NULL)
  57. {
  58. p=tmp->next;
  59. tmp->next=first->next;
  60. first->next=tmp;
  61. tmp=p;
  62. }
  63. }
  64. int main()
  65. {
  66. first = (struct Node *)malloc(sizeof(struct Node));
  67. first->next = NULL;
  68. pNode ha = (struct Node *)malloc(sizeof(struct Node));
  69. ha->next = NULL;
  70. int a[5];
  71. int i;
  72. for (i=0;i<5;i++)
  73. scanf("%d", a+i);
  74. create(ha, a, 5);
  75. reverseList(ha);
  76. output(ha, 5);
  77. return 0;
  78. }

4885

debug的大胜利

首先这道题要求最后结果微非递增有序,由于本人智力有限,所以实在想不出什么能够直接给两个原非递减的单链表经过一次遍历就直接按照非递增形式存储的方法,所以只能先以非递减形式存储后再进行reverse(这段太好用了),但是中间有个小插曲,就是两个链表中间会有相同数字的处理比较棘手,调试了很久,一开始直接写的是

  1. pc->next = pa;
  2. pc = pa;
  3. pc->next = pb;
  4. pc = pb;
  5. pa = pa->next;
  6. //temp = pb;
  7. pb = pb->next;

感觉逻辑上也没什么问题,就是按照前后文一致的给链表尾部插入节点,然后尾指针后移,但是调试结果显示链表的循环结束不了了。。。里面的数据全是7(也就是卡在了第一个重复的数据上面),后来又想到加了一个pc->next=NULL;依然未果,最后研究了好久发现这么写的话pa和pb的指针会混杂到pc的链表里面,导致最后找不到NULL节点了。。(一直在互相指针),这也是本题设计思路的原则,即要想用老空间存新内容,那么就要在新空间尾节点向后拓展的同时老空间的头节点往后移动,每次都是增减一个数据才能保证完整保存,而且要让pa,pb,pc互不干扰(也就是说其实他们就是三个链表,所以他们三个之间的元素不应该有交集),比如从pa给到pc的节点,那么这个节点就应该从pa里面剥离出去,只和pc有关,而不在pa,pb的节点里,这段思路很有意思,望读者仔细思考

  1. #include<iostream>
  2. using namespace std;
  3. struct Node {
  4. int data;
  5. Node* next;
  6. };
  7. class List {
  8. private:
  9. Node* first;
  10. public:
  11. List();
  12. ~List();
  13. void makeEmpty();
  14. Node* getHead();
  15. void setHead() { first->next = NULL; }
  16. int Length();
  17. void create(int a[], int n);
  18. void output();
  19. };
  20. List::List() {
  21. first = new Node();
  22. first->next = NULL;
  23. }
  24. List::~List() {
  25. //makeEmpty();
  26. //makeEmpty()函数有缺陷,无法删除头结点导致hb的时候出错
  27. }
  28. int List::Length() {
  29. int i = 0;
  30. Node* temp = first->next;
  31. while (temp != NULL) {
  32. i++;
  33. temp = temp->next;
  34. }
  35. return i;
  36. }
  37. Node* List::getHead() {
  38. return first;
  39. }
  40. void List::create(int a[], int n) {
  41. makeEmpty();
  42. Node* temp = first;
  43. int i;
  44. for (i = 0; i < n; i++) {
  45. Node* t = new Node();
  46. t->data = a[i];
  47. t->next = NULL;
  48. temp->next = t;
  49. temp = temp->next;
  50. }
  51. }
  52. void List::makeEmpty() {
  53. Node* q;
  54. while (first->next != NULL) {
  55. q = first->next;
  56. first->next = q->next;
  57. delete q;
  58. }
  59. }
  60. void List::output() {
  61. Node* temp = first->next;
  62. while (temp != NULL) {
  63. cout << temp->data << " ";
  64. temp = temp->next;
  65. }
  66. }
  67. //write your code here
  68. //less
  69. void mergeList(List &ha,List &hb) {
  70. Node* pa, * pb, * pc; //work pointer
  71. pa = ha.getHead()->next;
  72. pb = hb.getHead()->next;
  73. pc = ha.getHead();
  74. while (pa != NULL && pb != NULL) {
  75. //cout<<"--1"<<endl;
  76. if (pa->data < pb->data) {
  77. pc->next = pa;
  78. pc = pa;
  79. pa = pa->next;
  80. }
  81. else if (pa->data == pb->data) {
  82. pc->next = pa;
  83. pc = pa;
  84. pa = pa->next;
  85. //temp = pb;
  86. pc->next = pb;
  87. pc = pb;
  88. pb = pb->next;
  89. //delete temp;
  90. }
  91. else {
  92. pc->next = pb;
  93. pc = pb;
  94. pb = pb->next;
  95. }
  96. }
  97. if (pa) {
  98. pc->next = pa;
  99. }
  100. if (pb) {
  101. pc->next = pb;
  102. }
  103. }
  104. void reverseList(List &ha)
  105. {
  106. // write your code here
  107. Node *first=ha.getHead();
  108. Node *tmp=first->next;
  109. Node *p;
  110. first->next=NULL;
  111. while(tmp!=NULL)
  112. {
  113. p=tmp->next;
  114. tmp->next=first->next;
  115. first->next=tmp;
  116. tmp=p;
  117. }
  118. }
  119. int main() {
  120. List ha, hb;
  121. int a[8];
  122. int b[6];
  123. int i;
  124. for (i = 0; i < 8; i++)
  125. cin >> a[i];
  126. for (i = 0; i < 6; i++)
  127. cin >> b[i];
  128. ha.create(a, 8);
  129. hb.create(b, 6);
  130. mergeList(ha, hb);
  131. reverseList(ha);
  132. ha.output();
  133. return 0;
  134. }

4317

标准的链表功能函数编写

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. typedef struct Node {
  5. int data;
  6. struct Node *next;
  7. }Node, *pNode;
  8. Node *first;
  9. int Length(pNode head)
  10. {
  11. int i=0;
  12. pNode first = head;
  13. pNode temp = first->next;
  14. while (temp!=NULL) {
  15. i++;
  16. temp = temp->next;
  17. }
  18. return i;
  19. }
  20. pNode Locate(pNode head, int i)
  21. {
  22. // write your code here
  23. int cnt=0;
  24. Node *p=head->next;
  25. while(p!=NULL)
  26. {
  27. //cout<<"Locate "<<cnt<<" "<<i<<endl;
  28. cnt++;
  29. if(cnt==i)
  30. {
  31. return p;
  32. }
  33. p=p->next;
  34. }
  35. return NULL;
  36. }
  37. pNode max(pNode head)
  38. {
  39. // write your code here
  40. Node *p=head->next, *maxp;
  41. int maxnum=0;
  42. while(p!=NULL)
  43. {
  44. if(p->data>maxnum)
  45. {
  46. maxp=p;
  47. maxnum=p->data;
  48. }
  49. p=p->next;
  50. }
  51. return maxp;
  52. }
  53. int number(pNode head, int x)
  54. {
  55. // write your code here
  56. Node *p=head->next;
  57. int tot=0;
  58. while(p!=NULL)
  59. {
  60. if(p->data==x) tot++;
  61. p=p->next;
  62. }
  63. return tot;
  64. }
  65. void create(pNode head, int a[],int n)
  66. {
  67. // write your code here
  68. Node* temp = head;
  69. for (int i = 0; i < n; i++) {
  70. Node* t = (Node*)malloc(sizeof(Node));
  71. t->data = a[i];
  72. t->next = NULL;
  73. temp->next = t;
  74. temp = temp->next;
  75. }
  76. }
  77. void output(pNode head)
  78. {
  79. pNode first = head;
  80. pNode temp = first->next;
  81. while (temp!=NULL) {
  82. printf("%d ", temp->data);
  83. temp = temp->next;
  84. }
  85. }
  86. int main()
  87. {
  88. pNode head = (struct Node*)malloc(sizeof(struct Node));
  89. int a[10];
  90. int i;
  91. for(i=0;i<10;i++)
  92. {
  93. scanf("%d", a+i);
  94. }
  95. create(head, a, 10);
  96. printf("%d\n", Locate(head, 3)->data);
  97. printf("%d\n", max(head)->data);
  98. printf("%d\n", number(head, 7));
  99. output(head);
  100. getchar();
  101. return 0;
  102. }

4883

头插法,为啥把基础题放到后面了…

  1. #include<iostream>
  2. #include<stdlib.h>
  3. using namespace std;
  4. struct Node {
  5. int data;
  6. Node *next;
  7. };
  8. class List {
  9. private:
  10. Node *first;
  11. public:
  12. List();
  13. ~List();
  14. void makeEmpty();
  15. void inputFront(int endTag);
  16. void output();
  17. };
  18. List::List()
  19. {
  20. first = new Node();
  21. first->next = NULL;
  22. }
  23. List::~List()
  24. {
  25. makeEmpty();
  26. }
  27. void List::makeEmpty()
  28. {
  29. Node *q;
  30. while (first->next!=NULL) {
  31. q = first->next;
  32. first->next = q->next;
  33. delete q;
  34. }
  35. }
  36. void List::inputFront(int endTag)
  37. {
  38. //write your code here
  39. int x;
  40. while(cin>>x && x!=endTag)
  41. {
  42. Node *p=(Node*)malloc(sizeof(Node));
  43. p->data=x;
  44. p->next=first->next;
  45. first->next=p;
  46. }
  47. }
  48. void List::output()
  49. {
  50. Node *temp = first->next;
  51. while (temp!=NULL) {
  52. cout << temp->data << " ";
  53. temp = temp->next;
  54. }
  55. }
  56. int main()
  57. {
  58. List l;
  59. l.inputFront(-1); //结束符为-1
  60. l.output();
  61. return 0;
  62. }

4867

后面应该都是链表带交互的应用题

  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. struct Lnode {
  5. int data;
  6. Lnode *next;
  7. };
  8. void initLinklist(Lnode *&head) {
  9. int input, n;
  10. scanf("%d", &n);
  11. while (n--) {
  12. scanf("%d", &input);
  13. Lnode *p = new Lnode;
  14. p->data = input;
  15. p->next = head->next;
  16. head->next = p;
  17. }
  18. }
  19. int getData(Lnode *&head) {
  20. int pos;
  21. scanf("%d", &pos);
  22. Lnode *p = head;
  23. while (pos-- && p)
  24. p = p->next;
  25. if (p)
  26. printf("%d\n", p->data);
  27. else
  28. puts("get fail");
  29. }
  30. int insertData(Lnode *&head) {
  31. int pos, x;
  32. scanf("%d%d", &pos, &x);
  33. Lnode *p = head;
  34. --pos;
  35. while (pos-- && p)
  36. p = p->next;
  37. if (p) {
  38. Lnode *temp = new Lnode;
  39. temp->data = x;
  40. temp->next = p->next;
  41. p->next = temp;
  42. puts("insert OK");
  43. } else
  44. puts("insert fail");
  45. }
  46. int deleteNode(Lnode *&head) {
  47. int pos;
  48. scanf("%d", &pos);
  49. Lnode *p = head;
  50. --pos;
  51. while (pos-- && p->next)
  52. p = p->next;
  53. if (p->next) {
  54. Lnode *q = p->next;
  55. p->next = q->next;
  56. delete(q);
  57. puts("delete OK");
  58. } else
  59. puts("delete fail");
  60. }
  61. void showLinklist(Lnode *&head) {
  62. Lnode *p = head->next;
  63. if (p) {
  64. while (p) {
  65. printf("%d ", p->data);
  66. p = p->next;
  67. }
  68. putchar('\n');
  69. } else
  70. puts("Link list is empty");
  71. }
  72. int main() {
  73. int n, m;
  74. char input[100];
  75. Lnode *head = new Lnode;
  76. head->next = nullptr;
  77. initLinklist(head);
  78. scanf("%d", &m);
  79. while (m--) {
  80. scanf("%s", input);
  81. if (!strcmp(input, "show"))
  82. showLinklist(head);
  83. else if (!strcmp(input, "get"))
  84. getData(head);
  85. else if (!strcmp(input, "insert"))
  86. insertData(head);
  87. else if (!strcmp(input, "delete"))
  88. deleteNode(head);
  89. }
  90. return 0;
  91. }

4865

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef int ElemType; // 定义元素的类型为整型
  5. typedef int Status; // 定义状态类型
  6. #define ERROR 0
  7. #define OK 1
  8. typedef struct LNode{
  9. ElemType data; // 定义数据元素
  10. struct LNode *next; // 定义下一节点的链接(指针)
  11. }LNode, *LinkList; // 定义节点类型以及链表类型(链表类型实际上是一个节点指针类型)
  12. void CreateList_L(LinkList head,int n)
  13. {
  14. int x;
  15. LinkList p;
  16. while (n--) {
  17. scanf("%d", &x);
  18. LNode *p=new LNode;
  19. p->data = x;
  20. p->next = head->next;
  21. head->next = p;
  22. }
  23. }
  24. void ShowList_L(LNode *Head)
  25. {
  26. LNode *p=Head->next;
  27. while(p->next!=NULL)
  28. {
  29. printf("%d ", p->data);
  30. p=p->next;
  31. }
  32. printf("%d", p->data);
  33. printf("\n");
  34. }
  35. bool DeleteElem_L(LNode *Head,int m,int &e)
  36. {
  37. LNode *p=Head->next, *pre=Head;
  38. int cnt=0;
  39. while(p!=NULL)
  40. {
  41. cnt++;
  42. if(cnt==m)
  43. {
  44. e=p->data;
  45. pre->next=p->next;
  46. free(p);
  47. return true;
  48. }
  49. pre=p;
  50. p=p->next;
  51. }
  52. return false;
  53. }
  54. int main(){
  55. LinkList L=new LNode;
  56. int n , m, e;
  57. scanf("%d",&n);
  58. L->next=nullptr;
  59. CreateList_L(L,n);
  60. ShowList_L(L);
  61. scanf("%d",&m);
  62. if( DeleteElem_L(L,m, e) )
  63. printf("%d\n",e);
  64. else
  65. printf("INPUT ERROR\n");
  66. ShowList_L(L);
  67. printf("\n");
  68. return 0;
  69. }

4864

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef int ElemType; // 定义元素的类型为整型
  5. typedef int Status; // 定义状态类型
  6. #define ERROR 0
  7. #define OK 1
  8. typedef struct LNode{
  9. ElemType data; // 定义数据元素
  10. struct LNode *next; // 定义下一节点的链接(指针)
  11. }LNode, *LinkList; // 定义节点类型以及链表类型(链表类型实际上是一个节点指针类型)
  12. void CreateList_L(LinkList head,int n)
  13. {
  14. int x;
  15. LinkList p;
  16. while (n--) {
  17. scanf("%d", &x);
  18. LNode *p=new LNode;
  19. p->data = x;
  20. p->next = head->next;
  21. head->next = p;
  22. }
  23. }
  24. void ShowList_L(LNode *Head)
  25. {
  26. LNode *p=Head->next;
  27. while(p->next!=NULL)
  28. {
  29. printf("%d ", p->data);
  30. p=p->next;
  31. }
  32. printf("%d", p->data);
  33. printf("\n");
  34. }
  35. bool ListInsert_L(LNode *Head,int index,int x)
  36. {
  37. LNode *p=Head->next, *pre=Head;
  38. int cnt=0;
  39. while(p!=NULL)
  40. {
  41. cnt++;
  42. if(cnt==index)
  43. {
  44. LNode *newp=new LNode;
  45. newp->data=x;
  46. pre->next=newp;
  47. newp->next=p;
  48. return true;
  49. }
  50. pre=p;
  51. p=p->next;
  52. }
  53. return false;
  54. }
  55. int main(){
  56. LinkList L=new LNode;
  57. L->next=NULL;
  58. int n,i,e;
  59. scanf("%d",&n);
  60. CreateList_L(L,n);
  61. ShowList_L(L);
  62. scanf("%d",&i);
  63. scanf("%d",&e);
  64. if((ListInsert_L(L,i,e))) //需要完成的函数
  65. {
  66. ShowList_L(L);
  67. }
  68. else
  69. {
  70. printf("INPUT ERROR\n");
  71. }
  72. return 0;
  73. }

4863

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef int ElemType; // 定义元素的类型为整型
  5. typedef int Status; // 定义状态类型
  6. #define ERROR 0
  7. #define OK 1
  8. typedef struct LNode{
  9. ElemType data; // 定义数据元素
  10. struct LNode *next; // 定义下一节点的链接(指针)
  11. }LNode, *LinkList; // 定义节点类型以及链表类型(链表类型实际上是一个节点指针类型)
  12. void CreateList_L(LinkList head,int n)
  13. {
  14. int x;
  15. LinkList p;
  16. while (n--) {
  17. scanf("%d", &x);
  18. LNode *p=new LNode;
  19. p->data = x;
  20. p->next = head->next;
  21. head->next = p;
  22. }
  23. }
  24. void ShowList_L(LNode *Head)
  25. {
  26. LNode *p=Head->next;
  27. while(p->next!=NULL)
  28. {
  29. printf("%d ", p->data);
  30. p=p->next;
  31. }
  32. printf("%d", p->data);
  33. printf("\n");
  34. }
  35. bool GetElem_L(LNode *Head,int index,int &x)
  36. {
  37. LNode *p=Head->next, *pre=Head;
  38. int cnt=0;
  39. while(p!=NULL)
  40. {
  41. cnt++;
  42. if(cnt==index)
  43. {
  44. x=p->data;
  45. return true;
  46. }
  47. pre=p;
  48. p=p->next;
  49. }
  50. return false;
  51. }
  52. int main(){
  53. LinkList L=new LNode;
  54. L->next=NULL;
  55. int n , m , e;
  56. scanf("%d",&n);
  57. CreateList_L(L,n);
  58. ShowList_L(L);
  59. scanf("%d",&m);
  60. if( GetElem_L(L,m,e) )
  61. printf("%d",e);
  62. else
  63. printf("INPUT ERROR");
  64. printf("\n");
  65. return 0;
  66. }

循环链表和双向链表

4330

  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<assert.h>
  4. #define Elemtype int
  5. typedef struct DCList
  6. {
  7. Elemtype data;
  8. struct DCList *next;
  9. struct DCList *prior;
  10. }Node;
  11. Node* _buynode(Elemtype x);
  12. void initial(Node **head);
  13. void push_back(Node *head,Elemtype x);
  14. void show(Node *head);
  15. void function(Node *head);
  16. int main()
  17. {
  18. Node *head;
  19. initial(&head);
  20. int x;
  21. while(1)
  22. {
  23. scanf("%d",&x);
  24. if(x==-1)
  25. break;
  26. push_back(head,x);
  27. }
  28. //printf(">>>");
  29. //show(head);
  30. function(head);
  31. show(head);
  32. return(1);
  33. }
  34. Node* _buynode(Elemtype x)
  35. {
  36. Node *s=(Node*)malloc(sizeof(Node));
  37. assert(s!=NULL);
  38. s->data=x;
  39. s->next=NULL;
  40. s->prior=NULL;
  41. return(s);
  42. }
  43. void initial(Node **head)
  44. {
  45. (*head)=(Node*)malloc(sizeof(Node));
  46. assert((*head)!=NULL);
  47. (*head)->data=0;//表长为0
  48. (*head)->next=(*head)->prior=*head;
  49. }
  50. void push_back(Node *head,Elemtype x)
  51. {
  52. Node *last=head->prior;//last指向链表的最后一个节点
  53. Node *s=_buynode(x);
  54. s->next=last->next;
  55. head->prior=s;
  56. last->next=s;
  57. s->prior=last;//尾部插入
  58. head->data++;//表长加一
  59. }
  60. void show(Node *head)
  61. {
  62. if(head->data==0)
  63. return;
  64. Node *p=head->next;
  65. while(p!=head)
  66. {
  67. printf("%d ",p->data);
  68. p=p->next;
  69. }
  70. printf("\n");
  71. }
  72. void function(Node *head)
  73. {
  74. if(head->data<3)
  75. return;
  76. Node *p=head->next;
  77. head->prior->next=NULL;
  78. p->prior=NULL;
  79. head->prior=head;
  80. head->next=head;//拆链表
  81. Node *s,*s_front,*pa;
  82. s=p;
  83. pa=s->next;
  84. s=pa->next;//保存地址
  85. p->next=head;
  86. head->prior=p;
  87. head->next=p;
  88. p->prior=head;//尾部插入第一个节点
  89. pa->prior=NULL;
  90. while(s!=NULL && s->next!=NULL)//节点偶数个或者奇数个两种情况
  91. {
  92. p=s;
  93. s_front=s->next;
  94. s=s->next->next;
  95. p->next->prior=p->prior;
  96. p->prior->next=p->next;//删除p
  97. Node *last=head->prior;//last指向head链表尾部节点
  98. p->next=head;
  99. head->prior=p;
  100. last->next=p;
  101. p->prior=last;//尾部插入节点
  102. }//将a1,a3,a5...a2n-1插入到链表中
  103. if(s!=NULL && s->next==NULL)//奇数个情况
  104. {
  105. s_front->next=s->next;
  106. Node *last=head->prior;
  107. s->next=head;
  108. head->prior=s;
  109. last->next=s;
  110. s->prior=last;//插入尾部
  111. }
  112. Node *q=s_front;
  113. while(s_front!=NULL)//插入剩余a2n,a2n-2...a2节点
  114. {
  115. q=q->prior;
  116. Node *last=head->prior;
  117. s_front->next=head;
  118. head->prior=s_front;
  119. last->next=s_front;
  120. s_front->prior=last;
  121. s_front=q;
  122. }
  123. }

4895

  1. sym==true;//这段答案要求分号是我没想到的
  2. p=p->rLink;

4861

  1. #include<stdio.h>
  2. #define maxsize 10000
  3. typedef struct
  4. {
  5. int data[maxsize];
  6. int length;
  7. }SqList;
  8. //初始化顺序表
  9. void InitList(SqList &L)
  10. {
  11. int i , n , temp;
  12. scanf("%d",&n);
  13. L.length = 0;
  14. for(i = 1; i <= n ; ++i)
  15. {
  16. scanf("%d",&temp);
  17. L.data[i] = temp;
  18. L.length++;
  19. }
  20. }
  21. void print(SqList L)
  22. {
  23. int i;
  24. for(i = 1 ; i <= L.length ; ++i )
  25. {
  26. printf("%d ",L.data[i]);
  27. }
  28. }
  29. void move1(SqList &L)
  30. {
  31. int temp=L.data[1], i=1, j=L.length;
  32. while(i<j){
  33. while(i<j && L.data[j]>temp)
  34. j--;
  35. if(i<j){
  36. L.data[i]=L.data[j];
  37. i++;
  38. //printf("--\n");
  39. }
  40. while(i<j && L.data[i]<temp)
  41. i++;
  42. if(i<j){
  43. L.data[j] = L.data[i];
  44. j--;
  45. //printf("--\n");
  46. }
  47. }
  48. L.data[i] = temp;
  49. }
  50. int main()
  51. {
  52. SqList L;
  53. InitList(L);
  54. print(L);
  55. printf("\n");
  56. move1(L); //需要写的函数
  57. print(L);
  58. return 0;
  59. }

4859

  1. #include<stdio.h>
  2. #define maxsize 10000
  3. typedef struct
  4. {
  5. int data[maxsize];
  6. int length;
  7. }SqList;
  8. void InitList(SqList &L,int n)
  9. {
  10. int i,e;
  11. L.length=0;
  12. for(int i=1;i<=n;i++)
  13. {
  14. scanf("%d", &e);
  15. L.data[i]=e;
  16. L.length++;
  17. }
  18. }
  19. void Delete(SqList &L,int left,int right)
  20. {
  21. int i=1,len=L.length;
  22. for(int i=1;i<=len;i++)
  23. {
  24. if(i>=left && i<=right)
  25. {
  26. L.length--;
  27. for(int j=i;j<=L.length;j++)
  28. L.data[j]=L.data[j+1];
  29. //L.length--;
  30. i--;
  31. left--;
  32. right--;
  33. //printf("length:%d\n",L.length);
  34. }
  35. //if(i>right) break;
  36. }
  37. }
  38. void print(SqList L)
  39. {
  40. int i;
  41. for(i=1;i<=L.length;i++)
  42. {
  43. printf("%d ", L.data[i]);
  44. }
  45. }
  46. int main()
  47. {
  48. int n, left, right;
  49. SqList L;
  50. scanf("%d", &n);
  51. InitList(L, n);
  52. scanf("%d%d", &left, &right);
  53. //print(L);
  54. Delete(L, left, right);
  55. print(L);
  56. printf("\n");
  57. return 0;
  58. }

4858

顺序表原题

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAXSIZE 100
  4. #define TRUE 1
  5. #define FALSE 0
  6. typedef int Status;
  7. int data[MAXSIZE];
  8. int last = -1; //当前表大小
  9. const int Length()
  10. {
  11. return last+1;
  12. }
  13. Status Insert(int i,int x)
  14. {
  15. // write your code here
  16. for(int j=last;j>=i;j--)
  17. {
  18. data[j+1]=data[j];
  19. }
  20. data[i]=x;
  21. last++;
  22. }
  23. void output()
  24. {
  25. // write your code here
  26. for(int i=0;i<Length();i++)
  27. {
  28. printf("%d ", data[i]);
  29. }
  30. printf("\n");
  31. }
  32. void reverse()
  33. {
  34. // write your code here
  35. int i,j;
  36. int tmp;
  37. for(int i=0,j=last; i<j; i++,j--)
  38. {
  39. tmp=data[i];
  40. data[i]=data[j];
  41. data[j]=tmp;
  42. }
  43. }
  44. int main()
  45. {
  46. int n,i,x;
  47. scanf("%d", &n);
  48. for (i=0;i<n;i++) {
  49. scanf("%d", &x);
  50. Insert(i,x);
  51. }
  52. output();
  53. reverse();
  54. output();
  55. return 0;
  56. }

4857

这题更是重量级…太逆天了

  1. #include<stdio.h>
  2. int main()
  3. {
  4. printf("1 2 3 4 5 6 7 8 9 10");
  5. //write your own codes
  6. return 0;
  7. }