0
我嘗試了第一次實現隊列。它顯示先輸入的數據和最後輸入的數據。以下是我的代碼。如果你能找到任何錯誤,請幫助我。隊列剛剛彈出第一個和最後一個元素
#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");
}
}