#include<stdioh>
int main(){
int a,b,c,d,x;
a=c=0;
b=0;
c=20;
if(a)
d=d-10;
else if(b)
if(c)
x = 15;
else
x = 25;
printf("d=%d,x=%d\n",d,x);
/以下加的这一段是为了在程序里停住,防止CMD窗口闪现,可以去掉/
char h=getchar();
if(h=='y')
return 0;
}
我电脑强制执行结果:d=-858993460,x=-858993460请仔细对比!!!,你的程序有很多不对的地方。看我是哪些地方和你不一样的!!!
此程序我已经调试并测试通过了。
如果你要运行这个程序,注意把汉字全部去掉!!!
#include<stdioh>
#include<malloch>
#include <stdlibh>
typedef struct node
{
int data;
node next;
}node;//你结构体定义错误!!!!
struct node creat(int num)
{
struct node p,q,head;
int i;
p = (struct node )malloc(sizeof(struct node));
head = p;
q = p;
for(i=0;i<num-1;i++)
{
p = (struct node )malloc(sizeof(struct node));
q->next = p;
q = p;
}
p->next = NULL;
return head;
}
void input(struct node head)
{
struct node p;
p = head;
while(p->next != NULL)
{
scanf("%d",&(p->data));
p = p->next;
}
scanf("%d",&(p->data));
}
void output(struct node head)
{
struct node q;
q = head;
while(q->next != NULL)
{
printf("%d ",q->data);
q = q->next;
}
printf("%d\n",q->data);
}
void main()
{
struct node A,B,C;
struct node head=NULL;//先给一个初始值,头结点只赋值一次。用空判断
int a,b,i;
printf("请输入A链表的元素个数:\n");
scanf("%d",&a);
A = creat(a);
printf("请按从小到大的顺序输入A链表元素:\n");
input(A);
output(A);
printf("请输入B链表的元素个数:\n");
scanf("%d",&b);
B = creat(b);
printf("请按从小到大的顺序输入B链表元素:\n");
input(B);
output(B);
for(;;)//退出循环用空来判断
{
if(A!= NULL && B!= NULL)
{
if(A->data < B->data)
{
if(!head)//判断头结点指向哪个,只执行一次!! 下同
{
head=A;
C=head;
}
else
{
C->next=A;//此处应该这样;
C=C->next;
}
A = A->next;
}
else if(A->data == B->data)
{
if(!head)
{
head=A;
C=head;
}
else
{
C->next=A;
C=C->next;
}
A = A->next;
B = B->next;
}
else if(A->data > B->data)
{
if(!head)
{
head=B;
C=head;
}
else
{
C->next=B;
C=C->next;
}
B = B->next;
}
}
else
break;//跳出循环
}
//以下代码从循环拿出来,因为下面代码只执行一次就够了,没必要循环
if(A == NULL && B == NULL)
{
}
else if(A != NULL && B == NULL)
{
C ->next= A;//只需要这样就行了,。。。
}
else if(A == NULL && B != NULL)
{
C ->next= B;
}
printf("AB链表合并后的链表C中各元素依次为:\n");
output(head);//必须先输出再释放,不然你节点都释放了,怎么输出啊!!!
free(head);//此时不能free(A),free(B),因为此时A,B,节点的次序已经完全打乱了,节点已经完全链接到head了,只需要释放head就行了。
}
如有不对,欢迎赐教!!!#include \"afxwinh\"
class CMainFrame:public CWnd
{
public:
CMainFrame()
{
CreateEx(NULL,AfxRegisterWndClass(0,::LoadCursor(NULL,IDC_ARROW),(HBRUSH)(COLOR_WINDOW+1)),
_T(\"1、如何创建最简单的MFC程序?\"),WS_POPUPWINDOW,
//CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
100,100,100,100,
NULL,NULL,0);
}
};
class CTestApp : public CWinApp
{
virtual BOOL InitInstance();
};
CTestApp theApp;
BOOL CTestApp::InitInstance()
{
m_pMainWnd=new CMainFrame();
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}上面说的那些是对的,但是你的程序中还有其他的错误,我给你改了一下,你看一下
h 文件
#ifndef QUEUE_H
#define QUEUE_H
#include <string>
using namespace std;
class Queue{
public:
Queue(int);
bool Enqueue (int );
int Dequeue ();
int Peek (int) const;
int GetSize () const;
void Display () const;
int ShowQueue();
~Queue();
private:
int number;//初始时数组个数
int temp;
int index;//数组元素下标
int numNow;//数组现有数据个数
int line;
};
#endif;
cpp 文件
#include <iostream>
using namespace std;
#include "Queueh"
Queue::Queue(int Number)
{
number = Number;
line=new int[number];
}
bool Queue::Enqueue(int i)
{
if (i>(number-1))
return false;
cout<<"Please input a integer to fill the queue : "<<i<<" ";
cin>>line[i];
return true;
}
int Queue::ShowQueue()
{
int k;
cout<<"There are "<<number<<" elements in the queue They are :\n";
for(k=0;k<=number-1;k++)
{
cout<<line[k]<<" ";
}
cout<<endl;
return 0;
}
int Queue::Dequeue()
{
int i;
temp=line[0];
cout<<"This is Dequeue operation: "<<temp;
cout<<endl;
for (i=0;i<=number-2;i++)
line[i]=line[i+1];
numNow=number-1;
return temp;
}
int Queue::Peek(int index) const
{
return line[index];
}
int Queue::GetSize() const
{
if (numNow<=0)
return 0;
else
return numNow;
}
void Queue::Display() const
{
int i;
for(i=0;i<=(number-2);i++)
cout<<line[i];
cout<<endl;
}
Queue::~Queue()
{
delete [] line;
}
int Number;
int i;
int main()
{
cout<<"Please input the queue size:\n";
for(;;)
{
cin>>Number;
if ((Number<=0)||(Number>10))
cout<<"please input the queue size , between 1 and 10";
else
break;
}
Queue queue(Number);
for (i=0;i<Number;i++)
{
queueEnqueue(i);
}
queueShowQueue();
queueDequeue();
cout<<"There are "<<queueGetSize()<<" elements"<<" in the queue They are:\n";
queueDisplay();
return 0;
}struct Node{
Term entry;
Node next;
}
struct定义完以后大括号后面应该有;号,即应该是:
struct Node{
Term entry;
Node next;
};
否则解析器觉得你的这个结构定义没写完,当然不应该再出现'Class''Queue'了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)