当前位置:编程学习 > C#/ASP.NET >>

编译时正常,运行时却报错,调试器给的错误信息是expression_r cannot be uated。调试时在处出错(是在给链表输入5个原始数据元素这块不知)

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#define  TRUE   1
#define  FALSE  0
#define  OK     1
#define  ERROR  0

struct Student
       {   
          int    NO;      //学号
          char    name[10]; //姓名
        };

typedef struct Student EType;

//学生信息双向链表
struct DoubleNode
{
EType  data;
DoubleNode *plink;
DoubleNode *nlink;
};

 struct HeadNode
{
int length;
DoubleNode *first;
};
typedef HeadNode *DoubleChainList;

DoubleChainList L;

DoubleNode *Resultp;
DoubleNode *Resultn;
char re_choose[]={"\n选择非法,请输入正确的编号...\n"};

//作者信息
void Menu_name()     
{   
printf("\n\n\n\n\n\n\n");
printf("              *************************************************\n");
printf("                           线性表(双向链表)存储及运算\n\n");
printf("                           制作:xxx\n");
printf("                           班级:xxxx班\n");
printf("                           学号: xxxxxxxxx\n");
printf("                           指导老师: xxx\n");
printf("              **************************************************\n");
printf("\n\n\n\t\t");
}

//构造一个空表
void CreatDoubleChainList(DoubleChainList &L)
{
L=new HeadNode;
L->first=NULL;
L->length=0;
}

//在双向链表L中第k个元素之后插入元素x
bool InsertDoubleChainList(DoubleChainList &L, int k, EType &x)
{
if(k<0) return false;
int    index=1;
DoubleNode *current=L->first;
while (index<k && current)
{//找第个节点
index++;
current=current->nlink;
}
if(k>0 && !current)  return false;
    DoubleNode *q=new DoubleNode;
q->data=x;
if(k)
{//插入在之后
q->nlink=current->nlink;
q->plink=current;
        DoubleNode *p=current->nlink;
if(p)
p->plink=q;
current->nlink=q;
}
else
{//作为第一个元素节点插入
q->nlink=L->first;
q->plink=NULL;
        DoubleNode *p=L->first;
        if(p)
p->plink=q;
L->first=q;
}
L->length++;
return true;
}

//在双向链表L中删除第k个元素节点
bool InsertDoubleChainList(DoubleChainList &L, int k)
{
if(k<1 || !L->first)  return 0;
    DoubleNode *current=L->first;
    DoubleNode *p;
if(k==1)
{//删除的是链表中的第一个节点
p=current->nlink;
if(p)
p->plink=NULL;
L->first=p;
}
else
{
      DoubleNode *q=L->first;
  for(int index=1; index<k && q; index++)
  q=q->nlink;
  if(!p)  return false;
  current=q;  //current指向k第个节点
  q=current->plink;
  p=current->nlink;
  q->nlink=p;
  if(p)
  p->plink=q;
}
delete current; //释放被删除节点current的空间
L->length--;
return true;
}

//给定某一学号,找到该学生信息
DoubleNode * SearchtDoubleChainList(DoubleChainList &L, int x)
{
   DoubleNode *current=L->first;
  
   while(current && current->data.NO !=x)
   {
   return current;
   current=current->nlink;
   }
   return  NULL;
}


// 逐个地输出双向链表L中的数据元素
void Output(DoubleChainList &L)
{
    DoubleNode *current=L->first;
cout<<"\n\n";
cout<<" ┌────────┐"<<endl;
cout<<" │    学生信息表  │"<<endl;
cout<<" ├────┬───┤"<<endl;
cout<<" │        │      │"<<endl;
cout<<" │学    号│姓 名 │"<<endl;
cout<<" │    │   │"<<endl;
cout<<" ├────┼───┤"<<endl;

while(current)
{
     cout<<" │"<<setw(8)<<current->data.NO<<"│"<<setw(6)<<current->data.name<<"│"<<endl;
 cout<<" ├─────┼────┤"<<endl;
     current=current->nlink;
}
    cout<<" │        │      │"<<endl;
cout<<" └────┴───┘"<<endl;
}

//特殊查找,给定某一学号x,能同时找到该学生第i个前驱的学生信息,以及该学生第j个后继的学生信息
void FindbeforeAndAfter(DoubleChainList &L, int x, int i, int j)
{
     DoubleNode *p=NULL;
 DoubleNode *q=NULL;
     DoubleNode *current=L->first;
 int index;

     current=SearchtDoubleChainList(L, x);

 if(current)
 {
 p=current;
 for(index=0;index<=i&&p;index++)
 {
             p=p->plink;
 if(p)
 Resultp=p;
 }

 q=current;
         for(index=0;index<=j&&q;index++)
 {
             q=q->nlink;
     if(q)
 Resultn=q;
 }
 }
 else
 printf("没有找到您要查找的学号");
}

