You are on page 1of 27

CISP430

- Brandyn Hill -
Stacked and Linked
List

Page 1 of 27
Circular Array
Source Code: Output:
// Brandyn Hill

// Original Author: Professor Ross

#include "iostream"

using namespace std;

//the list

#define SIZE 10

char mylist[SIZE];

int head, tail, used;

//Function declarations

char remove(void);

void append(char);

int find(char);

void traverse(void);

int isempty(void);

//main for teststing the access functions

int main(){

//initialization

head = tail = used = 0;

append('A');

append('B');

append('C');

append('D');

append('E');

append('F');

Page 2 of 27
traverse();

find('X');

find('D');

traverse();

cout << "Removed" << remove() << endl;

cout << "Removed" << remove() << endl;

traverse();

//empty the list

cout << "Removed";

while(!isempty())

cout << remove() <<", ";

cout << endl;

traverse();

find('G');}

//recieve a data elelment and appends it to the tail of the list

void append(char d){

//if list is empty

if(!used){

mylist[tail] =d;

used++;

return;}

//prevent overflow

if((tail + 1) % SIZE == head){

cout << "Overflow. elelment not appended\n";

return;}

//append data

tail=(tail + 1)%SIZE;

Page 3 of 27
mylist[tail] = d;

used++;}

void traverse(void){

char p; // Pointer

//empty list

if(isempty()){

cout << "The list is empty.\n";

return;}

//1 Element

if(used == 1){

cout << "The lise contains " << mylist[head] << endl;

return;}

//more than 1 element

p = head;

printf("The list contains \n");

do{

printf("%c",mylist[p]);

p=(p+1)%SIZE;

} while(p != (tail+1)%SIZE);

cout << endl;}

//returns true if the list is empty, returns false otherwise

int isempty(void){

if(used)

return 0;

else

return 1;}

Page 4 of 27
char remove(void){

char temp;

//empty list

if(isempty()){

return -1;}

//1 element

if(used == 1){

used = 0;

return mylist[head];}

//more than 1 element

//remove data

temp = mylist[head];

head = (head + 1)%SIZE;

used--;

return temp;}

int find(char d){

int p;

//empty?

if (isempty()){

return 0; }

//only one node

if (used == 1){

if (mylist[head] == d){

used = 0;

cout << d << "found" << endl;

return 1;}

Page 5 of 27
else{

cout << d << "not found" << endl;

return 0;}}

// more than 1 element

p = head;

do{

if(mylist[p] == d) { //found it

//scoot stuff after p up

while(p != tail) {

mylist[p] = mylist[(p + 1)%SIZE];

p = (p + 1)%SIZE;}

tail--;

if(tail<0)tail=SIZE - 1;

used--;

cout << d << "found" << endl;

return 1;}

p = (p + 1)%SIZE;}

while(p != (tail + 1)% SIZE);

cout << d << "not found" << endl;

return 0;}

Big O Time Complexity: O(n)

Page 6 of 27
Stack List Circular Array Implementation
Source Code: Output:
// Stack Circular Array

// Brandyn Hill

// Original Author: Professor Ross

#include "iostream"

using namespace std;

//the list

#define SIZE 10

char mylist[SIZE];

int head, tail, used;

//Function declarations

char pop(void);

void q(char);

void push(char);

char peek(void);

void traverse(void);

bool isempty(void);

//main for teststing the access functions

int main(){

//initialization

head = tail = used = 0;

push('D');

push('C');

Page 7 of 27
push('B');

push('A');

traverse();

traverse();

cout << "Dequeued " << pop() << endl;

peek();

cout << "Dequeued " << pop() << endl;

peek();

traverse();

//empty the list

cout << "Dequeued ";

while(!isempty())

cout << pop() <<", ";

cout << endl;

traverse();}

//recieve a data elelment and appends it to the tail of the list

void push(char d){

//if list is empty

if(!used){

mylist[head] =d;

used++;

return;}

//prevent overflow

if((head - 1) % SIZE == tail){

cout << "Overflow. elelment not appended\n";

return;}

//append data

Page 8 of 27
head=(head - 1)%SIZE;

mylist[head] = d;

used++;}

char pop(void){

char temp;

if(isempty()){

return -1;}

if(used == 1){

used = 0;

return mylist[head];}

//more than 1 element

//remove data

temp = mylist[head];

head = (head + 1)%SIZE;

used--;

return temp;}

char peek(void){

char p; // Pointer

//empty list

if(isempty()){

cout << "The list is empty.\n";

return -1;}

//1 Element

if(used == 1){

cout << "The head of the list contains " << mylist[head] << endl;}

//more than 1 element

p = head;

Page 9 of 27
printf("The head of the list contains ");

printf("%c",mylist[p]);

cout << endl;}

