Professional Documents
Culture Documents
CISP430 - Brandyn Hill - Stacked and Linked Lists
CISP430 - Brandyn Hill - Stacked and Linked Lists
- Brandyn Hill -
Stacked and Linked
List
Page 1 of 27
Circular Array
Source Code: Output:
// Brandyn Hill
#include "iostream"
//the list
#define SIZE 10
char mylist[SIZE];
//Function declarations
char remove(void);
void append(char);
int find(char);
void traverse(void);
int isempty(void);
int main(){
//initialization
append('A');
append('B');
append('C');
append('D');
append('E');
append('F');
Page 2 of 27
traverse();
find('X');
find('D');
traverse();
traverse();
while(!isempty())
traverse();
find('G');}
if(!used){
mylist[tail] =d;
used++;
return;}
//prevent overflow
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()){
return;}
//1 Element
if(used == 1){
cout << "The lise contains " << mylist[head] << endl;
return;}
p = head;
do{
printf("%c",mylist[p]);
p=(p+1)%SIZE;
} while(p != (tail+1)%SIZE);
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];}
//remove data
temp = mylist[head];
used--;
return temp;}
int p;
//empty?
if (isempty()){
return 0; }
if (used == 1){
if (mylist[head] == d){
used = 0;
return 1;}
Page 5 of 27
else{
return 0;}}
p = head;
do{
if(mylist[p] == d) { //found it
while(p != tail) {
p = (p + 1)%SIZE;}
tail--;
if(tail<0)tail=SIZE - 1;
used--;
return 1;}
p = (p + 1)%SIZE;}
return 0;}
Page 6 of 27
Stack List Circular Array Implementation
Source Code: Output:
// Stack Circular Array
// Brandyn Hill
#include "iostream"
//the list
#define SIZE 10
char mylist[SIZE];
//Function declarations
char pop(void);
void q(char);
void push(char);
char peek(void);
void traverse(void);
bool isempty(void);
int main(){
//initialization
push('D');
push('C');
Page 7 of 27
push('B');
push('A');
traverse();
traverse();
peek();
peek();
traverse();
while(!isempty())
traverse();}
if(!used){
mylist[head] =d;
used++;
return;}
//prevent overflow
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];}
//remove data
temp = mylist[head];
used--;
return temp;}
char peek(void){
char p; // Pointer
//empty list
if(isempty()){
return -1;}
//1 Element
if(used == 1){
cout << "The head of the list contains " << mylist[head] << endl;}
p = head;
Page 9 of 27
printf("The head of the list contains ");
printf("%c",mylist[p]);
void traverse(void){
char p; // Pointer
//empty list
if(isempty()){
return;}
//1 Element
if(used == 1){
cout << "The lise contains " << mylist[head] << endl;
return;}
p = head;
do{
printf("%c",mylist[p]);
p=(p+1)%SIZE;
} while(p != (tail+1)%SIZE);
bool isempty(void){
if(used)
return false;
else
return true;}
Page 10 of 27
Queue Circular Array Implementation
Source Code: Output:
// Circular List Queue
// Brandyn Hill
#include "iostream"
#define SIZE 10
char mylist[SIZE];
//Function declarations
char dq(void);
void q(char);
void traverse(void);
bool isempty(void);
int main(){
//initialization
q('A');
q('B');
q('C');
q('D');
traverse();
traverse();
Page 11 of 27
traverse();
while(!isempty())
traverse();}
if(!used){
mylist[tail] =d;
used++;
return;}
//prevent overflow
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];
used--;
return temp;}
void traverse(void){
char p;
if(isempty()){
return;}
if(used == 1){
cout << "The lise contains " << mylist[head] << endl;
return;}
p = head;
do{
printf("%c",mylist[p]);
p=(p+1)%SIZE;
} while(p != (tail+1)%SIZE);
bool isempty(void){
if(used)
return false;
else
return true;}
Page 13 of 27
Priority Queue Circular Array
Implementation
Source Code: Output:
// Circular List Priority Queue
// Brandyn Hill
#include "iostream"
//the list
#define SIZE 10
char mylist[SIZE];
//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);
int main(){
//initialization
append('A');
Page 14 of 27
append('B');
append('C');
append('D');
append('E');
append('F');
insert('Z');
traverse();
find('X');
find('D');
traverse();
pInsert('T');
traverse();
pInsert('V');
traverse();
traverse();
peek();
while(!isempty())
traverse();
find('G');}
Page 15 of 27
//recieve a data elelment and appends it to the tail of the list
if(!used){
mylist[tail] =d;
used++;
return;}
//prevent overflow
return;}
//append data
tail=(tail + 1)%SIZE;
mylist[tail] = d;
used++;}
if(!used){
mylist[head] =d;
used++;
return;}
//prevent overflow
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()){
return -1;}
//1 Element
if(used == 1){
cout << "The head of the list contains " << mylist[head] << endl;}
p = head;
printf("%c",mylist[p]);
void traverse(void){
char p; // Pointer
//empty list
if(isempty()){
return;}
//1 Element
if(used == 1){
cout << "The lise contains " << mylist[head] << endl;
return;}
Page 17 of 27
//more than 1 element
p = head;
do{
printf("%c",mylist[p]);
p=(p+1)%SIZE;
} while(p != (tail+1)%SIZE);
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];}
//remove data
temp = mylist[head];
Page 18 of 27
used--;
return temp;}
int p;
//empty?
if (isempty()){
return 0; }
if (used == 1){
if (mylist[head] == d){
used = 0;
return 1;}
else{
return 0;}}
p = head;
do{
if(mylist[p] == d) { //found it
while(p != tail) {
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;}
return 0;}
int p;
int temp;
//empty?
if(!used){
mylist[head] =d;
used++;
return;}
if (used == 1){
if (mylist[head] == d){
used = 0;
return;}
else{
return;}
p = head;
do{
Page 20 of 27
if(mylist[p] <= d)
{ //found it
return;
//append data
used++;
tail++;
mylist[p] = d;
return;
else
p++;}
return;}
Page 21 of 27
Linked List
Source Code: Output:
// Linked List Implementation
// Brandyn Hill
#include <iostream>
//our node
struct node {
node *next;
char d;
};
node*tail =0;
// function declarations
char remove(void);
int find(char);
void traverse(void);
int isempty(void);
int main(){
append('A');
append('B');
append('C');
Page 22 of 27
append('D');
append('F');
traverse();
find('X');
find('D');
traverse();
traverse();
while(isempty())
cout<< endl;
traverse();
find('G');
p-> next = 0;
p-> d = d;
if (!(head)){
else{
Page 23 of 27
tail = p;
void traverse(void){
node *p = head;
while(p)
p = p -> next;
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;
if (!head)
Page 24 of 27
return -1;
if (head == tail){
delete head;
head = tail = 0;
return temp;
p=head;
temp = p -> d;
delete p;
return temp;
//if the char is found, removes the char element and returns 1, otherwise returns 0
node *c;
node *pc;
//empty?
if (!head){
return 0;
Page 25 of 27
if (head == tail){
//destroy node
delete head;
head = tail = 0;
return 1;
else
return 0;
pc = head;
delete pc;
return 1;
while(c){
Page 26 of 27
if (c == tail)
tail = pc;
//destroy ode
delete c;
return 1;}
pc = c;
c = c -> next;}
return 0;}
Page 27 of 27