间接寻址


  1. #include<iostream>
  2. using namespace std;
  3. const int Maxsize= 10;
  4. template< class student>
  5. struct Node {
  6. student data;
  7. Node<student> *next;
  8. };
  9. template< class student>
  10. class inadd{
  11. public:
  12. inadd(); //无参构造函数
  13. inadd(student score[], int n); //有参构造函数
  14. ~inadd(); //析构函数
  15. void print(); //遍历操作
  16. student get(int i); //按位查找操作
  17. int Locate(student x); //按值查找操作
  18. void insert(int i,student x); //插入操作
  19. student Delete(int i); //删除操作
  20. bool changeList(int i,student x); //改变某一结点的值 i为节点的位置,x为替换的值
  21. private:
  22. Node<student> *first; //头指针
  23. int length; //结点数量
  24. Node<student> *address[Maxsize]; //结点指针数组
  25. };
  26. template< class student>
  27. inadd<student>::inadd()
  28. {
  29. first= new Node<student>;
  30. first->next= NULL;
  31. }
  32. template< class student>
  33. inadd<student>::inadd(student score[], int n)
  34. {
  35. if (n>Maxsize) throw( "溢出");
  36. Node<student> *s;
  37. first= new Node<student>;
  38. first->next= NULL; //初始化一个空链表
  39. for( int i=n -1;i>= 0;i--)
  40. {
  41. s= new Node<student>;
  42. s->data=score[i]; //为每个数组元素建立一个结点
  43. s->next=first->next;
  44. first->next=s; //将结点s插入头结点之后
  45. }
  46. }
  47. template< class student>
  48. inadd<student>::~inadd() //析构函数
  49. {
  50. Node<student> *q;
  51. while(first!= NULL)
  52. {
  53. q=first;
  54. first=first->next;
  55. delete q;
  56. }
  57. }
  58. template< class student>
  59. void inadd<student>::insert( int i,student x)
  60. {
  61. Node<student>*p,*s; int count;
  62. p=first;count= 0;
  63. while(p!= NULL&&count<i -1)
  64. {
  65. p=p->next;
  66. count++;
  67. }
  68. if(p== NULL) throw "位置非法";
  69. s= new Node<student>;
  70. s->data=x;
  71. s->next=p->next;
  72. p->next=s;
  73. length++;
  74. }
  75. template< class student>
  76. student inadd<student>::Delete( int i)
  77. {
  78. Node<student> *q,*p;
  79. student x;
  80. int count;
  81. p=first;count= 0; //注意P指针要指向头结点
  82. while(p!= NULL&&count<i -1) //此操作目的是找到i-1个结点
  83. {
  84. p=p->next;
  85. count++;
  86. }
  87. if(p== NULL||p->next== NULL) throw "位置"; //结点p不存在或p后继结点不存在
  88. else{
  89. q=p->next;
  90. x=q->data; //暂存被删结点
  91. p->next=q->next;
  92. delete q;
  93. return x;
  94. }
  95. }
  96. template< class student>
  97. student inadd<student>::get( int i)
  98. {
  99. Node<student>*p; int count;
  100. p=first->next;count= 1;
  101. while(p!= NULL&&count<i)
  102. {p=p->next;count++;}
  103. if(p== NULL) throw "位置非法";
  104. else return p->data;
  105. }
  106. template< class student>
  107. int inadd<student>::Locate(student x)
  108. {
  109. Node<student>*p;
  110. int count = 1;
  111. p=first->next;
  112. while(p!= NULL)
  113. {
  114. if(p->data==x) return count;
  115. p=p->next;
  116. count++;
  117. }
  118. return 0;
  119. }
  120. template< class student>
  121. void inadd<student>::print()
  122. {
  123. Node<student>*p;
  124. p=first->next;
  125. while(p!= NULL)
  126. { cout<<p->data<< " ";
  127. p=p->next;
  128. }
  129. }
  130. int main()
  131. {
  132. float score[ 5]={ 88.5, 52.5, 99, 73.5, 98};
  133. inadd< float>Student(score, 5); //创建对象
  134. cout<< "初始数据如下:"<< endl;
  135. Student.print();
  136. cout<< endl<< "在位置5插入成绩95,结果如下:"<< endl;
  137. Student.insert( 5, 95);
  138. Student.print();
  139. cout<< endl<< "在学生2删除成绩为:"<<Student.Delete( 2)<< endl<< "删除后结果如下:"<< endl;
  140. Student.print();
  141. cout<< endl<< "学生2的成绩为:"<<Student.get( 3)<< endl;
  142. cout<< endl<< "成绩99所在位置为:"<<Student.Locate( 99)<< endl;
  143. cout<< "最终数据如下:"<< endl;
  144. Student.print();
  145. cout<< endl;
  146. return 0;
  147. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值