Chinaunix首页 | 论坛 | 博客
  • 博客访问: 20994
  • 博文数量: 1
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 27
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-24 16:12
文章分类

全部博文(1)

文章存档

2013年(1)

我的朋友

分类: C/C++

2013-10-09 17:43:24

链表基础知识:


链表基本操作:
1 初始化链接,并赋值
2 遍历
3 销毁
4 清空
5 判断是否为空
6 获取长度
7 获取索引上的元素
8 定位某一元素,判断某一元素是否在链接中
9 获取某一元素的前驱

点击(此处)折叠或打开

  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;

  4. #define LIST_INIT_SIZE 10
  5. #define LIST_INCREMENT 2

  6. typedef struct suquenceList{
  7.   int *elem;
  8.   int size;
  9.   int length;
  10. }SqList;

  11. class SequenceList {
  12.   public :
  13.     int initSequenceList(SqList *L,int n);
  14.     void traversalSequenceList(SqList *L);
  15.     void destroySequenceList(SqList *L);
  16.     void clearSequenceList(SqList *L);
  17.     int isEmptySequenceList(SqList *L);
  18.     int getSequenceListLength(SqList *L);
  19.     int getSequenceListElem(SqList *L,int i, int *e);
  20.     int locateSequenceListElem(SqList *L,int e);
  21.     int priorSequenceListElem(SqList *L,int e,int *p);
  22. };

  23. int SequenceList::initSequenceList(SqList *L,int n){
  24.   L->elem = (int *)malloc(n*sizeof(int*));
  25.   if(!L->elem)
  26.     return 1;
  27.   L->length = 0;
  28.   L->size = n;
  29.   int i;
  30.   cout << "initSequenceList: pls input the numbers to set in sequenceFile "
  31.        << endl;
  32.   for(i=0;i<n;i++){
  33.     scanf("%d",L->elem+i);
  34.     L->length++;
  35.   }
  36.   if(L->length==n)
  37.     cout << "initSequenceList success";
  38.   else
  39.     cout << "initSequenceList failed";
  40.   cout << endl;
  41.   return 0;
  42. }

  43. void SequenceList::traversalSequenceList(SqList *L){
  44.   int i;
  45.   cout << "traversalSequenceList: "
  46.        << endl;
  47.   for(i=0;i<L->length;i++){
  48.     cout << L->elem[i]
  49.          << endl;
  50.   }
  51. }

  52. void SequenceList::destroySequenceList(SqList *L){
  53.   free(L->elem);
  54.   L->elem=NULL;
  55.   L->length=0;
  56.   L->size=0;
  57.   if(L->elem==NULL && L->length==0 && L->size==0){
  58.     cout << "destroySequenceList success"
  59.          << endl;
  60.   }
  61. }

  62. void SequenceList::clearSequenceList(SqList *L){
  63.   cout << "clearSequenceList: "
  64.        << endl;
  65.   L->length = 0;
  66. }

  67. int SequenceList::isEmptySequenceList(SqList *L){
  68.   cout << "isEmptySequenceList: "
  69.        << endl;
  70.   if(L->length==0){
  71.     cout << "SequenceList is empty"
  72.          << endl;
  73.     return 0;
  74.   }else{
  75.     cout << "SequenceList is not empty"
  76.          << endl;
  77.     return 1;
  78.   }
  79. }

  80. int SequenceList::getSequenceListLength(SqList *L){
  81.   cout << "getSequenceListLength : "
  82.        << L->length
  83.        << endl;
  84.   return L->length;
  85. }

  86. int SequenceList::getSequenceListElem(SqList *L,int i,int *e){
  87.   if( i<0 || i>L->length ){
  88.     cout << "input index wrong"
  89.          << endl;
  90.     return 1;
  91.   }
  92.   *e=*(L->elem+i-1);
  93.   return 0;
  94. }

  95. int SequenceList::locateSequenceListElem(SqList *L,int e){
  96.   int i = -1;
  97.   for(i=0;i<L->length;i++){
  98.     if(L->elem[i]==e){
  99.       break;
  100.     }
  101.   }
  102.   if(i<0 || i>=L->length){
  103.     cout << "locateSequenceListElem : index is wrong "
  104.          << i
  105.          << endl;
  106.     return -1;
  107.   }
  108.   cout << "locateSequenceListElem : index is "
  109.        << i
  110.        << endl;
  111.   return i;
  112. }

  113. int SequenceList::priorSequenceListElem(SqList *L,int w,int *p){

  114. }


  115. int main(){
  116.   SequenceList sql;
  117.   int n;
  118.   cout << "pls input the sequenceFile's size; "
  119.        << endl;
  120.   scanf("%d",&n);
  121.   //SqList *L;
  122.   SqList *L=(SqList*)malloc(sizeof(SqList));
  123.   sql.initSequenceList(L,n);
  124.   sql.isEmptySequenceList(L);
  125.   sql.getSequenceListLength(L);
  126.   sql.traversalSequenceList(L);
  127.   cout << "pls input the wanted Index : "
  128.        << endl;
  129.   int i;
  130.   int *j = (int*)malloc(sizeof(int));
  131.   scanf("%d",&i);
  132.   sql.getSequenceListElem(L,i,j);
  133.   cout << "L->elem[index] is "
  134.        << *j
  135.        << endl;
  136.   free(j);
  137.   cout << "pls input the wanted elem"
  138.        << endl;
  139.   scanf("%d",&i);
  140.   sql.locateSequenceListElem(L,i);
  141.   sql.clearSequenceList(L);
  142.   sql.isEmptySequenceList(L);
  143.   sql.getSequenceListLength(L);
  144.   sql.traversalSequenceList(L);
  145.   sql.destroySequenceList(L);
  146.   free(L);
  147.   return 0;
  148. }

阅读(1463) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:没有了

给主人留下些什么吧!~~