C++:为什么编译没错,但就是运行不出结果呢?程序如下:

#include <stdio.h>
#include <stdlib.h>
typedef struct node //自定义链表结构体
{
int data;
struct node *next;
}LNode ;
LNode *create(int n)
{
LNode *head,*p,*q;
int i,m;
p=(LNode*)malloc(sizeof(LNode));
p->next=NULL;
head=p; //建立头结点
printf("Please enter %d integers:\n",n);
for(i=1;i<=n;i++)
{
q=(LNode*)malloc(sizeof(LNode));
scanf("%d",&m); //输入链表的data值
q->data=m;q->next=NULL;
p->next=q;p=q;
}
return head;
}
int DeleteLink(LNode* head,int i)
{
LNode *p,*q;
int j=0;
p=head;
while((p!=NULL)&&(j<i-1))
{
p=p->next;
j++;
}
if(p==NULL) return 0;
q=p->next;
p->next=q->next;
free(q);
return 0;
}
Print(LNode *head,int m) //输出函数
{
LNode *p;
p=head;
printf ("\n");
while (p->next!=NULL)
{
p=p->next;
printf ("%d ",p->data);
}
return 0;
}
int main ()
{
int n,i;
LNode *head,*p;
p=(LNode*)malloc(sizeof(LNode));
p->next=NULL;
head=p;
LNode *create(int);
int DeleteLink(LNode *, int);
int Print(LNode *,int);
printf("How many nodes do you want in this linklist?\n");
scanf("%d",&n);
create(n);
printf("Which node do you want to delete?\n");
scanf("%d",&i);
DeleteLink(head,i);
Print(head,n-1);
return 0;
}

虽然不太清楚你想要啥,给你看看我的写法,希望对你有帮助:

struct CListNode{
 int d;
 CListNode* next;
};
struct CList{
 int length;
 CListNode* head;
};
CList* CListNew(int n) {
 CList* l = (CList*)malloc(sizeof(CList));
 l->length = n;
 l->head = NULL;
 if (n > 0) {
  l->head = (CListNode*)malloc(sizeof(CListNode));
  l->head->next = NULL;
  l->head->d = -1;
 }
 CListNode* p = l->head;
 for(int i=1; i<n;i++) {
  CListNode* q = (CListNode*)malloc(sizeof(CListNode));
  q->next = NULL;
  q->d = -1;
  p->next = q;
  p = q;
 }
 return l;
}
void CListDelete(CList* l) {
 CListNode* p = l->head;
 while (p != NULL) {
  CListNode* q = p->next;
  free(p);
  p = q;
 }
 free(l);
}
bool CListRemove(CList* l, int index) {
 CListNode* p = l->head;
 CListNode* prev = NULL;
 for(int i = 0; i < index; i++) {
  prev = p;
  p = p->next;
 }
 if (p == NULL)
 { return false; }
 if (prev == NULL)
 { l->head = p->next; }
 else
 { prev->next = p->next; }
 free(p);
 l->length -= 1; 
 return true;
}
void CListInput(CList* l) {
 CListNode* p = l->head;
 while (p != NULL) {
  scanf("%d", &p->d);
  p = p->next;
 }
}
void CListPrint(CList* l) {
 CListNode* p = l->head;
 printf("[");
 if (p != NULL) {
  while (true) {
   printf("%d", p->d);
   p = p->next;
   if (p == NULL) {
    break;
   }
   printf(",");
  }
 }
 printf("]");
}
int main() {
 CList* l = CListNew(3);
 CListInput(l);
 CListRemove(l,1);
 CListPrint(l);
 CListDelete(l);
 return 0;
}

温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2017-09-01
int main ()
{
int n,i;
LNode *head,*p;
//指针p没有用,这段可以注释掉
// p=(LNode*)malloc(sizeof(LNode));
// p->next=NULL;
// head=p;
LNode *create(int);
int DeleteLink(LNode *, int);
int Print(LNode *,int);
printf("How many nodes do you want in this linklist?\n");
scanf("%d",&n);
head = create(n); //原始代码返回的链表头指针没有复制给phead
printf("Which node do you want to delete?\n");
scanf("%d",&i);
DeleteLink(head,i);
Print(head,n-1);
return 0;
}

另外,给出的Print函数的定义有误,应该是前面忘记了返回类型int本回答被提问者采纳
第2个回答  2014-10-09
代码就不能按照C++的格式贴出来吗?脑袋都看晕了追问

不好意思,我初学编程,目前只接触过TC。。。。

追答

这样贴代码,别人才好帮你看。

相似回答