Friday, July 13, 2012

LinkedList

In C++
/*
 * part1.h
 *
 */

#ifndef PART1_H_
#define PART1_H_

#pragma once

//---------------------------------------------------------------------------
struct CarPart
{
 long     PartNumber;
 char     PartName[40];
 double   UnitPrice;
 CarPart* Next;

};

//---------------------------------------------------------------------------
class ListOfParts
{
 int size;

public:
 ListOfParts();
 int Count();
 CarPart* Head;

 int Add(CarPart* Item);
 CarPart *Retrieve(int pos);
 bool Delete();
};

#endif /* PART1_H_ */

#include <iostream>
#include "parts1.h"
using namespace std;         //otherwise std::cout


//---------------------------------------------------------------------------
ListOfParts::ListOfParts()  //constructor
// : size(0), Head(NULL)
{
 size = 0;
 Head = NULL;  // either way : size(0), Head(BULL)
}
//---------------------------------------------------------------------------
int ListOfParts::Count()
{
 return size;
}
//---------------------------------------------------------------------------

int ListOfParts::Add(CarPart *NewItem)
{
 CarPart *Node = new CarPart;  //add struct CarPart into the class

 Node          = NewItem;
 Node->Next    = Head;   //of the struct pointer Next
 Head          = Node;   // of class pointer Head
 return size++;
}
//---------------------------------------------------------------------------
CarPart *ListOfParts::Retrieve(int Position)
{
 CarPart *Current = Head;
 for(int i = 0; i < Position && Current != NULL; i++)
 {
  Current = Current->Next;
 }
 return Current;
}
//---------------------------------------------------------------------------
bool ListOfParts::Delete()
{
 if( Head == NULL )
 {
  cout << "The list is empty\n";
  return false;
 }
 else
 {
  CarPart *Current;

  Current = Head->Next;
  Head->Next = Current->Next;
  size--;
  return true;
 }
}

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

#include "parts1.h"


using namespace std;

//---------------------------------------------------------------------------
int main()
{
 ListOfParts *Parts = new ListOfParts();     //class
 CarPart     *Part;       //struct

 // Visual C++ 6 can't "scope" a variable in a for loop
 int i;

 Part  = new CarPart;
 Part->PartNumber = 9743;
 strcpy(Part->PartName, "Air Filter");
 Part->UnitPrice  = 8.75;
 Parts->Add(Part);

 Part  = new CarPart;
 Part->PartNumber = 27487;
 strcpy(Part->PartName, "Clutch Disk");
 Part->UnitPrice  = 47.15;
 Parts->Add(Part);

 Part  = new CarPart;
 Part->PartNumber = 87873;
 strcpy(Part->PartName, "Brake Disk");
 Part->UnitPrice  = 35.15;
 Parts->Add(Part);

 Part  = new CarPart;
 Part->PartNumber = 27644;
 strcpy(Part->PartName, "A/C Filter Drier");
 Part->UnitPrice  = 55.55;
 Parts->Add(Part);

 cout << "Number of Parts: " << Parts->Count() << endl;

 cout << "\n-=- List of Parts -=-";
 for(i = 0; i < Parts->Count(); i++)
 {
  CarPart* One = Parts->Retrieve(i);

  cout << "\nCar Part Information";
  cout << "\nPart #:      " << One->PartNumber;
  cout << "\nDescription: " << One->PartName;
  cout << "\nUnit Price: $" << One->UnitPrice << endl;
 }

 Parts->Delete();

 cout << "\nNumber of Parts: " << Parts->Count() << endl;

 cout << "\n-=- List of Parts -=-";
 for(i = 0; i < Parts->Count(); i++)
 {
  CarPart* One = Parts->Retrieve(i);

  cout << "\nCar Part Information";
  cout << "\nPart #:      " << One->PartNumber;
  cout << "\nDescription: " << One->PartName;
  cout << "\nUnit Price: $" << One->UnitPrice << endl;
 }

 return 0;
}
Output:
Number of Parts: 4

-=- List of Parts -=-
Car Part Information
Part #:      27644
Description: A/C Filter Drier
Unit Price: $55.55

Car Part Information
Part #:      87873
Description: Brake Disk
Unit Price: $35.15

Car Part Information
Part #:      27487
Description: Clutch Disk
Unit Price: $47.15

Car Part Information
Part #:      9743
Description: Air Filter
Unit Price: $8.75

Number of Parts: 3

-=- List of Parts -=-
Car Part Information
Part #:      27644
Description: A/C Filter Drier
Unit Price: $55.55

Car Part Information
Part #:      27487
Description: Clutch Disk
Unit Price: $47.15

Car Part Information
Part #:      9743
Description: Air Filter
Unit Price: $8.75
In Java, it is much simple with a lot of less lines. Java has so many rules but it can be easy when you are master of the rules. I try to output the same format as C++. But it does more for example removeLast/removeFirst/getFirst/getLast item of the list and so on....LinkedList is a part of java.util.* so it is much easy.
//http://www.javatutorialhub.com/java-arrays.html
import java.util.*;

class CarPart{
 long     PartNumber;
 String   PartName;
 double    UnitPrice;
 CarPart(int i, String s, double j){
   PartNumber = i;
   PartName = s;
   UnitPrice = j;
 }
   public String toString() {
        
        System.out.println("Car Part Information");
     return "Part #:  "+ PartNumber + "\n" +"Description: "+ PartName + "\n" + "Unit Price " + UnitPrice;
    }
 
}

class LinkedListPart{
   public static void main(String args[]){
     LinkedList<CarPart> parts = new LinkedList<CarPart>();
     parts.add(new CarPart(9743,"Air Filter",8.75));
     parts.add(new CarPart(27487,"Clutch Disk",47.15));
     parts.add(new CarPart(87873,"Brake Disk",35.15));
     parts.add(new CarPart(27644,"A/C Filter Drier",55.55));
     System.out.println("Number of Parts " + parts.size());
     for (CarPart element : parts) 
       System.out.println(element + "\n");
     parts.remove(0); 
     System.out.println("--After remove first part from LinkedList---"); 
     for (CarPart element : parts) 
       System.out.println(element + "\n");
     parts.removeLast();
     System.out.println("---After remove last part from LinkedList---"); 
      for (CarPart element : parts) 
       System.out.println(element + "\n");
  }
}

Output:
Number of Parts 4
Car Part Information
Part #:  9743
Description: Air Filter
Unit Price 8.75

Car Part Information
Part #:  27487
Description: Clutch Disk
Unit Price 47.15

Car Part Information
Part #:  87873
Description: Brake Disk
Unit Price 35.15

Car Part Information
Part #:  27644
Description: A/C Filter Drier
Unit Price 55.55

--After remove first part from LinkedList---
Car Part Information
Part #:  27487
Description: Clutch Disk
Unit Price 47.15

Car Part Information
Part #:  87873
Description: Brake Disk
Unit Price 35.15

Car Part Information
Part #:  27644
Description: A/C Filter Drier
Unit Price 55.55

---After remove last part from LinkedList---
Car Part Information
Part #:  27487
Description: Clutch Disk
Unit Price 47.15

Car Part Information
Part #:  87873
Description: Brake Disk
Unit Price 35.15

No comments: