Created
October 23, 2019 00:06
-
-
Save melvincabatuan/87d64642346aab3f58a5052e19bd8070 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <iostream> | |
// Cobalt-mkc: 10/23/2019 | |
using namespace std; | |
//====== Empty List =========/ | |
/* list element definition */ | |
struct node | |
{ | |
int item; // data | |
node *next; // pointer to next item | |
}; | |
node *head = NULL; // list entry point (or front) | |
int count = 0; | |
//---------------------------// | |
/* Minimal operations */ | |
void addFront(int value); | |
bool empty(); | |
void displayList(); | |
int size(); | |
//=========================// | |
/* Implementations */ | |
void addFront(int value) | |
{ | |
// 1. Create a new node | |
node *temp = new node; | |
// 2. Assign data to new node | |
temp->item = value; | |
// 3. Set pointer to next | |
if (empty()) | |
{ | |
temp->next = NULL; // first item | |
} | |
else | |
{ | |
temp->next = head; // insert in front of head | |
} | |
// 4. Set new node as head (front) | |
head = temp; | |
// 5. Increment count | |
++count; | |
return; | |
} | |
void displayList(){ | |
node *current = head; | |
while(current != NULL){ | |
cout << current->item << endl; | |
current = current->next; | |
} | |
} | |
bool empty(){ | |
return head == NULL; | |
} | |
int size(){ | |
return count; | |
} | |
int main(){ | |
for(int i = 1; i < 11; ++i){ | |
addFront(i); | |
} | |
cout << "List size = " << size() << endl; | |
displayList(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment