Copy... Copy → Paste !

Share
Xem chủ đề cũ hơnXem chủ đề mới hơn
avatar
Silver Dragon
Silver Dragon
Gender : Nam

Posts Posts : 1094
Thanked : 660
Coins Coins : 6987
Tham Gia : 26/08/2008
Birthday : 05/08/1920

Xem lý lịch thành viên
10102012
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;
}
Share this post on:Excite BookmarksDiggRedditDel.icio.usGoogleLiveSlashdotNetscapeTechnoratiStumbleUponNewsvineFurlYahooSmarking

Comments

avatar
on Wed 10 Oct 2012, 02:36FR
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;
}

Xem chủ đề cũ hơnXem chủ đề mới hơn
Permissions in this forum:
Bạn không có quyền trả lời bài viết