A2Pro THPT Buôn Ma Thuột [2007-2010]

Có một nơi mà chúng ta sẽ không bao giờ quên.. nơi thấm đẫm tình bạn, tình yêu thương thân thiết, nơi gắn bó mãi mãi với những kí ức đẹp nhất của thời học sinh yêu dấu... Nơi đó là đây !

Đăng Nhập

Quên mật khẩu

Latest topics

» 「△△」の資料送付ありがとうございます
by FR Wed Jan 18, 2017 7:51 am

» CV
by ƒ®ï€ñð Sun Aug 28, 2016 8:40 pm

» 50 trạng từ hay xuất hiện trong bài thi JLPT
by ƒ®ï€ñð Sat Jul 02, 2016 1:23 pm

» Chia sẻ kỹ năng làm bài nghe N2-N1
by ƒ®ï€ñð Sun Jun 26, 2016 8:15 am

» 自動運転は必要である。
by ƒ®ï€ñð Wed Jun 22, 2016 12:43 am

» 遺伝子検診断
by ƒ®ï€ñð Sun Jun 12, 2016 10:38 pm

» Phân biệt 「~の」「~こと」
by ƒ®ï€ñð Thu May 05, 2016 9:50 am

» 私の宝物
by ƒ®ï€ñð Sat Apr 16, 2016 11:37 pm

» ~「くらい」と~「ぐらい」の違いは何?
by ƒ®ï€ñð Fri Apr 15, 2016 10:06 pm

» 私の国の人々に広げたい日本の文化です
by ƒ®ï€ñð Mon Apr 11, 2016 11:12 pm


CTDL&GT - List

Share

FR
Silver Dragon
Silver Dragon

Gender : Nam

Posts Posts : 1024 Thanked : 660
Coins Coins : 6245
Tham Gia : 26/08/2008
Birthday : 05/08/1920

Status Status : Sẽ ra sao nếu như, tao là mày và mày là tao ?

code CTDL&GT - List

Bài gửi by FR on Wed Oct 10, 2012 1:31 am

Code:
#ifndef __LIST__CPP__
#define __LIST__CPP__

#include "List.h"

List::List() {
   pHead = NULL;
   count = 0;
}

void List::addFirst(int newdata) {
   Node* pTemp = new Node;
   pTemp->data = newdata;
   pTemp->next = pHead;
   pHead = pTemp;
   count++;
}

void List::addLast(int newdata){
   Node *pTail = pHead;
   if(pHead == NULL){
      addFirst(newdata);
   }
   else{
      while(pTail->next != NULL){
         pTail = pTail->next;
      }
      Node *pTemp = new Node();
      pTemp->data = newdata;
      pTemp->next = NULL;
      pTail->next = pTemp;
//      pTail = pTemp;
      count ++;
   }
}

void List::clear()
{
   while (! empty()) removeFirst();
   return;
}

List::~List()  {
   Node* pTemp = pHead;
   while (pTemp!=NULL) {
      pTemp = pTemp->next;
      delete pHead;
      pHead = pTemp;
   }
}

void List::display() {
   Node* pTemp = pHead;
   while (pTemp!=NULL)
   {
      cout << pTemp->data<<", ";
      pTemp = pTemp->next;
   }
   cout<<endl;
}

void List::addConstant(int nConstant)
{
   if(pHead == NULL)
      return;
   Node *pTemp = pHead->next;
   if(pTemp == NULL){
      pHead->data += nConstant;
      return;
   }
   while(pTemp->next != NULL)
      pTemp = pTemp->next;
   pTemp->data += nConstant;
}

void List::reverseList(){
   Node* pHeadOld = pHead;
   Node* pTemp = NULL;
   pHead = NULL;
   while(pHeadOld != NULL){
      pTemp = pHeadOld;
      pHeadOld = pHeadOld->next;
      pTemp->next = pHead;
      pHead = pTemp;
   }
}

void List::addPoly(List *f2)
{
   int min_size = size() < f2->size() ? size() : f2->size();
   reverseList();
   f2->reverseList();
   Node *pTemp = pHead;
   int i = 1;
   int data = 0;
   while(i <= min_size){
      f2->retrieve(i,data);
      pTemp->data += data;
      pTemp = pTemp->next;
      i++;
   }
   i = min_size + 1;
   reverseList();
   if(size() < f2->size()){
      while(i <= f2->size()){
         f2->retrieve(i,data);
         addFirst(data);
         i++;
      }
      count = f2->size();
   }
   f2->reverseList();
   return;
}

int List::empty()
{
   return (pHead==NULL);
}

void List::removeFirst()
{
   if (pHead!=NULL)
   {
      Node* pTemp = pHead;
      pHead = pHead->next;
      delete pTemp;
      count --;
   }
   return;
}

int List::size()
{
   return count;
}

