| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 409 人关注过本帖
标题:为什么不能实现功能?
取消只看楼主 加入收藏
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
结帖率:38.67%
收藏
 问题点数:0 回复次数:2 
为什么不能实现功能?
#include "stdio.h"
#include "stdlib.h"
#define OK 1
#define NULL 0
#define ERROR -1
typedef int ElemType;
typedef int Status;
typedef struct node
{
    int data;
    struct node *prior;
    struct node *next;
}Lnode,*DuLinklist;
void creat(DuLinklist &L,int m)
{
    DuLinklist p,q;
   
    int i;
    L=(DuLinklist)malloc(sizeof(Lnode));
    L->next=NULL;
    q=L;
    for(i=1;i<=m;i++)
    {
        p=(DuLinklist)malloc(sizeof(Lnode));
        printf("input number:\n");
        scanf("%d",&p->data);
        p->next=NULL;
        p->prior=q;
        q->next=p;
        q=p;
    }
   
}

void print(DuLinklist L)
{
    DuLinklist p;
    p=L->next;
    while(p)
    {
        printf("%5d",p->data);
        p=p->next;
    }
    printf("\n");
}

Status insert(DuLinklist &L,int i,ElemType e)//这个插入节点的功能为什么不能运行?代码该如何修改?
{
    DuLinklist p;
    DuLinklist s;
    p=L;
    int j=0;
    while(p&&j<i-1)
    {
        p=p->next;
        ++j;
    }
    if(!p||j>i-1)
    {
        return ERROR;
    }
    s=(DuLinklist)malloc(sizeof(Lnode));
    s->data=e;
    s->prior=p->prior;
    p->prior->next=s;
    s->next=p;
    p->prior=s;
    return OK;
}


int main(int argc, char* argv[])
{
    DuLinklist L;
    int i=0;
    int e=0;
    creat(L,5);
    print(L);
    printf("\n");
    printf("input i:\n");
    scanf("%d",&i);
    printf("input e:\n");
    scanf("%d",&e);
    insert(L,i,e);
    print(L);
    return 0;
}
搜索更多相关主题的帖子: include number 
2009-10-15 18:55
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
收藏
得分:0 
恩,找出来了,但是当我输入1,2,3,4,5,插入1到最后的一个节点时,程序还是只能输出1,2,3,4,5而不是1,2,3,4,5,1?
2009-10-15 20:54
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
收藏
得分:0 
#include "stdio.h"
#include "stdlib.h"
#define OK 1
#define NULL 0
#define ERROR -1
typedef int ElemType;
typedef int Status;
typedef struct node
{
    int data;
    struct node *prior;
    struct node *next;
}Lnode,*DuLinklist;
 
void creat(DuLinklist &L,int m)
{
    DuLinklist p,q;
     
    int i;
    L=(DuLinklist)malloc(sizeof(Lnode));
    L->next=NULL;
    q=L;
    for(i=1;i<=m;i++)
    {
        p=(DuLinklist)malloc(sizeof(Lnode));
        printf("input number:\n");
        scanf("%d",&p->data);
        p->next=NULL;
        p->prior=q;
        q->next=p;
        q=p;
    }
}
 
void print(DuLinklist L)
{
    DuLinklist p;
    p=L->next;
    while(p)
    {
        printf("%5d",p->data);
        p=p->next;
    }
    printf("\n");
}
 
Status insert(DuLinklist &L,int i,ElemType e)
{
    DuLinklist p;
    DuLinklist s;
    p=L->next;
    int j=0;
    int m=0;
    while(p&&j<i-1)
    {
        p=p->next;
        ++j;
    }
    if(!p||j>i-1)
    {
        return ERROR;
    }
    s=(DuLinklist)malloc(sizeof(Lnode));
    s->data=e;
    s->prior=p->prior;
    p->prior->next=s;
    s->next=p;
    p->prior=s;
 
    return OK;
}
 
Status Delete(DuLinklist &L,int i,ElemType e) //这里不能插入尾节点,只能插入第一个节点?代码该如何修改?
{
    DuLinklist p;
    DuLinklist q;
    p=L;
    int j=0;
    while(p->next&&j<i-1)
    {
        p=p->next;
        ++j;
    }
    if(!(p->next)||j>i-1)
    {
        return ERROR;
    }
    return OK;
}
 
int main(int argc, char* argv[])
{
    DuLinklist L;
    int i=0;
    int e=0;
    creat(L,5);
    print(L);
    printf("\n");
    printf("input i:\n");
    scanf("%d",&i);
    printf("input e:\n");
    scanf("%d",&e);
    insert(L,i,e);
    print(L);
    return 0;
}
2009-10-15 21:07
快速回复:为什么不能实现功能?
数据加载中...
 
   



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

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