Created
December 3, 2012 01:59
-
-
Save MosheBerman/4192122 to your computer and use it in GitHub Desktop.
Linked List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Widget.cpp | |
// 3-LinkedLists | |
// | |
// Created by Moshe Berman on 12/2/12. | |
// Copyright (c) 2012 Moshe Berman. All rights reserved. | |
// | |
#include "Widget.h" | |
Widget *Widget::first = NULL; | |
Widget *Widget::last = NULL; | |
// | |
// Linked List | |
// | |
void Widget::enqueue(Widget *widget){ | |
if (last != NULL) { | |
last->next = widget; | |
} | |
if (first == NULL) { | |
first = widget; // If the list was empty, set first widget | |
} | |
last = widget; | |
} | |
Widget* Widget::dequeue(){ | |
Widget *widget = first; | |
if (widget != NULL) { | |
first = widget->next; | |
} | |
return widget; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Widget.h | |
// 3-LinkedLists | |
// | |
// Created by Moshe Berman on 12/2/12. | |
// Copyright (c) 2012 Moshe Berman. All rights reserved. | |
// | |
#ifndef ____LinkedLists__Widget__ | |
#define ____LinkedLists__Widget__ | |
#include <iostream> | |
class Widget { | |
public: | |
Widget():price(0), quantity(0){ | |
next = NULL; | |
if (first == NULL) { | |
first = this; | |
} | |
last = first; | |
}; | |
Widget(double _price,int _quantity):price(_price), quantity(_quantity){ | |
next = NULL; | |
if (first == NULL) { | |
first = this; | |
} | |
last = first; | |
}; | |
double price; | |
int quantity; | |
// Linked List | |
Widget *next; | |
static Widget *first; | |
static Widget *last; | |
void enqueue(Widget *widget); | |
Widget* dequeue(); | |
}; | |
typedef Widget* WidgetPtr; | |
#endif /* defined(____LinkedLists__Widget__) */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment