Thread: Data structs and pointers

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    3

    Data structs and pointers

    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:

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    3
    Header File is at the end of the code;;;beginning line: #define MAX 100

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    3
    This additional info will address the problem

    //delrec ( &qemp1, emprec.firstdata); note this should be
    //delrec ( &qemp1, emprec.curdata);

    The problem is I don't know how or where to (in header file, in queue functions, et al )init curdata to firstdata; next2 to next so that as I perform processes on the records I have constantly updating next2->curdata via pointers

    This is the deal:
    read in the records using as linked list queue

    modify records using a different queue via pointers with next2 pointer (i.e. q2->next2->curdata

    I hope this helps...if I knew the correct verbage, I'm sure this would be a two line sentence.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs, pointers and functions
    By osici in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 12:35 AM
  2. Confused with pointers and structs
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 10-12-2008, 02:22 PM
  3. display copied data into structs
    By bazzano in forum C Programming
    Replies: 5
    Last Post: 05-02-2006, 02:12 PM
  4. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 07:14 PM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 07:13 AM