PDA

نسخه کامل مشاهده نسخه کامل : حذف گره در لیست پیوندی



h66
20-05-2010, 11:11
سلام دوستان.ببخشید توی کتاب schutlz درس ساختمان داده برای حذف یک گره با اطلاعات معلوم ITEM الگوریتم زیر رو داده و من میخوام به کد c++ تبدیلش کنم. هرجا تو اینترنت سر زذم با استفاده از کلاس node اینکارو انجام دادن، پس یعنی با تبدیل این الگوریتم به c++ عملیات حذف رو انجام داد بدون نوشتن کلاس nodeو اینا؟ میشه کسی این کد رو برام به c++ تبدیل کنه؟
START مکان ابتدای لیست
LINK آرایه مربوط به فیلد اشاره گر به گره بعدی
INFO آرایه مربوط به اطلاعات فیلد
LOC مکان گره n
LOCP مکان گره قبل از n
SAVE مکان گره قبلی

if START=NULL then
set LOC:=NULL and LOCP:=NULL , and return
if INFO[START]=ITEM , then
set LOC:=START and LOCP=NULL, and return
set SAVE:=START and PTR:=LINK[START]
while PTR!=NULL
if INFO[PTR]=ITEM then
set LOC:=PTR and LOCP:=SAVE, and return
set SAVE:=PTR and PTR:=LINK[PTR]
end while
set LOC:=NULL
return

h66
20-05-2010, 16:59
لااقل یکی بگه مگه میشه بدون استفاده از کلاسnode و کلاس linklist، تابعی برای یافتن گره مورد نظر در لیست پیوندی نوشت؟ آخه الگوریتم بالا در کتاب schultz با دو آرایه link و info و بدون کلاسه
لطفا راهنماییم کنید:41:

h66
22-05-2010, 10:59
یعنی اینقدر سوالم سخته که کسی حاضر نیست کمکم کنه؟ start رو در ورودی برنامه به ما میدن؟

adine2020
26-06-2010, 00:44
این یه برنامه کامل که شامل یک لیست رو ایجاد میکنه چاپش می کنه و گره مورد نظر رو حذف می کنه
امیدوارم به دردت بخوره یه مثال از کتاب جعفرنژاد قمی
#include "iostream.h"
#include "stdlib.h"
#include "conio.h"
class node {
friend class linkedList;
private:
char name[30] ;
int stno ;
int unit ;
node *next;
};
class linkedList {
public:
linkedList();
~linkedList();
void enter ();
void display ();
void delNode ();
private:
node *first;
node *last;
};
//***************
linkedList::linkedList()
{
first = last = NULL;
}
//***************
linkedList::~linkedList()
{
node *curPtr = first;
node *temp;
while(curPtr)
{
temp = curPtr;
delete temp;
curPtr = curPtr -> next;
}
}
//***************
void linkedList::enter()
{
node *help;
help = new node;
help -> next = NULL ;
if (first == NULL)
first = last = help ;
else
{
last -> next = help;
last = help;
}
cout << "Enter name of student:";
cin.getline(last -> name, 30) ;
cout << "Enter student number :";
cin >> last -> stno;
cout << "Enter number of unit :";
cin >> last -> unit;
}
//********************
void linkedList::display()
{
node *h;
int i ;
if (first == NULL) {
cout << "List is empty .";
getch() ;
return;
}
h = first ;
clrscr() ;
gotoxy(5,4) ;
cout << "name st.number unit ";
gotoxy(5, 5);
cout <<"--------- --------- -----" ;
i = 6 ;
do {
gotoxy(1, i);
cout << h -> name;
gotoxy(25, i);
cout << h -> stno;
gotoxy(38, i);
cout << h -> unit;
i ++ ;
h = h -> next ;
} while (h != NULL) ;
gotoxy(5, i++) ;
cout << "**********************" ;
cout << "***************" ;
gotoxy(10, i++) ;
cout << "Press a key to continue.";
getch() ;
}
//*****************
void linkedList::delNode()
{
node *h, *p;
int stnum ;
if(!first)
{
cout << "List is empty.";
getch();
return;
}
cout << "Enter student number for delete:";
cin >> stnum ;
h = p = first ;
while (h != NULL)
{
if (h -> stno != stnum)
{
p = h ;
h = h -> next ;
continue ;
}
else { //remove from start
if (h == first) {
first = h -> next ;
delete h ;
delete p ;
break ;
}//end of if
else {
if(h == last)
last = p;
p -> next = h -> next;
delete h ;
break ;
}//end of else
}//end of else
}//end of while
}
//****************
int menu();
//****************
int main ()
{
linkedList list;
while (1)
{
clrscr() ;
switch (menu())
{
case 1:
list.enter() ;
break ;
case 2:
list.delNode() ;
break ;
case 3:
list.display() ;
break ;
case 4:
exit(0) ;
}//end of switch
} //end of while
} // end of main
//********************
int menu()
{
char c[5];
cout << "1.Enter name to list\n";
cout << "2.Remove name from list\n";
cout << "3.Print the list\n";
cout << "4.Exit from program\n";
cout << "Enter your select(1-4):";
cin.getline(c, 5);
return atoi(c);
}

