| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 409 人关注过本帖
标题:为什么不能实现功能?
只看楼主 加入收藏
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
结帖率:38.67%
收藏
 问题点数:0 回复次数:5 
为什么不能实现功能?
#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
zhaoguoge
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:46
专家分:121
注 册:2009-7-8
收藏
得分:0 
一点注释都没……习惯不好!
2009-10-15 19:01
zhaoguoge
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:46
专家分:121
注 册:2009-7-8
收藏
得分:0 
/* 用链表实现学生成绩信息的管理  */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stud_node{
     int    num;
     char  name[20];
     int    score;
     struct stud_node *next;
};
struct stud_node * Create_Stu_Doc();  /* 新建链表 */
struct stud_node * InsertDoc(struct stud_node * head, struct stud_node *stud); /* 插入 */
struct stud_node * DeleteDoc(struct stud_node * head, int num);  /* 删除 */
void Print_Stu_Doc(struct stud_node * head);  /* 遍历 */
 
int main(void)
{
  struct stud_node *head,*p;
  int choice, num, score;
  char name[20];
  int size = sizeof(struct stud_node);
 
  do{
    printf("1:Create 2:Insert 3:Delete 4:Print 0:Exit\n");
    scanf("%d", &choice);
    switch(choice){
    case 1:  
           head=Create_Stu_Doc();
           break;
    case 2:  
           printf("Input num,name and score:\n");
           scanf("%d%s%d", &num,name, &score);
           p = (struct stud_node *) malloc(size);
           p->num = num;
           strcpy(p->name, name);
           p->score = score;  
           head=InsertDoc(head, p);
           break;
    case 3:  
           printf("Input num:\n");
           scanf("%d", &num);
           head = DeleteDoc(head, num);
           break;
         case 4:  
           Print_Stu_Doc(head);
           break;
         case 0:
          break;
    }
  }while(choice != 0);
 
  return 0;
}
 
/*新建链表*/
struct stud_node * Create_Stu_Doc()   
{  
    struct stud_node * head,*p;
    int num,score;
    char  name[20];
    int size = sizeof(struct stud_node);
 
    head=NULL;
    printf("Input num,name and score:\n");
    scanf("%d%s%d", &num,name, &score);
    while(num != 0){
       p = (struct stud_node *) malloc(size);
       p->num = num;
       strcpy(p->name, name);
       p->score = score;  
       head=InsertDoc(head, p);    /* 调用插入函数 */
       scanf("%d%s%d", &num, name, &score);
   }
   return head;
}
 
/* 插入操作 */
struct stud_node * InsertDoc(struct stud_node * head, struct stud_node *stud)   
{   
   struct stud_node *ptr ,*ptr1, *ptr2;
 
    ptr2 = head;  
    ptr = stud;         /* ptr指向待插入的新的学生记录结点 */
    /* 原链表为空时的插入 */  
    if(head == NULL){  
        head = ptr;         /* 新插入结点成为头结点 */
        head->next = NULL;
    }
    else{             /* 原链表不为空时的插入 */
         while((ptr->num > ptr2->num) && (ptr2->next != NULL)){
            ptr1 = ptr2;     /* ptr1, ptr2各后移一个结点 */
            ptr2 = ptr2->next;
         }
         if(ptr->num <= ptr2->num){     /* 在ptr1与ptr2之间插入新结点 */
            if(head==ptr2)  head = ptr;
            else ptr1->next = ptr;
            ptr->next = ptr2;
        }
        else{                /* 新插入结点成为尾结点 */
            ptr2->next =ptr;
            ptr->next = NULL;  
        }  
    }
    return head;
}
 
/* 删除操作 */
struct stud_node * DeleteDoc(struct stud_node * head, int num)   
{  
    struct stud_node *ptr1, *ptr2;  
 
    /* 要被删除结点为表头结点 */
    if(head!=NULL && head->num == num){   
         ptr2 = head;
         head = head->next;
         free(ptr2);
    }
    if(head == NULL)  /*链表空 */
        return NULL;
     /* 要被删除结点为非表头结点  */
     ptr1 = head;
     ptr2 = head->next; /*从表头的下一个结点搜索所有符合删除要求的结点 */
     while(ptr2!=NULL){
         if(ptr2->num == num){     /* ptr2所指结点符合删除要求 */
             ptr1->next = ptr2->next;
             free(ptr2);  
         }
         else  
             ptr1 = ptr2;       /* ptr1后移一个结点 */
         ptr2 = ptr1->next;     /* ptr2指向ptr1的后一个结点 */
     }
     return head;
}
 
/*遍历操作*/
void Print_Stu_Doc(struct stud_node * head)  
{ struct stud_node * ptr;
   if(head == NULL){
       printf("\nNo Records\n");
       return;
    }
    printf("\nThe Students' Records Are: \n");
    printf("    Num   Name   Score\n");
    for(ptr = head; ptr; ptr = ptr->next)
        printf("%8d%20s%6d \n", ptr->num, ptr->name, ptr->score);
}
拿去学习用!
2009-10-15 19:02
hzyzxj
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:76
专家分:168
注 册:2009-6-14
收藏
得分:0 
兄弟 ,看你的功底 应该能找出错误的啊。
2009-10-15 19:47
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.015804 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved