คือตอนนี้ผมได้เขียนโปรแกรม สามารถแสดงค่ารับค่าได้หมดแล้ว แต่ตอนนี้ผมอยากให้โปรแกรมเจ้ากรรมของผมมัน print ค่าหรือเปลี่ยนการเก็บค่าแบบถอยหลังได้ครับ
#include "Ass1.h"
string line(50,'*');
int mainMenu();
int menuInsert();
int main() {
List<int> a;
ListItr<int> b;
ListItr<int> c;
b = a.zeroth();
struct Node*next;
int choose,choose2,number,temp, position;
do {
choose = mainMenu();
switch (choose)
{
case 1:
do {
choose2 = menuInsert();
switch (choose2)
{
case 1:
cout << "Enter Number : ";
cin >> number;
a.insert(number, b);
break;
case 2:
cout << "Enter Number : ";
cin >> number;
if(a.isEmpty())
cout << "---Data not found" << endl;
else {
c = a.findback();
a.insert(number, c);
}
system("pause");
break;
case 3:
b = a.zeroth();
cout << "Enter Number : ";
cin >> number;
cout << "Enter Position : ";
cin >> position;
if (position <= a.findcount()) {
for (int i = 1; i < position; i++) {
b.advance();
}
a.insert(number, b);
}
else
cout << "---Data not found" << endl;
system("pause");
break;
default:
break;
}
} while (choose2!=0);
break;
case 2:printList(a);
system("pause");
break;
case 3:
cout << line << endl;
cout << "\tFind Number" << endl;
cout << line << endl;
cout << "Enter Number find : ";
cin >> number;
cout << line << endl;
c = a.find(number);
if (c.retrieve() == 0)
cout << "\t----Data not Found ----" << endl;
else
cout << "\t Position is : " <<"\t" << c.retrieve() << " is element of List" << endl;
cout << line << endl;
system("pause");
break;
case 4:
cout << line << endl;
cout << "\t\tRemove data form list" << endl;
cout << line << endl;
cout << "Enter Number remove : ";
cin >> number;
cout << line << endl;
c = a.find(number);
temp = c.retrieve();
if (temp == 0)
cout << "\t----Data not Found ----" << endl;
else {
a.remove(temp);
cout << "\t Remove : " << temp << " success" << endl;
}
cout << line << endl;
system("pause");
break;
/*case 5:
cout << line << endl;
cout << "\t\tFindKth data form list" << endl;
cout << line << endl;
cout << "Enter Number FindKth : ";
cin >> number;
cout << line << endl;
if(number > a.findcount())
cout << "\t----Data not Found ----" << endl;
else {
temp = a.findposition(number);
if (temp == 0)
cout << "\t----Data not Found ----" << endl;
else {
cout << "\t Position of " << number << " The element is : " << temp << " " << endl;
}
}
cout << line << endl;
system("pause");
break;
default:
break;*/
}
} while (choose!=0);
return 0;
}
int mainMenu() {
int choice;
system("cls");
cout << line << endl;
cout << "\tAssignment Linked List"<<endl;
cout << line << endl;
cout << "\t1. -> Insert Linked List" << endl;
cout << "\t2. -> Print Linked List " << endl;
cout << "\t3. -> Find Linked List" << endl;
cout << "\t4. -> Remove Linked List" << endl;
//cout << "\t5. -> FindKth Linked List" << endl;//
cout << "\t0. -> Exit" << endl;
cout << line << endl;
cout << "Enter choice :";
cin >> choice;
return choice;
}
int menuInsert() {
int choice;
system("cls");
cout << line << endl;
cout << "\tMenu Insert linked List"<<endl;
cout << line << endl;
cout << "\t1. -> Insert front" << endl;
cout << "\t2. -> Insert back" << endl;
cout << "\t3. -> Insert position" << endl;
cout << "\t0. -> Back Mainmenu" << endl;
cout << line << endl;
cout << "Enter your choose : ";
cin >> choice;
return choice;
}
ผมอยากให้มันอยู่ใน case5 คือเรากดเลือกเมนูมันแล้วจะสลับลิสต์จากหลังไปหน้าและโชว์ค่าครับ ที่ผมคิดไว้ตอนนี้คือใช้อาเลย์มาเก็บ t"t
รบกวนขอความช่วยเหลือหน่อยครับ เรื่อง coding c++ reverse linkedlist
#include "Ass1.h"
string line(50,'*');
int mainMenu();
int menuInsert();
int main() {
List<int> a;
ListItr<int> b;
ListItr<int> c;
b = a.zeroth();
struct Node*next;
int choose,choose2,number,temp, position;
do {
choose = mainMenu();
switch (choose)
{
case 1:
do {
choose2 = menuInsert();
switch (choose2)
{
case 1:
cout << "Enter Number : ";
cin >> number;
a.insert(number, b);
break;
case 2:
cout << "Enter Number : ";
cin >> number;
if(a.isEmpty())
cout << "---Data not found" << endl;
else {
c = a.findback();
a.insert(number, c);
}
system("pause");
break;
case 3:
b = a.zeroth();
cout << "Enter Number : ";
cin >> number;
cout << "Enter Position : ";
cin >> position;
if (position <= a.findcount()) {
for (int i = 1; i < position; i++) {
b.advance();
}
a.insert(number, b);
}
else
cout << "---Data not found" << endl;
system("pause");
break;
default:
break;
}
} while (choose2!=0);
break;
case 2:printList(a);
system("pause");
break;
case 3:
cout << line << endl;
cout << "\tFind Number" << endl;
cout << line << endl;
cout << "Enter Number find : ";
cin >> number;
cout << line << endl;
c = a.find(number);
if (c.retrieve() == 0)
cout << "\t----Data not Found ----" << endl;
else
cout << "\t Position is : " <<"\t" << c.retrieve() << " is element of List" << endl;
cout << line << endl;
system("pause");
break;
case 4:
cout << line << endl;
cout << "\t\tRemove data form list" << endl;
cout << line << endl;
cout << "Enter Number remove : ";
cin >> number;
cout << line << endl;
c = a.find(number);
temp = c.retrieve();
if (temp == 0)
cout << "\t----Data not Found ----" << endl;
else {
a.remove(temp);
cout << "\t Remove : " << temp << " success" << endl;
}
cout << line << endl;
system("pause");
break;
/*case 5:
cout << line << endl;
cout << "\t\tFindKth data form list" << endl;
cout << line << endl;
cout << "Enter Number FindKth : ";
cin >> number;
cout << line << endl;
if(number > a.findcount())
cout << "\t----Data not Found ----" << endl;
else {
temp = a.findposition(number);
if (temp == 0)
cout << "\t----Data not Found ----" << endl;
else {
cout << "\t Position of " << number << " The element is : " << temp << " " << endl;
}
}
cout << line << endl;
system("pause");
break;
default:
break;*/
}
} while (choose!=0);
return 0;
}
int mainMenu() {
int choice;
system("cls");
cout << line << endl;
cout << "\tAssignment Linked List"<<endl;
cout << line << endl;
cout << "\t1. -> Insert Linked List" << endl;
cout << "\t2. -> Print Linked List " << endl;
cout << "\t3. -> Find Linked List" << endl;
cout << "\t4. -> Remove Linked List" << endl;
//cout << "\t5. -> FindKth Linked List" << endl;//
cout << "\t0. -> Exit" << endl;
cout << line << endl;
cout << "Enter choice :";
cin >> choice;
return choice;
}
int menuInsert() {
int choice;
system("cls");
cout << line << endl;
cout << "\tMenu Insert linked List"<<endl;
cout << line << endl;
cout << "\t1. -> Insert front" << endl;
cout << "\t2. -> Insert back" << endl;
cout << "\t3. -> Insert position" << endl;
cout << "\t0. -> Back Mainmenu" << endl;
cout << line << endl;
cout << "Enter your choose : ";
cin >> choice;
return choice;
}
ผมอยากให้มันอยู่ใน case5 คือเรากดเลือกเมนูมันแล้วจะสลับลิสต์จากหลังไปหน้าและโชว์ค่าครับ ที่ผมคิดไว้ตอนนี้คือใช้อาเลย์มาเก็บ t"t