PDA

View Full Version : لیست پیوندی



Shiva.K
جمعه 07 خرداد 1389, 19:15 عصر
کدسی++ حذف و اضافه کردن یک ند رو دارم ولی نوشتن یک برنامه مثل لیست نمرات و شماره دانشجویی که با آرایه به راحتی نوشته میشه خیلی سخته.

Help me please:ناراحت::اشتباه:

tdkhakpur
جمعه 07 خرداد 1389, 19:18 عصر
خب مشکل کجاست؟داخل برنامه شما لیست پیوندی چه ارتباطی با آرایه دارد؟

Shiva.K
دوشنبه 10 خرداد 1389, 09:02 صبح
نه، ربطی به آرایه نداره فقط از اونجایی که لیست پیوندی و آرایه تفاوت عمدشون طرز قرار گرفتن و دسترسیشون در حافظه ست خوب کد نویسی با آرایه ها خیلی راحتترهو نمی دونم شایدم چون موضوع برام جدیده این طور فکر می کنم. مشکل تو برنامه ی زیره!:عصبانی++:
error: namespace ,current


#include <iostream>

using namespace std;

struct NodeType {
int data;
NodeType *next;
};

class linklist {
private:
NodeType *first;
NodeType *last;
public:

linklist() {
first = new NodeType();
last = new NodeType();
first = last = NULL;
}
~linklist();
void insertback();
void insertforward();
void delnode(int); // delete int from linklist
void showlist(); //print linklist
// void addnodeback(int,int); //insert 1 befor 2
// void sortlink(); //sort lnklist
};

linklist::~linklist() {
NodeType *temp, *current = first;
while (current) {
temp = current;
current = current->next;
delete temp;
}
first = NULL;
}

void linklist::insertback() {

int num;
cout << "enter num. end with -999";
cin >> num;
while (num != (-999)) {
NodeType *newnode;
newnode = new NodeType();
newnode->next = NULL;
newnode->data = num;
if (first == NULL) {
first = newnode;
last = newnode;
} else {
newnode->next = first;
first = newnode;
}
cin >> num;

}
}

void linklist::insertforward() {

int num;
cout << "enter number. end with -999";
cin >> num;
while (num != -999) {
NodeType *newnode;
newnode = new NodeType();
newnode ->next = NULL;
newnode ->data = num;
if (first == NULL) {
first = newnode;
last = newnode;
} else {
last->next = newnode;
last = newnode;
}
cin >> num;
}
}

// for print the list
void linklist::showlist() {
if (first == NULL) {
cout << "list is empty.";
return;
}
NodeType *current;
current = new NodeType();
current = first;
while (current != NULL) {
cout << current->data << " ";
current = current->next;
}
}



//for delete a from the listnode
void linklist::delnode(int x) {
if (first == NULL) {
cout << "list is empty.";
return;
}
if (first->next == NULL) {
NodeType *temp, *current = first;
while (current) {
temp = current;
current = current->next;
delete temp;
}
first = NULL;
return;
}
NodeType *current = new NodeType();
current = first;
while (current) {
if (current->next->data == x) {
NodeType *p = new NodeType();
p->next = current->next;
current->next = current->next->next;
delete p;
return;
}
current = current->next;
}
cout << "\n" << x << "deleted.\n";
}

int main() {
linklist n;
int a;
n.insertforward();
cout << "\n\n\n";
n.showlist();
cout << "\ndelete :";
cin >> a;
n.delnode(a);
cout << "\n\n\n";
n.showlist();
return 0;
}

tdkhakpur
دوشنبه 10 خرداد 1389, 12:39 عصر
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <dos.h>
#include <iostream>
using namespace std;
struct NodeType {
int data;
NodeType *next;
NodeType *prev;
};
class linklist {
private:
NodeType *first, *last;
public:
linklist() {
last = first = NULL;
}
~linklist();
// void insertback();
void insertforward();
void delnode(int); // delete int from linklist
void showlist(); //print linklist
// void addnodeback(int,int); //insert 1 befor 2
// void sortlink(); //sort lnklist
};
linklist::~linklist() {
NodeType *temp, *cur;
cur = first;
do{
temp = cur->next;
delete []cur;
cur = temp;
}while(cur!=NULL);
first = last = NULL;
}
void linklist::insertforward()
{
int num;
NodeType *newnode;
cout << "enter num. end with -999";
cin >> num;
while (num != (-999)) {
if( first == NULL ){
first = last = newnode = new NodeType();
newnode->next = newnode->prev = NULL;
}else{
newnode = new NodeType();
newnode->next = NULL;
newnode->prev = last;
last->next = newnode;
last = newnode;
}
newnode->data = num;
cin >> num;
}
}
// for print the list
void linklist::showlist() {
NodeType *current;
if (first == NULL) {
cout << "list is empty.";
return;
}
current = first;
while (current != NULL) {
cout << current->data << " ";
current = current->next;
}
}

//for delete a from the listnode
void linklist::delnode(int x) {
NodeType *temp = first, *cur;
for( int i=0; i<x && temp!=NULL; i++)
temp = temp->next;
if (temp == NULL) {
cout << "list is empty.";
return;
}else{
cur = temp;
temp = temp->prev;
temp->next = cur->next;
temp = cur->next;
temp->prev = cur->prev;
delete [] cur;
cout << "\n" << x << "deleted.\n";
}
}
int main() {
linklist n;
int a;
n.insertforward();
cout << "\n\n\n";
n.showlist();
cout << "\ndelete :";
cin >> a;
n.delnode(a);
cout << "\n\n\n";
n.showlist();
return 0;
}

farzadyazdan
سه شنبه 11 خرداد 1389, 11:23 صبح
سلام دوست عزیز
یه برنامه تو سایت www.downloadproje.com (http://www.downloadproje.com)هست که می تونی اون رو دانلود کنی و ازش استفاده کنی من خودم هم از این برنامه استفاده کردم. فکر می کنم مشکلت حل بشه.