موضوع: لیست پیوندی
نمایش پست تنها
  #4  
قدیمی 12-30-2009
زکریا فتاحی آواتار ها
زکریا فتاحی زکریا فتاحی آنلاین نیست.
کاربر فعال
 
تاریخ عضویت: Nov 2008
محل سکونت: پاوه
نوشته ها: 1,479
سپاسها: : 20

120 سپاس در 80 نوشته ایشان در یکماه اخیر
زکریا فتاحی به Yahoo ارسال پیام
پیش فرض

اینم یه لیست پیوندی دیگه.



کد:
/* 
  

#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;
}
__________________

درمان غم عشق بگویم که چه باشد
وصل است و بهار است و می بربط و چنگ است
....
زنهار مرو هیچ سوی بیشه ی عشاق
چنگال غمش تیزتر از چنگال پلنگ است

----
ئه و روژه ی "مه جنون" له زوخاو نوشی ...مه ینه ت فه وتانی، کفنیان پوشی
من بومه واریس له قه وم و خویشی . . . سپاردی به من ئازاری و ئیشی

پاسخ با نقل قول
جای تبلیغات شما اینجا خالیست با ما تماس بگیرید