搜索
首页 电脑/网络 程序设计 VC++

一个简单的VC程序,谁会?

  。
  编写一个程序,有单链表的节点类型如下:typedef struct node{ int no; struct node *next;}node;设计如下函数:void create(Node *h):建立不带头节点的单链表hint len(Node *h):返回不带头节点的单链表h的长度void del(Node *h,int i):删除不带头节点的单链表h的第i个节点void disp(Node *h):输出不带头节点的单链表h的所有节点值其中:main函数如下:void main(){ Node *head; int i; create(head); disp(head); cout>i; del(head,i); disp(head);}。

全部回答

2005-03-30

0 0
    题目有点烂。 head没有任何值的时候就使用它是不合理的,所以我改了一下, 把 Node * head; 改成了Node *head=new Node; 在VC下,如果 head不指向任何值,把它做为参数是没有意义的。
     #include typedef struct node { int no; struct node *next; }Node; /*把node改成了Node*/ void create(Node *h){ int i; static Node *current; h->no=1; h->next=0; current=h; for(i=2;ino=i; temp->next=0; current=current->next=temp; } } int len(Node *h){ int count=0; Node *current=h; while(current!=0){ count++; current=current->next; } return count; } void disp(Node *h){ Node *current=h; while(current!=0){ coutnonext; } coutlen(h)) return; if(--i){ while(--i) current=current->next; temp=current->next; current->next=temp->next; delete temp; } else{ temp=h; h=h->next; delete temp; } } void main() { Node *head=new Node; int i; create(head); disp(head); cout>i; del(head,i); disp(head); }。
  

2005-03-30

79 0
    由于要在函数中改变实参的值,以下函数的参数略有改变,声明了引用类型: void create(Node *h); //楼主的声明 void create(Node *&h , int maxno); //修改后的声明 void del(Node *h,int i); //楼主的声明 void del(Node *&h,int i); //修改后的声明 另外,对于create()还增加了一个参数:maxno,作用是指定链表节点的no成员的值。
    指定一个maxno后,创建出来的链表包含0~maxno之间所有整数的节点(max>=0,否则会创建空链表)。 由于没有装vc,以下程序在tc++3,0下调试通过: //list。
    cpp #include #include typedef struct node { int no; struct node *next; } Node; void create(Node *&h , int maxno); //建立不带头节点的单链表h,参数有所改动 int len(Node *h); //返回不带头节点的单链表h的长度 void del(Node *&h,int i); //删除不带头节点的单链表h的第i个节点(i从0起计算),参数有所改动 void disp(Node *h); //输出不带头节点的单链表h的所有节点值 void main() { Node *head; int i; create(head,3); count >i; del(head,i); disp(head); } void create(Node *&h , int maxno) { Node *p; int i; if (maxno >= 0) //创建第一个节点 { p = (Node *)malloc(sizeof(Node)); p->no = 0; h = p; } else { h = NULL; //参数表中声明h为Node指针的引用类型,所以可以通过形参改变实参的值 return ; } for(i=1 ; inext = (Node *)malloc(sizeof(Node)); p->next->no = i; p = p->next; } p->next = NULL; } int len(Node *h) { int lencount = 0; while(h) { lencount++; h = h->next; //参数表中h没有被声明为引用类型,改变h的值不会改变实参的值 } return lencount; } void del(Node *&h,int i) { int j; Node *p , *q; if (inext; //h是引用类型,改变它的值可以改变实参的值 free(p); return ; } for(j=1,p=h ; jnext; } if (p!=NULL) { q->next = p->next; free(p); } } void disp(Node *h) { while(h!=NULL) { cout no; h = h->next; } } 。
  

类似问题换一批

热点推荐

热度TOP

相关推荐
加载中...

热点搜索 换一换

电脑/网络
VC++
硬件
电脑装机
程序设计
互联网
操作系统/系统故障
笔记本电脑
反病毒
百度
软件
程序设计
VC++
VB
数据库
C/C++
汇编语言
JAVA相关
C#/.NET
其他编程语言
VC++
VC++
举报
举报原因(必选):
取消确定举报