void List::retrieve(int i,int &data){
   if(i > count || i <= 0) return;
   Node *pTemp = pHead;
   while(i != 1){
      pTemp = pTemp->next;
      i--;
   }
   data = pTemp->data;
}

void List::printPoly(){
   int exp = count - 1;
   Node *pTemp = pHead;
   cout<<pTemp->data<<"x^"<<exp;
   pTemp = pTemp->next;
   exp --;
   while(exp > 0){
      if(pTemp->data > 0){
         cout<<" + "<<pTemp->data<<"x^"<<exp;
      }
      else if(pTemp->data   < 0){
         cout<<" "<<pTemp->data<<"x^"<<exp;
      }
      pTemp = pTemp->next;
      exp --;
   }
   if(pTemp->data > 0){
      cout<<" + "<<pTemp->data;
   }
   else{
      cout<<" "<<pTemp->data;
   }
   cout<<endl;
}
#endif

Code:
#ifndef __LIST__H__
#define __LIST__H__

#include <stdio.h>
#include <iostream.h>

class Node {
      public:
    int data;
    Node* next;
};

class List {
   private:
   int count;
   Node* pHead;
   public:
   List() ;
   void addFirst(int) ;
   void addLast(int);
   void display();
   void addConstant(int);
   void addPoly(List *);
   int empty();
   void removeFirst();
   void clear();
   void reverseList();
   int size();
   void retrieve(int, int &);
   void printPoly();
   void appendList(List *);
   void subPoly(List *);
   void divPoly(List *);
   ~List();   
};

#endif

Code:
#include "List.h"
#include "List.cpp"

int main()
{
   List *aList = new List();
   List *aList2 = new List();
   aList->addFirst(1);
   aList->addFirst(2);
   aList->addFirst(2);
   aList->addFirst(4);
//   aList->addFirst(5);
   aList->display();

   aList2->addFirst(1);
   aList2->addFirst(2);
   
   aList->addConstant(5);
   aList->display();
   
   aList->addPoly(aList2);
   aList->display();

   aList->printPoly();


   return 0;
}

FR
Silver Dragon
Silver Dragon

Gender : Nam

Posts Posts : 1024 Thanked : 660
Coins Coins : 6245
Tham Gia : 26/08/2008
Birthday : 05/08/1920

Status Status : Sẽ ra sao nếu như, tao là mày và mày là tao ?

code Re: CTDL&GT - List

Bài gửi by FR on Wed Oct 10, 2012 2:36 am

Stack


Code:
Stack::Stack()
{
top=NULL;
count = 0;
}

Stack::~Stack()
{

}

//==================================
void Stack::Push(int x)
{
Node *pNew = new Node();
if(pNew->next != NULL)
{
pNew->data = x;
pNew->next = top;
top = pNew;
count++;
}
}
//==================================
void Stack::Pop (int &x)
{
Node *pDel= new Node();
if (count > 0)
{
pDel->next = top;
x = top->data;
top = top->next;
count--;
delete pDel;
}
else
cout<<"Stack Rong Khong The Pop DL"<<endl;
}
//================================================
void Stack::clear()
{
Node *temp = new Node();
while (top != NULL)
{
temp = top;
top = top->next;
delete temp;
}
count = 0;
}
//==================================
void Stack::PrintAll()
{
Node *temp = new Node();
temp = top;
while (temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
}
//====================================
int Stack::GetSize()
{
return count;
}
//====================================
int Stack::IsEmpty()
{
return (count == 0);
}
//====================================
int Stack::GetStackTop(int &x)
{
if (count == 0)
return 0;
else
{
x= top->data;
return 1;
}
}
//=====================================
void Stack::CovertDecimalToBinary(int n)
{
cout<<n<<" doi thanh: ";
while (n > 0)
{
int x;
x = n%2;
n= n/2;
Push(x);
}

Node *temp = new Node();
temp = top;
while (temp != NULL)
{
cout<<temp->data;
temp = temp->next;
}
}

void Stack::ReserveStack()
{
Stack stack;
Node *temp = new Node();
temp1 = top;
while (temp1 != NULL )
{
stack.Push(temp1->data);
temp1= temp1->next;
}

//int Count = stack.GetSize();
//Node *temp2 = new Node();
//temp = stack.
//while ()

}

____________________________

Stack::Stack()
{
count = 0;
}

Stack::~Stack()
{
Clear();
}
void Stack::Push(int x)
{

a[count] = x;
count++;
}

void Stack::Pop(int &x)
{

x = a[count-1];
count--;
}

void Stack::PrintAll()
{
for (int i= 0; i<count; i++)
{
cout<<a[i]<<" ";
}
}

void Stack::Clear()
{
count=0;
}

int Stack::GetSize()
{
return count;
}


    Hôm nay: Fri Jan 20, 2017 4:53 pm