adine2020
26-06-2010, 01:00
این یه برنامه کامل لیست پیوندی
که با کلاس نوشته شده امیدوارم به دردت بخوره
#include "iostream.h"
#include "stdlib.h"
#include "conio.h"
class node {
friend class linkedList;
private:
char name[30] ;
int stno ;
int unit ;
node *next;
};
class linkedList {
public:
linkedList();
~linkedList();
void enter ();
void display ();
void delNode ();
private:
node *first;
node *last;
};
//***************
linkedList::linkedList()
{
first = last = NULL;
}
//***************
linkedList::~linkedList()
{
node *curPtr = first;
node *temp;
while(curPtr)
{
temp = curPtr;
delete temp;
curPtr = curPtr -> next;
}
}
//***************
void linkedList::enter()
{
node *help;
help = new node;
help -> next = NULL ;
if (first == NULL)
first = last = help ;
else
{
last -> next = help;
last = help;
}
cout << "Enter name of student:";
cin.getline(last -> name, 30) ;
cout << "Enter student number :";
cin >> last -> stno;
cout << "Enter number of unit :";
cin >> last -> unit;
}
//********************
void linkedList::display()
{
node *h;
int i ;
if (first == NULL) {
cout << "List is empty .";
getch() ;
return;
}
h = first ;
clrscr() ;
gotoxy(5,4) ;
cout << "name st.number unit ";
gotoxy(5, 5);
cout <<"--------- --------- -----" ;
i = 6 ;
do {
gotoxy(1, i);
cout << h -> name;
gotoxy(25, i);
cout << h -> stno;
gotoxy(38, i);
cout << h -> unit;
i ++ ;
h = h -> next ;
} while (h != NULL) ;
gotoxy(5, i++) ;
cout << "**********************" ;
cout << "***************" ;
gotoxy(10, i++) ;
cout << "Press a key to continue.";
getch() ;
}
//*****************
void linkedList::delNode()
{
node *h, *p;
int stnum ;
if(!first)
{
cout << "List is empty.";
getch();
return;
}
cout << "Enter student number for delete:";
cin >> stnum ;
h = p = first ;
while (h != NULL)
{
if (h -> stno != stnum)
{
p = h ;
h = h -> next ;
continue ;
}
else { //remove from start
if (h == first) {
first = h -> next ;
delete h ;
delete p ;
break ;
}//end of if
else {
if(h == last)
last = p;
p -> next = h -> next;
delete h ;
break ;
}//end of else
}//end of else
}//end of while
}
//****************
int menu();
//****************
int main ()
{
linkedList list;
while (1)
{
clrscr() ;
switch (menu())
{
case 1:
list.enter() ;
break ;
case 2:
list.delNode() ;
break ;
case 3:
list.display() ;
break ;
case 4:
exit(0) ;
}//end of switch
} //end of while
} // end of main
//********************
int menu()
{
char c[5];
cout << "1.Enter name to list\n";
cout << "2.Remove name from list\n";
cout << "3.Print the list\n";
cout << "4.Exit from program\n";
cout << "Enter your select(1-4):";
cin.getline(c, 5);
return atoi(c);
}