//操作选择函数
void main_switch(char j)
{
    int k;
int m;
int n;
    //int pos; //为什么pos不能定义为 unsigned 
    EType x;
    DoubleNode *p;
    switch(j)
{

case '1' ://显示链表中数据元素
system("cls");
Output(L);
system("pause");
system("cls");
break;

case '2' ://插入数据元素
system("cls");
Output(L);
cout<<"\n\t当前数据元素的下标范围:0---"<<L->length<<endl;
printf("\n\t请输入要插入点的位置:");
scanf("%d",&k);
fflush(stdin);
if (k < 0 || k > L->length)
{
cout <<"\n"<<re_choose<<endl;
system("pause");
system("cls");
break;
}
printf("Please input the number:");
scanf("%d",&x.NO);
                printf("Please input the name:");
scanf("%s",&x.name);
if(InsertDoubleChainList(L,k,x))
{
printf("插入成功!\n\n");
Output(L);
}
else
    printf("插入失败!\n\n");
system("pause");
system("cls");
break;

case '3'://删除数据元素
cout<<"\n\t当前数据元素的下标范围:1---"<<L->length<<endl;
printf("\n\t请输入要删除数据元素的位置:");
scanf("%d",&k);
fflush(stdin);
if (k < 1 || k > L->length)
{
cout <<"\n"<<re_choose<<endl;
system("pause");
system("cls");
break;
}
if(InsertDoubleChainList(L,k))
{
printf("删除成功!\n\n");
Output(L);
}
else
printf("删除失败!\n\n");
system("pause");
system("cls");
break;

case '4'://查找数据元素
system("cls");
Output(L);
printf("\n\t请输入要查找的学生号:");
scanf("%d",&k);
fflush(stdin);
p=SearchtDoubleChainList(L,k);
if(p)
{
printf("查找成功!");
printf("学号:%d\n",p->data.NO);
                    printf("姓名:%s\n",p->data.name);
}
else
                     printf("查找失败!");
system("pause");
system("cls");
break;

case '5'://特殊查找
                 system("cls");
 printf("请输入要查找的学号:");
 scanf("%d",&k);
 printf("要查找该学生的第几个前驱的学生信息:");
 scanf("%d",&m);
                 printf("要查找该学生的第几个后驱的学生信息:");
 scanf("%d",&n);
                 FindbeforeAndAfter(L, k, m, n);
 if(Resultp)
 {
 printf("第%d个前驱的学生信息为:",m);
 printf("学号:%d  姓名:%s",Resultp->data.NO,Resultp->data.name);
 }
 else
 printf("没有第%d个前驱节点的学生信息!",m);

                 if(Resultp)
 {
 printf("第%d个后驱的学生信息为:",n);
 printf("学号:%d  姓名:%s",Resultp->data.NO,Resultn->data.name);
 }
 else
 printf("没有第%d个后驱节点的学生信息!",n);
                system("pause");
system("cls");
break;

case '0': 
exit(0);
break;
default  :
cout <<re_choose<<endl;
system("pause");
system("cls");
break;

}//end switch
}

void Menu()    //菜单函数
{
cout <<"\n\t\t"<<"请选择以下一个功能:"<<endl;
cout <<"\n\t\t"<<"1.显示链表中的数据元素."<<endl;
cout <<"\t\t2.插入数据元素." << endl;
cout <<"\t\t3.删除数据元素." << endl;
    cout <<"\t\t4.查找数据元素."<<endl;
cout <<"\t\t5.特殊查找数据元素."<<endl;
cout <<"\t\t0.退出.\n"<<endl;
cout <<"\t\t===============================\n"<<endl;
}

int main()
{
   char a[100];
    int s;

    system("cls");
Menu_name();
system("pause");
system("cls");
CreatDoubleChainList(L);
    struct Student arr[5]={{1101,"李萍"},{1108,"王林"},{1112,"杨宁"},{1120,"江博"},{1127,"张雨"}};;
    DoubleNode *p=L->first;
   
    for(s=0;s<5;s++) 
{
p->data.NO=arr[s].NO;
        strcpy(p->data.name, arr[s].name);
        InsertDoubleChainList(L,s,arr[s]);
    }
       
while(1)
{
system("cls");
Menu();
printf("\n\t请输入功能编号:");

gets(a);

        if(a[1]!='\0')
{
cout <<"\n"<<re_choose<<endl;
system("pause");
system("cls");
continue;
}
        else
{
if(a[0]=='0') 
break;
main_switch(a[0]);

}

}

return 0;
}
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,