| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 379 人关注过本帖
标题:线性表的插入和删除,,遇到麻烦了,望大仙解决,嘿嘿。。。。。
只看楼主 加入收藏
Noll_Nie
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:71
专家分:139
注 册:2011-4-19
结帖率:81.82%
收藏
已结贴  问题点数:30 回复次数:3 
线性表的插入和删除,,遇到麻烦了,望大仙解决,嘿嘿。。。。。
程序代码:
#include <iostream>
#include <stdlib.h>
using namespace std;

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2

#define LIST_INIT_SIZE 10
#define LISTINCREMENT 10

typedef int elemtype;

typedef struct
{
    elemtype *elem;
    int length;
    int list_size;
}sqlist;

//构造空的线性表
elemtype init_list_sq(sqlist &list)
{
    list.elem = (elemtype*)malloc(LIST_INIT_SIZE*sizeof(elemtype));
    if(!list.elem)
       exit(OVERFLOW);
    list.length = 0;
    list.list_size = LIST_INIT_SIZE;
    return OK;
}//


//在表list中第i个位置插入元素e
elemtype list_insert_sq(sqlist &list, int i, elemtype e)
{
    //在顺序表list的第i个位置插入元素e
    //i的合法位置为1到list_length-1
    if(i<1 || i>list.length+1)
        return ERROR;
    if(list.length>=list.list_size)
    {
        elemtype *newbase=(elemtype *)realloc(list.elem, (list.list_size+LISTINCREMENT)*sizeof(elemtype));
        if(!newbase)
            exit(OVERFLOW);
        list.elem = newbase;
        list.list_size += LISTINCREMENT;
    }
    elemtype *q = &list.elem[i-1];
    elemtype *p = NULL;
    for(p=&(list.elem[list.length-1]); p>=q; --p)
    {
        *(p+1) = *p;
    }
    *q = e;
    ++list.length;
    cout<<"插入成功!"<<endl;
    return OK;
}//

//删除list表中第i个元素e
elemtype list_delete_sq(sqlist &list, int i, elemtype &e)
{
    //删除元素的合法位置1<=i&&i<=list.length
    if(i<1&&i>list.length)
        return ERROR;
    elemtype *p = &(list.elem[i-1]);
    e = *p;
    elemtype *q = &list.elem[list.length - 1];
    for(++p; p<=q; ++p)
    {
        *(p-1) = *p;
    }
    --list.length;
    return OK;
}//


int main(int argc, char *argv[])
{
    sqlist sq_list;
    init_list_sq(sq_list);
   
    do
    {
        int i=0;
        int elem = 0;
       cout<<"Input the elem you want (or Input 1111 to exit!): ";
       cin>>elem;
       if(elem==1111)
           break;
       list_insert_sq(sq_list, i, elem);
       ++i;
    }while(TRUE);
    return 0;
}
输入了,没结果,不知道插入成功没,而且添加输出,还是没显示!
搜索更多相关主题的帖子: 线性表 
2011-08-26 10:48
ljw970243
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:43
专家分:105
注 册:2011-8-20
收藏
得分:10 
//typedef int elemtype;  

typedef struct linkList
{
    elemtype *elem;   //这个是做什么用的? 数据?
    linkList * next;  //链表需要一个指向自身的指针.
    int length;
    int list_size;
}sqlist;


[ 本帖最后由 ljw970243 于 2011-8-26 15:42 编辑 ]
2011-08-26 15:38
QQ346957135
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:148
专家分:658
注 册:2011-8-9
收藏
得分:20 
图片附件: 游客没有浏览图片的权限,请 登录注册

修改后的示范如下:
程序代码:
#include <iostream>
#include <stdlib.h>
using namespace std;

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2

#define LIST_INIT_SIZE 10
#define LISTINCREMENT 10

typedef int elemtype;

typedef struct
{
    elemtype *elem;
    int length;
    int list_size;
}sqlist;

//构造空的线性表
elemtype init_list_sq(sqlist &list)
{
    list.elem = (elemtype*)malloc(LIST_INIT_SIZE*sizeof(elemtype));
    if(!list.elem)
       exit(OVERFLOW);
    list.length = 0;
    list.list_size = LIST_INIT_SIZE;
    return OK;
}//


//在表list中第i个位置插入元素e
elemtype list_insert_sq(sqlist &list, int i, elemtype e)
{
    //在顺序表list的第i个位置插入元素e
    //i的合法位置为1到list_length-1
    if(i<1 || i>list.length+1)
        return ERROR;
    if(list.length>=list.list_size)
    {
        elemtype *newbase=(elemtype *)realloc(list.elem, (list.list_size+LISTINCREMENT)*sizeof(elemtype));
        if(!newbase)
            exit(OVERFLOW);
        list.elem = newbase;
        list.list_size += LISTINCREMENT;
    }
    elemtype *q = &list.elem[i-1];
    elemtype *p = NULL;
    for(p=&(list.elem[list.length-1]); p>=q; --p)
    {
        *(p+1) = *p;
    }
    *q = e;
    ++list.length;
    cout<<"插入成功!"<<endl;
    return OK;
}//

//删除list表中第i个元素e
elemtype list_delete_sq(sqlist &list, int i, elemtype &e)
{
    //删除元素的合法位置1<=i&&i<=list.length
    if(i<1&&i>list.length)
        return ERROR;
    elemtype *p = &(list.elem[i-1]);
    e = *p;
    elemtype *q = &list.elem[list.length - 1];
    for(++p; p<=q; ++p)
    {
        *(p-1) = *p;
    }
    --list.length;
    return OK;
}//
void OutPut(sqlist list)
{
    for(int i=0;i<list.length;i++)
    {
        cout<<list.elem[i]<<endl;
    }
}

int main(int argc, char *argv[])
{
    sqlist sq_list;
    init_list_sq(sq_list);
    int i=1;
    do
    {
        
        int elem = 0;
        cout<<"Input the elem you want (or Input 1111 to exit!): ";
        cin>>elem;
        if(elem==1111)
            break;
        list_insert_sq(sq_list, i, elem);
        ++i;
    }while(TRUE);
    OutPut(sq_list);
    return 0;
}

A real warrior never quits.
2011-08-26 18:08
Noll_Nie
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:71
专家分:139
注 册:2011-4-19
收藏
得分:0 
回复 2楼 ljw970243
事实上这是线性表的顺序实现,那个指针就是用来指向数组的,谢谢你的关注,楼下的解决了
2011-08-27 18:35
快速回复:线性表的插入和删除,,遇到麻烦了,望大仙解决,嘿嘿。。。。。
数据加载中...
 
   



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

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