void traverse(void){

char p; // Pointer

//empty list

if(isempty()){

cout << "The list is empty.\n";

return;}

//1 Element

if(used == 1){

cout << "The lise contains " << mylist[head] << endl;

return;}

//more than 1 element

p = head;

printf("The list contains \n");

do{

printf("%c",mylist[p]);

p=(p+1)%SIZE;

} while(p != (tail+1)%SIZE);

cout << endl;}

bool isempty(void){

if(used)

return false;

else

return true;}

Big O Time Complexity: O(n)

Page 10 of 27
Queue Circular Array Implementation
Source Code: Output:
// Circular List Queue

// Brandyn Hill

// Original Author: Proffessor Ross

#include "iostream"

using namespace std;

#define SIZE 10

char mylist[SIZE];

int head, tail, used;

//Function declarations

char dq(void);

void q(char);

void traverse(void);

bool isempty(void);

int main(){

//initialization

head = tail = used = 0;

q('A');

q('B');

q('C');

q('D');

traverse();

traverse();

cout << "Dequeued " << dq() << endl;

cout << "Dequeued " << dq() << endl;

Page 11 of 27
traverse();

//empty the list

cout << "Dequeued ";

while(!isempty())

cout << dq() <<", ";

cout << endl;

traverse();}

void q(char d){

//if list is empty

if(!used){

mylist[tail] =d;

used++;

return;}

//prevent overflow

if((tail + 1) % SIZE == head){

cout << "Overflow. elelment not appended\n";

return;}

//append data

tail=(tail + 1)%SIZE;

mylist[tail] = d;

used++;}

char dq(void){

char temp;

if(isempty()){

return -1;}

if(used == 1){

used = 0;

Page 12 of 27
return mylist[head];}

temp = mylist[head];

head = (head + 1)%SIZE;

used--;

return temp;}

void traverse(void){

char p;

if(isempty()){

cout << "The list is empty.\n";

return;}

if(used == 1){

cout << "The lise contains " << mylist[head] << endl;

return;}

//more than 1 element

p = head;

printf("The list contains \n");

do{

printf("%c",mylist[p]);

p=(p+1)%SIZE;

} while(p != (tail+1)%SIZE);

cout << endl;}

bool isempty(void){

if(used)

return false;

else

return true;}

Big O Time Complexity: O(n)

Page 13 of 27
Priority Queue Circular Array
Implementation
Source Code: Output:
// Circular List Priority Queue

// Brandyn Hill

// Original Author: Proffessor Ross

#include "iostream"

using namespace std;

//the list

#define SIZE 10

char mylist[SIZE];

int head, tail, used;

//Function declarations

char dq(void);

char peek(void);

void append(char);

void insert(char);

void pInsert(char);

int find(char);

void traverse(void);

bool isempty(void);

//main for teststing the access functions

int main(){

//initialization

head = tail = used = 0;

append('A');

Page 14 of 27
append('B');

append('C');

append('D');

append('E');

append('F');

insert('Z');

traverse();

find('X');

find('D');

traverse();

cout << "Priority insert T," << endl;

pInsert('T');

traverse();

cout << "Priority insert V," << endl;

pInsert('V');

traverse();

cout << "Removed " << dq() << endl;

cout << "Removed " << dq() << endl;

traverse();

peek();

//empty the list

cout << "Removed ";

while(!isempty())

cout << dq() <<", ";

cout << endl;

traverse();

find('G');}

Page 15 of 27
//recieve a data elelment and appends it to the tail of the list

void append(char d){

//if list is empty

if(!used){

mylist[tail] =d;

used++;

return;}

//prevent overflow

if((tail + 1) % SIZE == head){

cout << "Overflow. elelment not appended\n";

return;}

//append data

tail=(tail + 1)%SIZE;

mylist[tail] = d;

used++;}

void insert(char d){

//if list is empty

if(!used){

mylist[head] =d;

used++;

return;}

//prevent overflow

if((head - 1) % SIZE == tail){

cout << "Overflow. elelment not appended\n";

return;}

//append data

Page 16 of 27
head=(head - 1)%SIZE;

mylist[head] = d;

used++;}

char peek(void){

char p; // Pointer

//empty list

if(isempty()){

cout << "The list is empty.\n";

return -1;}

//1 Element

if(used == 1){

cout << "The head of the list contains " << mylist[head] << endl;}

//more than 1 element

p = head;

printf("The head of the list contains ");

printf("%c",mylist[p]);

cout << endl;}

void traverse(void){

char p; // Pointer

//empty list

if(isempty()){

cout << "The list is empty.\n";

return;}

//1 Element

if(used == 1){

cout << "The lise contains " << mylist[head] << endl;

return;}

Page 17 of 27
//more than 1 element

p = head;

printf("The list contains \n");

do{

printf("%c",mylist[p]);

p=(p+1)%SIZE;

} while(p != (tail+1)%SIZE);

cout << endl;}

