C++中如何建立一个顺序表(2)

2019-01-26 17:08

4. for(i=1;iListLen;i++) 5. {

6. cout<<\ 7. cout<<\ 8. cout<<\

9. cout<<\ 10. } 11. }

顺序表操作完整示例:

基本上就是把上面的函数放到一块,集中展示了一下功能,代码有些长,请耐心阅读^.^

[cpp] view plaincopyprint?

1. #include 2. #include 3. using namespace std;

4. #define MAXLEN 100 //定义顺序表的最大长度 5. /**************顺序表的定义部分*****************/ 6. struct DATA 7. {

8. string key; //结点的关键字 9. string name; 10. int age; 11. };

12. struct SLType //定义顺序表结构 13. {

14. DATA ListData[MAXLEN+1];//保存顺序表的结构数组 15. int ListLen; //顺序表已存结点的数量 16. };

17. /************顺序表的初始化函数*****************/

18. void SLInit(SLType * SL) //初始化顺序表 19. {

20. SL->ListLen=0; 21. }

22. /***********计算线性表的长度*******************/ 23. int SLLenght(SLType *SL) 24. {

25. return(SL->ListLen); //返回顺序表的元素数量 26. }

27. /*********插入结点*******************************/ 28. int SLInsert(SLType *SL,int n,DATA data) 29. { 30. int i;

31. if(SL->ListLen>=MAXLEN) //顺序表结点数量已超过最大数量 32. {

33. cout<<\顺序表已满,不能插入结点!\ 34. return 0; //返回0表示插入不成功 35. }

36. if(n<1||n>SL->ListLen) //插入结点的序号不合法 37. {

38. cout<<\插入序号错误!\ 39. return 0; 40. }

41. for(i=SL->ListLen;i>=n;i--) //将顺序表中的数据向后移动 42. {

43. SL->ListData[i+1]=SL->ListData[i]; 44. }

45. SL->ListData[n]=data; 46. SL->ListLen++;

47. return 1; //成功插入,返回1 48. }

49. /***********************追加结点*************************/ 50. int SLAdd(SLType * SL,DATA data)

51. {

52. if(SL->ListLen>=MAXLEN) 53. {

54. cout<<\顺序表已满,不能再添加结点了!\ 55. return 0; 56. }

57. SL->ListData[++SL->ListLen]=data; 58. return 1; 59. }

60. /***********************删除结点*************************/ 61. int SLDelete(SLType *SL,int n) //删除顺序表中的数据元素 62. { 63. int i;

64. if(n<1||n>SL->ListLen) //删除结点的序号不合法 65. {

66. cout<<\删除序号错误!\ 67. return 0; 68. }

69. for(i=n;iListLen;i++)//将顺序表中的数据向前移动 70. {

71. SL->ListData[i]=SL->ListData[i+1]; 72. }

73. SL->ListLen--; //顺序表元素数量减1 74. return 1; //成功删除返回1 75. }

76. /*******************按照序号查找结点********************/

77. DATA * SLFindByNum(SLType *SL,int n)//根据序号返回数据元素 78. {

79. if(n<1||n>SL->ListLen) //查询结点的序号不合法 80. {

81. cout<<\查询序号错误!\ 82. return 0; 83. }

84. return &(SL->ListData[n]); 85. }

86. /*******************按照关键字查找结点********************/

87. DATA *SLFindByCont(SLType * SL,string name)//按关键字查询结点 88. { 89. int i;

90. for(i=1;i<=SL->ListLen;i++) 91. {

92. if(SL->ListData[i].name==name)//如果找到结点 93. {

94. return &(SL->ListData[i]); 95. } 96. }

97. return 0; //在整个表中都没有找到,返回0 98. }

99. /*******************显示所有的结点********************/ 100. void SLALL(SLType *SL) 101. { 102. int i;

103. for(i=1;i<=SL->ListLen;i++) 104. {

105. cout<<\

<ListData[i].age<

111. SLType SL; //定义顺序表变量

112. DATA data; //定义结点保存数据类型变量 113. DATA *pdata;//定义指向结点的指针变量 114. string name;

115. cout<<\顺序表操作演示:\

116. SLInit(&SL);//初始化顺序表 117. do

118. { //循环添加结点数据

119. cout<<\请输入要添加的结点(学号 姓名 年龄):\ 120. cin>>data.key>>data.name>>data.age; 121. if(data.age) //若年龄不为0 122. {

123. if(!SLAdd(&SL,data))//若添加结点失败 124. {

125. break; //退出循环 126. } 127. }else 128. { 129. break; 130. } 131. }while(1);

132. cout<<\顺序表中的结点顺序为:\ 133. SLALL(&SL); //显示所有的结点 134. cout<<\请输入要取出的结点序号:\ 135. cin>>i;

136. pdata=SLFindByNum(&SL,i);//按序号查找结点 137. if(pdata) 138. {

139. cout<<\第\个结点为:

key:\ 140. }

141. cout<<\请输入要查找的姓名:\ 142. cin>>name;

143. pdata=SLFindByCont(&SL,name); 144. if(pdata) 145. {

146. cout<<\


C++中如何建立一个顺序表(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:高一下学期月考试题(必修二)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: