注册 登录
编程论坛 C语言论坛

Oj上runtime error怎么改,求!

浮烟醉 发布于 2019-09-28 12:41, 2069 次点击
程序代码:
#include<stdbool.h>
#include<stdio.h>
#include<stdlib.h>
typedef int ElementType;
typedef struct LNode *PtrToLNode;
struct LNode {
    ElementType Data;
    PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;
List MakeEmpty()
{
   
    List L;

 
    L = (List)malloc(sizeof(struct LNode));
    L->Next = NULL;

 
    return L;
}

 
int Length(List L)
{
    Position p;
    int cnt=0;
    p=L;
    while(p!=NULL)
    {
        p=p->Next;
        cnt++;
        
    }
    return cnt;
}
/* 带头结点的插入 */

bool Insert( List L, ElementType X, int i )
{ /* 这里默认L有头结点 */
    Position tmp, pre;
    int cnt=0;
    pre=L;
    while(pre && cnt<i-1)
    {
        pre=pre->Next;
        cnt++;
      }  
              
    if ( pre==NULL ||cnt!=i-1) {
        
        return false;
    }
    else {
        tmp = (Position)malloc(sizeof(struct LNode)); /* 申请、填装结点 */
        tmp->Data = X;
        tmp->Next = pre->Next;
        pre->Next = tmp;
        return true;
    }
}

 
/* 带头结点的删除 */
bool Delete( List L, int i )
{ /* 这里默认L有头结点 */
    Position tmp, pre;
    int cnt=0;
    pre=L;
    while(pre && cnt<=i-1)
    {    pre=pre->Next;
        cnt++;
        
        
    if ( pre==NULL || cnt!=i-1||pre->Next==NULL) {
        return false;
    }
    else {
        tmp=pre->Next;
        pre->Next = tmp->Next;
        free(tmp);
        
    }
    return true;
}
}
int main(int argc,char** argv){
    List L=MakeEmpty();
    int n,op;
    int i;
    ElementType x;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&op);
        switch(op)
        {
            case 1:scanf("%d %d",&x,&i);
            Insert(L,x,i);
            break;
            case 2:scanf("%d",&i);
            Delete(L,i);
            break;
        }   
    }
    while(L!=NULL)
       {
        L=L->Next;
        printf("%d ",L->Data);
        }
        return 0;
}
1 回复
#2
燕小六2019-10-09 10:56
题目链接发下
1