پی سی سیتی

پی سی سیتی (http://p30city.net/index.php)
-   زبان های برنامه نویسی Programming (http://p30city.net/forumdisplay.php?f=12)
-   -   لیست پیوندی (http://p30city.net/showthread.php?t=17983)

adine2020 12-13-2009 08:58 PM

لیست پیوندی
 
من یه برنامه ساده می خوام در سی پلاس پلاس
که لیست پیوندی رو با کلاس پیاده سازی کنه
اگه ممکنه در مورد هر کدوم از توابع کلاس و کاری که انجام میده هم توضیح بده
مرسی

دانه کولانه 12-13-2009 09:05 PM

لیست پیوندی - جعفرنژاد قمی
 
این کلاس لیست پیوندی هست که من خودم ازش استفاده میکنم مال جعفر نژاد قمی هست


کد:

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


class node {
              friend class linkList;
              char name[20];
              int stno;
              float ave;
          node *next;
};
//************************************
class linkList{
      public:
                linkList();
            ~linkList();
                void addNode();
            void delNode(int &);
                void display();
      private:
                  node *first;
              node *last;
};
//************************************
linkList::linkList()
{
                          first=last=NULL;
}
//*********************************
linkList::~linkList()
{
                    node *curPtr=first;
                            node *temp;
                    while(curPtr){
                                              temp=curPtr;
                                  curPtr=curPtr->next;
                                              delete temp;
                    }
}
//************************************************88

void linkList::delNode(int &no)
{
    node *curPtr=first,*nextPtr=first;
      while(nextPtr){
          if(no==nextPtr->stno)
                  if(nextPtr==first){
                  first=first->next;
                        delete nextPtr;
                  break;
                  }
              else {
                        if(nextPtr==last)
                  last=curPtr;
                        curPtr->next=nextPtr->next;
                  delete nextPtr;
                        break;
              }
                else{
                curPtr=nextPtr;
                      nextPtr=nextPtr->next;
                }
                  }
          }

         
//*******************************************************8

void linkList::addNode()
{
      node *newPtr=new node;
    if(!newPtr){cout<<"not allocation;",getch();}
      newPtr->next=NULL;
      clrscr();
      cout<<"Name      "<<"stno    "<<"  aver\n";
      gotoxy(1,2);
      cin>>newPtr->name;
      gotoxy(20,2);
      cin>>newPtr->stno;
      gotoxy(30,2);
      cin>>newPtr->ave;
      if(first==NULL)
      first=last=newPtr;
      else{
            last->next=newPtr;
            last=newPtr;
            }
}

void linkList::display()
{
      int r=2;
      clrscr();
      cout<<"name          "<<"sn      "<<"      avra\n";
      node *curPtr=first;
      while(curPtr){
                        gotoxy(1,r);
                        cout<<curPtr->name<<"  ";
                        gotoxy(20,r);
                        cout<<curPtr->stno<<" ";
                        gotoxy(30,r);
                        cout<<curPtr->ave<<"  ";
                        curPtr=curPtr->next;
                        r++;
                        }
}
//888888888888888888888888888888888888888888888888888888

int menu();

int main()
{
    int no;
    linkList testList;
    for(;;){
                clrscr();
                switch(menu()){
                                    case 1:testList.addNode();break;
                                    case 2:
                                          cout<<"enter stno to delet";
                                          cin>>no;
                                          testList.delNode(no);break;
                                          case 3: testList.display();getch();break;
                                          case 4: exit(1);
                                          }
                                          }
}

int menu()
{
    int choice;
    cout<<"1 enter a studn\n"
          <<"2 delet \n"
          <<"3 disp \n"
          <<"4 end prog\n";
          cout<<"\n enter your select (1-4)";
          cin>>choice;
          return choice;
}


دانه کولانه 12-13-2009 09:10 PM

کلاس لیست پیوند - سورس برنامه سی پلاس پلاس لیست پیوندی LIST NODE C++ source
 
1 فایل پیوست
کلاس لیست پیوند - سورس برنامه سی پلاس پلاس لیست پیوندی LIST NODE C++ source

جواب شما در کمتر از 5 دقیقه


اینم فایل اجرایی اون با حجم پایین با کامپایلر TCW که از اون قدیمیهای خوش دسته {داش مشتی}

زکریا فتاحی 12-30-2009 11:58 AM

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



کد:

/*
 

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



اکنون ساعت 08:59 PM برپایه ساعت جهانی (GMT - گرینویچ) +3.5 می باشد.

Powered by vBulletin® Version 3.8.4 Copyright , Jelsoft Enterprices مدیریت توسط کورش نعلینی
استفاده از مطالب پی سی سیتی بدون ذکر منبع هم پیگرد قانونی ندارد!! (این دیگه به انصاف خودتونه !!)
(اگر مطلبی از شما در سایت ما بدون ذکر نامتان استفاده شده مارا خبر کنید تا آنرا اصلاح کنیم)