| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 975 人关注过本帖
标题:顺序表的实现
只看楼主 加入收藏
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
快速回复:顺序表的实现
数据加载中...
 
   



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

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