PDA

View Full Version : سوال: مشکل در نوشتن درج دو لیست



alikhounsary
سه شنبه 04 آذر 1393, 16:27 عصر
با سلام
به مشکل جالبی برخورد کردم:
این برنامه هیچ مشکلی نداره، ولی نمیتونم تابع
M رو در تابع اصلی (main) فراخوانی کنم
میزنم ران بشه
گیر میکنه
تابع درست کار میده ها چک شده کامل ;)
میخوام لیست رو بدیم به تابع اصلی که چاپ کنه , چی کار کنم؟

#include <iostream>#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>


using namespace std;
class node
{
public:
int num;
node *next;
};
class list
{
public:
node *first;
node *last;
int size;
list ()
{
first = last = NULL;
}
~list ()
{
node *cur=first,*temp;
while (cur)
{
temp = cur;
cur = cur -> next;
delete temp;
}
}
void bubble ()
{
node *ptr,*preptr, *curlast;
curlast=last;
while(curlast!=first)
{
preptr=NULL;
ptr=first;
while(ptr!=curlast)
{
if(ptr->next->num< ptr->num)
{
if(ptr->next==curlast)
curlast=ptr;
if(!ptr->next->next)
last=ptr;
if(!preptr)
first=ptr->next;
else
preptr->next=ptr->next;
ptr->next=ptr->next->next;
if(!preptr)
first->next=ptr;
else
preptr->next->next=ptr;
}
if(!preptr)
preptr=first;
else
preptr=preptr->next;
ptr=preptr->next;
}
curlast=preptr;
}
}


};
node *M (list L , list K)
{
list H;
H.first=NULL;
node *help1,*help2,*help3;
help1 = L.first;
help2 = K.first;
do {
if ((help2==NULL)|| (help1!=NULL && (help1->num < help2->num))) {
if (H.first == NULL)


{
H.first = help1;
help3=help1;
}
else
{
help3->next = help1;
help3=help3->next;
}
help1 = help1->next;
} else {
if (H.first == NULL)
{
H.first = help2;
help3=help2;
}
else
{
help3->next = help2;
help3=help3->next;
}


help2 = help2->next;


}
} while (help1!=NULL || help2!=NULL);


// L.first = H.first;
return H.first;
}/*
node* Merge (list L , list K)
{
list H;
cout << "H.first = " << H.first;


return H.first;
}*/
int main ()
{
char i,m[11];
node *nod,*temp;
int c;
list L,K;
L.first = NULL;
K.first = NULL;
string a;
do {
cout << "\n\t1-Enter List A and B \n\t2-Merege \n\t0-Exit\n\tEnter your choice: ";
cin >> i;
switch (i) {
case '1':
gets(m);
/*********Get List A*********/
while (1) {
cout << "Enter numbers for A, for end Press Enter: ";
gets(m);
if (strlen(m)==0)
break;
c = atoi(m);
nod = new node;
nod -> next = NULL;
if (L.first == NULL) {
L.first = nod;
L.last = nod;
}
else {
L.last->next = nod;
L.last = nod;
}
nod -> num = c;


}
L.bubble ();
cout << L.first -> num << " ";
temp = L.first -> next;
while (temp!=NULL) {
cout << temp -> num << " ";
temp = temp -> next;
}
cout << endl;
/*********Get List B*********/
while(1) {
cout << "Enter numbers for B, for end Press Enter: ";
gets(m);
if (strlen(m)==0)
break;
c = atoi(m);
nod = new node;
nod -> next = NULL;
if (K.first == NULL) {
K.first = nod;
K.last = nod;
}
else {
K.last->next = nod;
K.last = nod;
}
nod -> num = c;
}
K.bubble ();
cout << K.first -> num << " ";
temp = K.first -> next;
while (temp!=NULL) {
cout << temp -> num << " ";
temp = temp -> next;
}


break;
case '2':
M(L,K);
break;
}


}
while (i!='0');
return 0;
}