请选择 进入手机版 | 继续访问电脑版
查看: 520|回复: 0

[云计算] 顺序表的实现

3万

主题

3万

帖子

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
100167
发表于 2016-8-14 17:58:24
代码好像有问题,还请大神指正
  1. class LinkNode
  2. {
  3. friend class SingleList;
  4. private:
  5. int data;
  6. LinkNode * link;
  7. LinkNode(LinkNode *ptr = NULL)
  8. {
  9. link = ptr;
  10. }
  11. LinkNode(const int & item ,LinkNode *ptr = NULL)
  12. {
  13. data = item;
  14. link = ptr;
  15. }
  16. };
复制代码
  1. #include <iostream>
  2. #include "LinkNode.h"
  3. using namespace std;
  4. class SingleList
  5. {
  6. protected:
  7. LinkNode *first;
  8. public:
  9. SingleList()
  10. {
  11. first = new LinkNode;
  12. }
  13. SingleList(const int x)
  14. {
  15. first = new LinkNode(x);
  16. }
  17. SingleList(SingleList & L);
  18. ~SingleList()
  19. {
  20. makeEmpty();
  21. }
  22. void makeEmpty();
  23. int Length()const;
  24. LinkNode *getHead()const
  25. {
  26. return first->link;
  27. }
  28. int Search(int & x)const;
  29. LinkNode *Locate(int i)const;
  30. int getData(int i)const;
  31. void setData(int i,int & x);
  32. bool Insert(int i,int & x);
  33. bool Remove(int i,int & x);
  34. void Sort();
  35. bool isEmpty()
  36. {
  37. return (first->link==NULL)?true:false;
  38. }
  39. void input();
  40. void output();
  41. void makeChoice(char choice);
  42. };
复制代码
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include "SingleList.h"
  4. using namespace std;
  5. SingleList::SingleList(SingleList & L)
  6. {
  7. first = new LinkNode;
  8. LinkNode *s_ptr = L.getHead();
  9. LinkNode *s_dtr = first;
  10. while(s_ptr)
  11. {
  12. LinkNode *newNode = new LinkNode(s_ptr->data);
  13. newNode->link = s_dtr->link;
  14. s_dtr->link = newNode;
  15. s_ptr = s_ptr->link;
  16. s_dtr = s_dtr->link;
  17. }
  18. }
  19. void SingleList::makeEmpty()
  20. {
  21. LinkNode * q;
  22. while(first->link)
  23. {
  24. q = first->link;
  25. first->link = q->link;
  26. delete q;
  27. }
  28. }
  29. int SingleList::Length()const
  30. {
  31. LinkNode *current = first;
  32. int n=0;
  33. while(current->link)
  34. {
  35. current = current->link;
  36. n++;
  37. }
  38. return n;
  39. }
  40. int SingleList::Search(int & x)const
  41. {
  42. LinkNode *current = first->link;
  43. for(int i =1; i<=Length(); i++)
  44. {
  45. if(current->data==x)
  46. {
  47. return i;
  48. }
  49. else
  50. {
  51. current = current->link;
  52. }
  53. }
  54. return -1;
  55. }
  56. bool SingleList::Insert(int i,int & x)
  57. {
  58. if(LinkNode * current = Locate(i) )
  59. {
  60. LinkNode *newNode = new LinkNode(x);
  61. newNode->link = current->link;
  62. current->link = newNode;
  63. return true;
  64. }
  65. else
  66. {
  67. return false;
  68. }
  69. }
  70. bool SingleList::Remove(int i,int & x)
  71. {
  72. LinkNode *current = Locate(i-1);
  73. LinkNode *q = current->link;
  74. current->link = current->link->link;
  75. delete q;
  76. return true;
  77. }
  78. LinkNode *SingleList::Locate(int i)const
  79. {
  80. LinkNode *current = first;
  81. for(int n=1; n<=i; n++)
  82. {
  83. if(current->link)
  84. {
  85. current = current->link;
  86. }
  87. else
  88. {
  89. break;
  90. }
  91. }
  92. return current;
  93. }
  94. int SingleList::getData(int i)const
  95. {
  96. LinkNode *current = Locate(i);
  97. return current->data;
  98. }
  99. void SingleList::Sort()
  100. {
  101. }
  102. void SingleList::input()
  103. {
  104. cout <<"Denfine the content of Your List"<<endl;
  105. int x;
  106. LinkNode *current = first;
  107. while(cin >> x)
  108. {
  109. LinkNode *newNode = new LinkNode(x);
  110. newNode->link = current->link;
  111. current->link = newNode;
  112. current = current->link;
  113. }
  114. cout<<"List created."<<endl;
  115. }
  116. void SingleList::output()
  117. {
  118. LinkNode *current = first->link;
  119. for(int i=1; i<=Length(); i++)
  120. {
  121. if(current)
  122. {
  123. cout<<current->data<<endl;
  124. current = current->link;
  125. }
  126. else
  127. {
  128. break;
  129. }
  130. }
  131. }
  132. void SingleList::makeChoice(char choice)
  133. {
  134. int i,x;
  135. if(choice == '1')
  136. {
  137. input();
  138. }
  139. else if(choice == '2')
  140. {
  141. cout<<"Put in the i and x:"<<endl;
  142. cin>>i>>x;
  143. Insert(i,x);
  144. }
  145. else if(choice == '3')
  146. {
  147. cout<<"Put in the i and x:"<<endl;
  148. cin>>i>>x;
  149. Remove(i,x);
  150. }
  151. else if(choice == '4')
  152. {
  153. cout<<"Put in the x:"<<endl;
  154. cin>>x;
  155. Search(x);
  156. }
  157. else if(choice == '5')
  158. {
  159. output();
  160. }
  161. else
  162. {
  163. cout<<"Wrong!"<<endl;
  164. }
  165. }
复制代码


回复

使用道具 举报