queue

why it is print just last node were add


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <string>

using namespace std;


struct personal_rec
{
       string name;
       int birth;
       };
       
       struct patient
       {
              int id;
              int pr;
              string status;
              personal_rec person;
              patient *next;
              
              };

void enqueu (  patient *& , patient *&);
void dequeue (patient *& , patient *&);
void print ( patient * );

int main()
{
    
    patient * front=NULL , * rear=NULL;

	int num;
	do
	{
		cout<<"1. add patient"<<endl<<"2. delete patient "<<endl<<"3. print Info"<<endl<<"enter your choice"<<endl;
		cin>>num;

		switch(num)
		{
	   	case 1:enqueu ( rear , front);
			break;
			
    	  case 2:dequeue (front , rear);
			break;		

		case 3:print ( front );
			break;
		}}
		while (num!=3);


               
	return 0 ;
}



void enqueu (   patient *& front ,  patient *& rear)
{
     
    patient *n;
    n=new patient;
	cout<<"enter id"<<endl;
    cin>>n->id;
	cout<<"enter name"<<endl;
    cin >> n->person.name; 
    cout<<"enter birth"<<endl;
     cin >> n->person.birth; 
    cout<<"enter status"<<endl;
    cin >> n->status; 
    cout<<"enter priority"<<endl;
    cin>>n->pr;
	n->next=NULL;


     if(front==NULL)
     {
     front=n;
     rear=n;}
     
     else
     if(n->pr==0 && front!=NULL)
     
	 {
     rear->next=n;
     rear=n;
	 }

   
      else  // here if pr=!0
if (front->pr==0 && n->pr!=0)
      {n->next=front;
       front=n;
       }
       
       else
       {
           patient *cur=front;
           while (cur->pr!=0)
           {
                 cur=cur->next;
                 }
                   cur->next=n;
       
}
	 }
	 
	 
	 
void print ( patient * front)
{
	patient *current=front;
	while (current!=NULL)
	{
		cout<<"id :"<<current->id<<endl;
		cout<<"name :"<<current->person.name<<endl;
		cout<<"birth :"<<current->person.birth<<endl;
		cout<<"status :"<<current->status<<endl;
		cout<<"priority :"<<current->pr<<endl;

		current=current->next;

	}
	getchar();
getchar();
}



void dequeue (patient *&front , patient *&rear)

{
	patient *cur;
	if(front==NULL)
		cout<<"empty";
	else
	{
		if(front->next==NULL)
		{
			delete front;
		front=rear=NULL;}


		else
		{
			cur=front;
		front=front->next;
		delete cur;
		}

	}
}
any answer ?
???????????????????
Your problem is that you call enqueu with arguments (rear, front), but the function is expecting (front, rear). (By the way, you misspelled enqueue.)

Also, since I was looking at your code:

If (front == NULL) fails in line , you don't need to check (front != NULL) in line 82.

In lines 99 - 102, you are walking through the elements in the list looking for one with a priority == 0. If you never find one, you will walk past the end of the list.
Topic archived. No new replies allowed.