我第一次尝试实现了一个que。它显示先输入的数据和最后输入的数据。以下是我的代码。如果您发现任何错误,请帮助我。
#include <windows.h>
#include "stdafx.h"
#include "iostream"
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int rollno;
struct node*n;
};
void read(struct node*);
void display(struct node*);
struct node* create();
struct node* cread();
struct node*head = NULL;
struct node*tail = NULL;
void read(struct node*p)
{
scanf("%d",&p->rollno);
p->n=NULL;
printf("\n");
}
void display(struct node*p)
{
printf("%d\n",p->rollno);
}
struct node* create()
{
struct node*q;
q=(struct node*)malloc(sizeof(struct node));
return q;
}
struct node* cread()
{
struct node*j;
j=create();
read(j);
return j;
}
void push(struct node*cur)
{
if(head==NULL)
{
head=cur;
tail=cur;
}
else
{
struct node*f;
//f=head;
//cur->n=f;
//head=cur;
head->n=cur;
}
}
void pop_all()
{
struct node*p;
p=head;
if(head==NULL)
{printf("\n\t\t\tQUEUE EMPTY\n");}
else
{
head = tail;
while(head!=NULL)
{
display(head);
p=head;
head = head->n;
//tail = tail->n;
//head=tail;
//free(p);
}
while(head!=NULL)
{
display(head);
p=head;
head = head->n;
//tail = tail->n;
//head=tail;
//free(p);
}
}
}
void main()
{
struct node*cur;
int a,q;
char ch;
while(1)
{
printf("Press 1 For PUSH\n");
printf("Press 2 For POP ALL\n");
printf("Press 3 For EXIT\n");
scanf("%d",&a);
if(a==1)
{
cur=cread();
push(cur);
}
else if(a==2)
{
pop_all();
}
else if(a==3)
{
break;
}
printf("Press Any Key To Clear Screen\n");
ch=getch();
system("cls");
}
}
答案 0 :(得分:1)
有一些问题。
malloc()
in C。tail
变量,因此可以将其删除。