Big Program Small problems

I have attached a large program that I am working on, which I hope one day
will act much like a business basic program (similar to gw-basic).  I am
experiencing some problems with my app crashing occasionally under 95,98 &
NT.  I think it is something to do with either my linked lists (for the line
numbers) or m pointers... anyways, I look for your advise.

Here it is...

--------[CUT]--------

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>

/* globals */
char cursor[]="->";

typedef struct NodeTag{          /* Structure of each node */
    int line_num;
    char line_code[80];
    struct NodeTag *link;
    }NodeType;

int InsertNewNode(int num,char *A,NodeType **L);
int PrintList(NodeType *L,int);
int DeleteNode(NodeType **L,int n);
int getLineNum(char*);
void ucs(char *);

void main(void)
{

 int num=10, retval=0, lnum=0;
 char line[80],ch;
 NodeType *L;
 int n;
 char *p,*l;

 L=NULL;

 textcolor(YELLOW);
 textbackground(BLUE);

 clrscr();
 gotoxy(0,0);

 cprintf("MCBBX Interpreter\r\n");
 cprintf("%s%d ",cursor,num);
 gets(line);
 ucs(line);

 /* start in AUTO mode */
 while(strcmp(line,"")!=0){
  InsertNewNode(num,line,&L);
 num=num+10;

        cprintf("%s%d ",cursor,num);
  gets(line);
 ucs(line);
 }

 while(1)
 {
        cprintf(cursor);
 gets(line);
 ucs(line);
        p=line;
 if(isdigit(line[0])){
  lnum=getLineNum(p);
  InsertNewNode(lnum,p,&L);
 }

 if (strstr(line,"LIST")!=NULL) {
  lnum=atoi(p+4);
  retval=PrintList(L,lnum);
 }
 if (strstr(line,"EDIT")!=NULL)
                cprintf("editing\r\n");

 if (strstr(line,"DEL")!=NULL){
  lnum=atoi(p+4);
  retval=DeleteNode(&L,lnum);
 }
 if (strstr(line,"BYE")!=NULL){
                textcolor(LIGHTGRAY);
                textbackground(BLACK);
                clrscr();
                free(L);
        break;

 }

 }

Quote
}

int InsertNewNode(int num,char *A,NodeType **L)
{
 NodeType *N, *prev, *curr;
 int val;
        /* setup new node */
  N=(NodeType *)malloc(sizeof(NodeType));
  strcpy(N->line_code,A);
  N->line_num=num;
  N->link=NULL;

 if(*L==NULL){   /* if list is empty */
   *L=N;
   return(0);

 }
 /* locate the node of list L using curr and prev */
  prev=*L;
  curr=*L;
  if(curr->line_num==num)
                {   cprintf("change me\r\n");
   curr=curr->link;
   DeleteNode(L,num);
   if(curr==NULL){
    prev->link=N;
    return(0);
            }
  }

  if (curr->link==NULL){
   curr->link=N;
   return(0);
  }
        curr=curr->link;
  while(curr!=NULL){
      if(curr->line_num==num){
       curr=curr->link;
    DeleteNode(L,num);
    if(curr==NULL){
     prev->link=N;
     return(0);
    }

   }
   if(curr->line_num>num)
   {
    prev->link=N;
    N->link=curr;
    val=0; return(0);
   }
   prev=curr;
            curr=curr->link;
  }
   prev->link=N;
  return(0);

Quote
}

int PrintList(NodeType *L,int l_num)
{
 NodeType *N;

 /* let N point to the first node in list L */
  N=L;

  while(N->line_num<l_num)
  {
   N=N->link;
   if (N->link==NULL)
   {
    return(2);
   }
  }

  while(N!=NULL){
                        cprintf("%s%d ",cursor,N->line_num);
                        cprintf("%s\r\n",N->line_code); /* display code */
   N=N->link;               /* advance pointer N */
   }
  return(0);

Quote
}

int DeleteNode(NodeType **L, int n)
{
 NodeType *Curr, *Prev;

 Curr=*L;

 if (*L==NULL)
 {
  return(1); // nothing in the list
 }
 if(Curr->link==NULL){
  if(Curr->line_num==n)
  {
      *L=NULL;
   free(L);
   return(0); // ok - delete only node on list
  }
    return(2);

 }

 if(Curr->line_num==n){ // delete first node
  Prev=*L;
  Curr=Curr->link;
  *L=Curr;
        Prev->link=NULL;
  free(Prev);
  return(0);
    }

 while(Curr->link!=NULL){
  Prev=Curr;
  Curr=Curr->link;
  if (Curr->line_num==n)
  {
   Prev->link=Curr->link;
   Curr->link=NULL;
   free(Curr);
   return(0);
  }
 } // end while

 return(2); // specified node not found

Quote
}

int getLineNum(char *pline)
{
 char *ptr,*lptr;
 int i=0;

 while(isdigit(pline[i])){
  ptr[i]=pline[i];
  i++;
 }
 ptr[i]='\0';
 if (pline[i]==' '){ i++;}
 lptr=pline+i;
 strcpy(pline,lptr);
    return(atoi(ptr));

Quote
}

void ucs(char *str)
{
 char *p;
 int inquotes=0;

 p=str;
 while(*p)
 {
  if ((!inquotes) && (*p=='"')) {
         inquotes=1;
   p++;
   continue;
   }

  if ((inquotes) && (*p!='"')) {
   p++;
   continue;
   }
  if ((inquotes) && (*p=='"')) {
      inquotes=0;
   p++;
   continue;
   }

  if (!inquotes) *p=toupper(*p);
  p++;
 }
  // return 0;

Quote
}