| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1336 人关注过本帖
标题:如何删除元素?
只看楼主 加入收藏
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
结帖率:38.67%
收藏
已结贴  问题点数:10 回复次数:7 
如何删除元素?
#include "stdafx.h"

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define ERROR -1
#define OVERFLOW -2
typedef  int SElemType;
typedef  int Status;

typedef struct
{
    SElemType *base;
    SElemType *top;
    int stacksize;
}SqStack;

Status InitStack(SqStack &S)
{
    int n=0;
    int a=0;
    S.base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType));
    if(!S.base)exit(OVERFLOW);
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    if(!S.base)
    {
        printf("malloc error!\n");
        exit(0);
    }
    printf("please input a number:\n");
    scanf("%d",&n);
    printf("the number is %d\n",n);
    while(n>0)
    {
        scanf("%d",&a);
        *S.top++=a;
        n--;
    }
    return OK;
}
void print(SqStack S)
{
    while(S.base!=S.top)
    {
        printf("%4d",*--S.top);
    }
    printf("\n");
}

Status Push(SqStack &S)
{
    int e;
    if(S.top-S.base>=S.stacksize)
    {
        S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
        if(S.base)exit(OVERFLOW);
        S.top=S.base+S.stacksize;
        S.stacksize+=STACKINCREMENT;
    }
    printf("input the insert number:\n");
    scanf("%d",&e);
    *S.top++=e;
    return OK;
}

Status Pop(SqStack &S)//这个函数如何实现删除元素功能?代码该如何编写?
{
    int e;
    if(S.top==S.base)
    {
        return ERROR;
    }
    printf("input the delete number\n");
    scanf("%d",&e);
    e=*--S.top;
    if(e==*S.top)
    {
        S.top--;
    }
   
    return OK;
}

Status GetTop(SqStack S)
{
    int c;
    if(S.top==S.base)
    {
        printf("error!\n");
    }
    c=*(S.top-1);
    printf("zhanding is %d\n",c);
    return OK;
}
int main(int argc, char* argv[])
{
    SqStack S;
    int c=0;
    InitStack(S);
    print(S);
    Push(S);
    print(S);
    Pop(S);
    print(S);
    GetTop(S);
   
    return 0;
}
搜索更多相关主题的帖子: 删除 元素 
2009-11-04 19:08
viky2003
Rank: 5Rank: 5
等 级:职业侠客
帖 子:375
专家分:383
注 册:2007-4-11
收藏
得分:2 
C++,请去C++那边问。

要练习算法就来http:///!!有挑战哦!!
2009-11-04 20:00
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
收藏
得分:0 
麻烦帮帮忙啊
2009-11-05 00:31
UserYuH
Rank: 12Rank: 12Rank: 12
来 自:毅华
等 级:火箭侠
威 望:8
帖 子:720
专家分:3300
注 册:2009-8-10
收藏
得分:2 
回复 3楼 henji
你发的程序是个类似进栈出栈的问题,没有什么你说的删除元素,你之前发过很多关于链表的贴,都贴些功能较全的程序却问些基础的问题,这贴也同样.要多练些小题,打好基础再看这些程序.

努力—前进—变老—退休—入土
2009-11-05 01:51
ljt0000mf
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:104
专家分:157
注 册:2009-7-4
收藏
得分:2 

楼上很诚恳啊
2009-11-05 07:23
lijm1989
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:珠海
等 级:贵宾
威 望:12
帖 子:675
专家分:2844
注 册:2009-10-14
收藏
得分:2 
程序代码:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define ERROR -1
#define OVERFLOW -2
typedef  int SElemType;
typedef  int Status;

typedef struct
{
    SElemType *base;
    SElemType *top;
    int stacksize;
}SqStack;

Status InitStack(SqStack &S)
{
    int n=0;
    int a=0;
    S.base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType));
    if(!S.base)exit(OVERFLOW);
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    if(!S.base)
    {
        printf("malloc error!\n");
        exit(0);
    }
    printf("please input a number:\n");
    scanf("%d",&n);
    printf("the number is %d\n",n);
    while(n>0)
    {
        scanf("%d",&a);
        *S.top++=a;
        n--;
    }
    return OK;
}
void print(SqStack S)
{
    while(S.base!=S.top)
    {
        printf("%4d",*--S.top);
    }
    printf("\n");
}

Status Push(SqStack &S)
{
    int e;
    if(S.top-S.base>=S.stacksize)
    {
        S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
        if(S.base)exit(OVERFLOW);
        S.top=S.base+S.stacksize;
        S.stacksize+=STACKINCREMENT;
    }
    printf("input the insert number:\n");
    scanf("%d",&e);
    *S.top++=e;
    return OK;
}

Status Pop(SqStack &S)//   你写的代码中读入了要删除的数后进行的是取栈顶的操作,这样肯定不行,我改了下,
{
    int e;
    SElemType *top1=S.top;    // 保留栈顶指针的位置
    if(S.top==S.base) 
    {
        return ERROR;
    }
    printf("input the delete number\n");
    scanf("%d",&e);
    while(e!=*(--S.top));    //  寻找要删除的数的位置
        S.top++;
    while(S.top!=top1)  
         *(S.top-1)=*(S.top++);     //  移位删除···
    S.top--;
    S.stacksize -= STACKINCREMENT;  //  改变栈的大小
    return OK;
}

Status GetTop(SqStack S)
{
    int c;
    if(S.top==S.base)
    {
        printf("error!\n");
    }
    c=*(S.top-1);
    printf("zhanding is %d\n",c);
    return OK;
}
int main(int argc, char* argv[])
{
    SqStack S;
    int c=0;
    InitStack(S);
    print(S);
    Push(S);
    print(S);
    Pop(S);
    print(S);
    GetTop(S);
    
    return 0;
}
2009-11-05 13:22
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
收藏
得分:0 
while(e!=*(--S.top));   
        S.top++;//这个S.top已经是栈顶了?还能在加吗?
    while(S.top!=top1)  
         *(S.top-1)=*(S.top++);        
    S.top--;
    S.stacksize -= STACKINCREMENT;
2009-11-05 14:49
lijm1989
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:珠海
等 级:贵宾
威 望:12
帖 子:675
专家分:2844
注 册:2009-10-14
收藏
得分:0 
while(e!=*(--S.top)); 注意这个while ,是不断地找栈中与e相同的数,不相同的话,继续--S.top,经过这个while后(指针所指向的已经不是栈顶了),LZ编译试试···应该是你想要的··不过好像一般的栈的操作没有删除栈中某个元素的操作哦··
2009-11-05 14:58
快速回复:如何删除元素?
数据加载中...
 
   



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

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