标题:顺序表的实现
只看楼主
ghbibi
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-11-15
 问题点数:0 回复次数:1 
顺序表的实现
//还有很多缺陷,但学顺序表的话,知道这些就足够了
#include <cstdlib>
#include <iostream>

using namespace std;
#define list_init_size 100
#define listincreament 10

typedef struct
{
        int *elem;
        int length;
        int listsize;
}sqList;  //顺序存储结构表

void initSqList (sqList &L)
{
    L.elem = (int*)malloc (list_init_size *sizeof(int));
    if (!L.elem) exit(0);
    L.length = 0;
    L.listsize = list_init_size;
}

void insertSqList (sqList &L,int e)   // 插入元素,插入并没有排序
{
    if (L.length >= L.listsize)
    {               
       int* newbase = (int*)realloc(L.elem, (L.listsize + listincreament)*sizeof(int));
       if (!newbase) exit(0);
       L.elem = newbase;
       L.listsize += listincreament;         
    }
    L.elem[L.length] = e;
    ++L.length;
}
bool findSqList( sqList L, int e)
{
     bool b = false;
     for (int i = 0; i < L.length; i ++, L.elem ++)
     {
         if (*(L.elem) == e) b = true;
     }
     return b;
 }

void delSqList (sqList &L, int e) // 删除元素,对于重复的元素只能删一个 ,不能删除不
{
     if (findSqList (L, e))
{
    int *p;
    int i = 0;
   
    p = L.elem;
    while (*p != e)
    {
          i++;
          p++;
    }
    int *q;
    q = L.elem + i;
    for (; i < L.length; ++i, p++) *p = *(p+1);
    --L.length;
}  
else
cout << e << " is not the number of the list." << endl;
}
void printSqList (sqList L)
{
     int *p;
     p = L.elem;
     for(int i = 0; i < L.length; i++)
     {
       cout << *p << '\t';
       p++;
       }
       cout << endl;
 }
int main(int argc, char *argv[])
{
    sqList L;
    int n, m;
   
    initSqList(L);
    cout << "please input the number you want to input(-1 with end) :"<< endl;
    cin >> n;
    while ( n != -1)
    {
          insertSqList (L, n);
          cin >> n;
    }
    cout << "The list of number is : " << endl;
    printSqList(L);
    cout << "which of the number in the list do you want to delete(-1 with end): " << endl;
    cin >> n;
    while ( n != -1)
    {
          delSqList (L, n);
          printSqList(L);
          cout << "which of the number in the list do you want to delete(-1 with end): " << endl;
          cin >> n;
    }
    cout << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
搜索更多相关主题的帖子: 顺序表 int list elem init 
2007-12-28 15:25
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
得分:0 

到底是什么存储

倚天照海花无数,流水高山心自知。
2007-12-28 21:32



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-194826-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.667193 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved