home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DP Tool Club 3
/
CDASC03.ISO
/
sorties
/
4140
/
whois_.exe
/
SLLIST.CPP
< prev
next >
Wrap
Text File
|
1991-08-20
|
6KB
|
265 lines
/****************************************************************
File Name: sllist.cpp
Author: Andrew Frantz
Purpose: singly linked list(queue) function source
Date: 2/8/91
Environ: BORLAND C++ v2.0
Keywrd(s): CPP SINGLY LINKED LIST
****************************************************************/
#include "sllist.hpp"
#include <string.h>
// ===============================================================
node *sllist::add_queue(char *fname)
{
node *scan = head;
node *old = NULL;
if(!head) // if no list create list
{
node *temp = new node;
temp->next = NULL;
strcpy(temp->file_name,fname);
head = temp;
return temp;
}
scan = head;
while(scan) // traverse list
{
old = scan; // save previous
scan = (node *) scan->next; // next item
}
// append to end of list
node *temp = new node;
scan = old; // restore previous
scan->next = temp;
strcpy(temp->file_name , fname);
temp->next = NULL;
return temp;
}
// ===============================================================
node *sllist::add_queue(int c)
{
node *scan = head;
node *old = NULL;
if(!head) // if no list create list
{
node *temp = new node;
temp->next = NULL;
temp->rec_num = c;
head = temp;
return temp;
}
scan = head;
while(scan) // traverse list
{
if(c == scan->rec_num)
{
return NULL; // item already in list
}
old = scan; // save previous
scan = (node *) scan->next; // next item
}
// append to end of list
node *temp = new node;
scan = old; // restore previous
scan->next = temp;
temp->rec_num = c;
temp->next = NULL;
return temp;
}
// ===============================================================
void sllist::add_in_p_order(int c)
{
node *scan = head;
node *old = NULL;
if(!head) // if no list create list
{
node *temp = new node;
temp->next = NULL;
temp->rec_num = c;
head = temp;
return;
}
scan = head;
if(c < scan->rec_num) // put item at head of list
{
node *temp = new node; // create space for a new node
temp->next = scan;
temp->rec_num = c; // put in data
head = temp;
return;
}
else
{
while(scan) // traverse list
{
if(c < scan->rec_num)
{
node *temp = new node; // create space for a new node
temp->next = scan; // link to the list
temp->rec_num = c; // put in data
old->next = temp; // backward patch
return; // bye
}
old = scan; // save last scan
scan = (node *) scan->next; // next item
}
}
// append to end of list
node *temp = new node;
scan = old;
scan->next = temp;
temp->rec_num = c;
temp->next = NULL;
}
// ==========================================================
void sllist::del_one(int c)
{
node *scan=head;
node *old = NULL;
if(scan->rec_num == c) // first item in list
{
head = (node *) scan->next;
delete scan;
return;
}
while(scan) // traverse list
{
if(c == scan->rec_num)
{
old->next = (node *) scan->next;
delete scan;
return; // bye
}
old = scan; // save last scan
scan = (node *) scan->next; // next item
}
}
// ==========================================================
void sllist::del_all()
{
node *temp = head;
while(temp)
{
temp=head;
head=(node *) temp->next;
delete temp; // free up node
}
head=NULL;
}
// ==========================================================
void sllist::print_list()
{
node *temp = head;
int x=1;
while(temp)
{
printf(" %s\n", temp->file_name);
if(!(x % 20))
{
x=0;
printf("\n");
}
x++;
temp=(node *) temp->next;
}
printf("\nSlist printed\n");
}
// ===============================================================
void sllist::push_stack(int rec_num, int edge)
{
if(!head) // if no list create list
{
node *temp = new node;
temp->next = NULL;
temp->rec_num = rec_num;
temp->edge = edge;
head = temp;
return;
}
// push item to head of list
node *temp = new node; // create space for a new node
temp->rec_num = rec_num; // put in data
temp->edge = edge; // put in data
temp->next=head; // next points to what head is currently at
head = temp;
return;
}
// ===============================================================
int sllist::pop_stack(int *redge)
{
node *scan=head;
node *old=head;
int vertex=0;
if(head)
{
printf("Popping Off Vert = %d edge = %d \n", scan->rec_num, scan->edge);
vertex=scan->rec_num;
*redge=scan->edge;
scan=(node *) scan->next;
head=scan;
delete old;
return(vertex);
}
}
// ==========================================================
void sllist::print_stack()
{
node *temp = head;
while(temp)
{
printf(" Vertex = %d Edge = %d\n", temp->rec_num, temp->edge);
// getche();
temp=(node *) temp->next;
}
}
// ==========================================================
int sllist::check_stack_for_cycle()
{
node *temp = head;
int last_node;
int first_edge = temp->edge;
if(head)
{
while(temp)
{
if(temp->next == NULL)
{
last_node=temp->rec_num;
}
temp=(node *) temp->next;
}
if(last_node == first_edge)
return(1); //cycle complete
else
return(0);
}
else
return(1);
}
// ==========================================================
// =================== End of Listing =======================