Purpose: to emulate a multi-function program by working via pointers in multipe queues.
objective: to add data to a queue (qemp); work w/ the array (empdata) via (qemp1)...via pointers
Need: next2 pointer initialized to next
next will follow the records as I input them into qemp
next2 will follow the records as I modify them in qemp1
currdata will be initialized to firstdata;
Sample input file:
row1: lname, fname, minit
row2: multiple phone#s
row3: multiple fax#s
row4: multiple email addresses
addis steve d
123-456-1111 123-461-1233 123-454-2345
123-456-1112 123-461-1234 123-454-2346
email1, email2, email3
Code:
#include "test.h"
int main(void)
{
FILE *fin;
char buffer[MAX];
QUEUE qemp, qemp1;
EMP emprec;
int c;
//open file
fin = fopen("firstdata", "r");
//init q
qemp.head = qemp.tail = NULL;
qemp1.head = qemp1.tail = NULL;
//place data into q
while (fgets( buffer, sizeof buffer, fin ) != NULL )
{
sscanf ( buffer, "%s%s%s", &emprec.fname, &emprec.lname, &emprec.mname ) ;
//add multiple records of phone #s, email addresses & faxes*/
while ( fgets ( buffer, sizeof buffer, fin ) != NULL )
{
//phone #s
while ( buffer )
sscanf ( buffer, "%s", emprec.firstdata[c++].phone );
c = 0;
//fax#
while ( buffer )
sscanf ( buffer, "%s", emprec.firstdata[c++].fax );
c = 0;
//emails
while ( buffer )
sscanf ( buffer, "%s", emprec.firstdata[c++].email );
}
}
//addrec to qemp
addrec ( &qemp, emprec );
//del a phone #
//this is problem...
//delrec ( &qemp1, emprec.firstdata);
fclose(fin);
return 0;
}
NODE *createlist ( EMP inpdata, NODE *link )
{
//create a node
NODE *ptemp = ( NODE * ) malloc ( sizeof ( NODE ) ) ;
if ( ptemp ) //if not null
{
//assign data values to pointer
ptemp -> record = inpdata;
ptemp -> next = link;
ptemp -> next2 = ptemp -> next;
}
return ptemp;
}
int addrec ( QUEUE *q, EMP item)
{
int success = 1; //init value
//pointer to new node
NODE *new_node;
if ( q -> tail == NULL )
if ( ( q -> tail = createlist ( item, q -> tail ) ) == NULL )
success = 0;
else
q -> tail -> next = q -> tail;
else
if ( ( new_node = createlist ( item, q -> tail -> next ) ) == NULL )
success = 0;
else
q -> tail = q -> tail -> next = new_node;
return success;
}
int delrec ( QUEUE *q, EMP *dataout)
{
int success = 1;
NODE *del;
if ( qempty ( q ) )
{
success = 0;
}
else
{
*dataout = q -> tail -> next -> record;
del = q -> tail -> next;
q -> tail -> next = del -> next;
if ( ( q -> tail == del ) && ( q -> tail -> next == del ) )
q -> tail = NULL;
free ( del );
}
return success;
}
int qempty ( QUEUE *q )
{
return q -> tail == NULL;
}
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
typedef struct _firstdata
{
char phone, fax, email;
}firstdata;
typedef struct _EMP
{
char fname, lname, mname;
firstdata firstdata[MAX];
}EMP;
typedef struct _NODE
{
EMP record;
struct _NODE *next, *next2;
}NODE;
typedef struct _QUEUE
{
NODE *head, *tail;
}QUEUE;
NODE *createlist ( EMP record, NODE *pnext );
int addrec ( QUEUE *q, EMP item);
int delrec ( QUEUE *q, EMP *pout);
int qempty ( QUEUE *q );
Sample File: