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


You are not logged in. this website requires you to be logged in. please log in and try again !
A2Pro THPT Buôn Ma Thuột [2007-2010]

Thành lập năm 2008, từng là một diễn đàn lớp với tất cả tình cảm mình gửi gắm vào. Giờ đây là nơi lưu trữ những thứ lặt vặt mình lượm lặt được trong học tập, công việc và cuộc sống !


CTDL&GT - List

Share

FR
Silver Dragon
Silver Dragon

Gender : Nam

Posts Posts : 1054
Thanked : 660
Coins Coins : 6411
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 ?

CTDL&GT - List

Bài gửi by FR on 10/10/2012, 01:31

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 : 1054
Thanked : 660
Coins Coins : 6411
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 ?

Re: CTDL&GT - List

Bài gửi by FR on 10/10/2012, 02:36

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: 27/2/2017, 22:51