//returns true if the list is empty, returns false otherwise

bool isempty(void){

if(used)

return false;

else

return true;}

char dq(void){

char temp;

//empty list

if(isempty()){

return -1;}

//1 element

if(used == 1){

used = 0;

return mylist[head];}

//more than 1 element

//remove data

temp = mylist[head];

head = (head + 1)%SIZE;

Page 18 of 27
used--;

return temp;}

int find(char d){

int p;

//empty?

if (isempty()){

return 0; }

//only one node

if (used == 1){

if (mylist[head] == d){

used = 0;

cout << d << "found" << endl;

return 1;}

else{

cout << d << "not found" << endl;

return 0;}}

// more than 1 element

p = head;

do{

if(mylist[p] == d) { //found it

//scoot stuff after p up

while(p != tail) {

mylist[p] = mylist[(p + 1)%SIZE];

p = (p + 1)%SIZE;}

tail--;

if(tail<0)tail=SIZE - 1;

used--;

Page 19 of 27
cout << d << "found" << endl;

return 1;}

p = (p + 1)%SIZE;}

while(p != (tail + 1)% SIZE);

cout << d << "not found" << endl;

return 0;}

void pInsert(char d){

int p;

int temp;

//empty?

if(!used){

mylist[head] =d;

used++;

return;}

//only one node

if (used == 1){

if (mylist[head] == d){

used = 0;

cout << d << "found" << endl;

return;}

else{

cout << d << "not found" << endl;

return;}

// more than 1 element

p = head;

do{

Page 20 of 27
if(mylist[p] <= d)

{ //found it

if((head - 1) % SIZE == tail){

cout << "Overflow. elelment not appended\n";

return;

//append data

used++;

tail++;

for(int i = tail +1; i > p; i--) {

mylist[i] = mylist[(i - 1)];}

mylist[p] = d;

return;

else

p++;}

while(p != (tail + 1)% SIZE);

cout << d << "not found" << endl;

return;}

Big O Time Complexity: O(n!)

Page 21 of 27
Linked List
Source Code: Output:
// Linked List Implementation

// Brandyn Hill

// Original Author: Proffessor Ross

#include <iostream>

using namespace std;

//our node

struct node {

node *next;

char d;

};

// head and null pointers

node *head =0;

node*tail =0;

// function declarations

char remove(void);

void append (char);

int find(char);

void traverse(void);

int isempty(void);

//main to testing the acce function

int main(){

append('A');

append('B');

append('C');

Page 22 of 27
append('D');

append('F');

traverse();

find('X');

find('D');

traverse();

cout << "Removed "<< remove()<< endl;

cout << "Removed "<< remove() << endl;

traverse();

//empty the list

cout << "Removed ";

while(isempty())

cout << remove()<< " , ";

cout<< endl;

traverse();

find('G');

//receives a char element and appends it to the tail of the list

void append(char d){

//makes a new node

node *p = new node;

p-> next = 0;

p-> d = d;

if (!(head)){

head = tail = p;}

else{

tail -> next = p;

Page 23 of 27
tail = p;

void traverse(void){

node *p = head;

cout << "The list contains ";

while(p)

cout << (char) p->d << " ";

p = p -> next;

cout << endl;

//returns true if the list is empty, returns false otherwise

int isempty(void){

if (head)

return 0;

else

return 1;

//removes a char element from the head of the list and returns it

// return -1 if it is empty

char remove(void){

node *p;

char temp;

//retunr null if list is empty

if (!head)

Page 24 of 27
return -1;

// only one node

if (head == tail){

temp = head -> d;

delete head;

head = tail = 0;

return temp;

//more than one node

//remove and destroy head node

p=head;

head = head -> next;

temp = p -> d;

delete p;

return temp;

//searches the list for a char element

//if the char is found, removes the char element and returns 1, otherwise returns 0

int find(char d){

node *c;

node *pc;

//empty?

if (!head){

cout << d << " not found " << endl;

return 0;

//only one node ?

Page 25 of 27
if (head == tail){

if (head -> d == d){

//destroy node

delete head;

head = tail = 0;

cout << d << " not found " << endl;

return 1;

else

cout << d << " not found " << endl;

return 0;

//two or more nodes

pc = head;

c = head -> next;

if ( pc -> d == d) // found it at the end{

head = head -> next;

delete pc;

cout << d << " not found " << endl;

return 1;

//look at nodes after the head node

while(c){

if (c-> d == d)//found it after the head{

pc -> next = c -> next; //take care of tail

Page 26 of 27
if (c == tail)

tail = pc;

//destroy ode

delete c;

cout << d << " not found " << endl;

return 1;}

pc = c;

c = c -> next;}

cout << d << " not found " << endl;

return 0;}

Page 27 of 27

You might also like