#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

typedef struct
{
char fname[30];
char lname[30];
char MI;
}stud_name;

typedef struct
{
unsigned long idnum;
stud_name name;
char course [10];
int yr_level;
}studtype;

typedef struct
{
int grpnum;
char subj[24];
int time_sked;
int day;
int units;
char rmnum;
}class_sked;


--sir, is this a correct definition?i have to make a program that contains the following information.i've been trying it for days but i just dont know how to access data.all i wanna do is to "add, insert, search, delete, and segregate and count total number of students according to yr and course."i hope you can help me..

use the '.' operator to access data members in a structure

thanks.
do i still have to declare those data in the main func?

You only need to include struct members for fields that you need. For students:

idNum, fname, lname, gradYr, course, and DOB

(DOB=date of birth). You don't list it, but it's quite standard.
Course would be their major field of study perhaps.

Sounds like all you'll need, from your description.

Before the main() function, you'll prototype the struct - list it's fields and give each struct type, a name.

In main(), you'll declare the actual array of structs, so the data will be encapsulated, and given access only to the functions you send it to, as a parameter.

And whenever you post code, highlight it, and click on the [code] icon at the top of the editing window. Code tags around your program, make code easy to read, and easy to understand, and not all squished over to the left hand margin of the page.

Use code tags, or you'll be generally ignored!

P.S. Will you be using an array, or a linked list?

thanks for the advice.ill be using linked list.

OK. So you'll have your prototype for the struct above main(), (so it's global), and have a function to add another node to the list. In that function, you'll also have code to create one more instance of this struct.

So one node, holding one struct, for each student.

Singly linked or double linked list?

I would start with getting the linked list basics working OK - then add the struct stuff because that part of it involves a rather trivial amount of change to the code.

its a singly linked list.what should be done,if i am going to search for a name of a student and then it will show ,the full name,id,course as well as the class schedule..

Post up your linked list code (basic) program, as soon as you've gone as far as you can go with it.

I strongly suggest you get the linked list portion working right, FIRST, and then add the struct part to it, and all the details.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>


void printnode(struct node *);
void printlist(struct node *);
void add(struct node *ptr);
struct node *searchstud(struct node *, unsigned long *);
void deletenode(struct node *);
void insert(struct node *);
void deletelist(struct node *);
int segregate();
/* the function will segregate the students accdg to course and year and will return*/
 the total number of students;BSCS,BSIT,BSICT(1-4)*/

typedef struct
{
 char lname[16];
 char fname[24];
 char mi;
}name;

typedef struct
{
 unsigned long id;
 name sname;
 char crsNyr[8];
}student;
typedef struct
{
 student record;
 int grpnum;
 char subj[8];
 double time;
 int days, units;
 char room;
}class;
typedef struct node
{
 class sked;
 struct node *next;
};

struct node *head=(struct node *)NULL;
struct node *end=(struct node *)NULL;

void printnode(struct node *ptr)
{
 printf("Last name:%s\n", ptr->lname);     /* theres a warning message in the printnode function*/
 printf("First name:%s\n", ptr->fname);    /*it says that lname is not part of structure in printnode*/
 printf("Middle Initial:%s\n", ptr->mi);   /*and so on,with units*/
 printf("ID Number:%ld\n", ptr->id);             /*btw, im using turbo c*/
 printf("Course and Year:%s\n", ptr->crsNyr);
 printf("Class Schedule\n");
 printf("GroupNo.:%d\n",ptr->grpnum);
 printf("Subjects:%s\n",ptr->subj);
 printf("Schedule:%f\n", ptr->time);
 printf("Days:%d\n",ptr->days);
 printf("Room:%s\n",ptr->room);
 printf("Units:%d\n",ptr->units);
}
void printlist(struct node *ptr)
{
 while (ptr!=NULL)
 {
  printnode(ptr);
  ptr=ptr->next;

 }
}

void add(struct node *new)
{
 if (head == NULL)
     head = new;
 end->next=new;
 new->next=NULL;
 end = new;

}
struct node *searchstud(sturct node *ptr, unsigned long id)
{
 while(strcmp(id, ptr->id)!=0)
  {
   ptr=ptr->next;
   if(ptr==NULL)
     break;
  }
  return ptr;
}

void deletenode(struct node *ptr)
{
 struct node *temp,*prev;
 temp=ptr;
 prev=head;

 if(temp==prev)
   {
   head=head->next;
    if (end==temp)
	end=end->next;
    free(temp);
   }
    else{
       while(prev->next!=temp)
	{
	  prev=prev->next;
	 }
	  prev->next=temp->next;
	   if(end==temp)
	       end=prev;
	   free(temp);
      }
}
void insertnode(struct node *new)
{
 struct node *temp, *prev;

 if (head==NULL)
    {
    head=new;
    end=new;
    head->next=NULL;
    }
    temp=head;

    while(strcmp(temp->id, new->id)<0) /*which is better to be iserted the id number or the last name?*/
       {
	temp=temp->next;
	if (temp==NULL)
	 break;
       }
	if (temp==head)
	  {
	   new->next=head;
	   head=new;
	  }
	  else {
	   prev=head;
	   while(prev->next!=temp)
	      {
	      prev=prev->next;
	      }
	       prev->next=new;
	       new->next=temp;
	       if(end==prev)
		  end=new;
	  }
}
void deletelist(struct node *ptr)
{
 struct node *temp;
 if (head==NULL)

 if (ptr==head)
    {
     head=NULL;
     end=NULL;
    }
    else {
      temp=head;
      while(temp->next!=ptr)
	 temp=temp->next;
	 end=temp;
    }
    while(ptr!=NULL)
    {
     temp=ptr->next;
     free(ptr);
     ptr=temp;
    }

}



void main()
{
 name sname;
 student record;
 class sked;
 struct node *ptr;

 int ch=1,i;
 clrscr();

 while (ch!=0)
 {
  printf("\n");
  printf("Enter choice\n");
  printf("1 add student\n");
  printf("2 delete student\n");
  printf("3 list all names\n");
  printf("4 search a name\n");
  printf("5 insert student\n");
  printf("6 segregate and count total number accdg to crs and yr\n");
  printf("0 Exit\n");
  scanf("%d",&ch);

  switch(ch)
  {
   case 1: printf("ID Number:");
	    scanf("%ld",&record.id);


	    add(ptr);

	    break;
	    /* delete student*/
    case 2: printf("enter in idnumber:");
	      scanf("%ld", &record.id);
	      ptr=searchstud(head, id);
	      if(ptr==NULL){
		printf("IDnumber %ld not found\n", id);
	      }
	      else
		deletenode(ptr);
	      break;
    case 3: /*list all nodes*/
		printlist(head);
		break;
	    /*search and print a name*/
    case 4: printf("enter student's id no.:")
	     scanf("%ld",&record.id);
	     ptr=searchstud(head, id);
	     if (ptr==NULL)
		 {
		  printf("ID number %ld not found\n", id);
		  }
		  else
		   printnode(ptr);
		   break;
    case 5:  /* insert a student*/
	     printf("enter in id no.:");
	     scanf("%ld",&record.id);
	    printf("\t\t\tName\n");
	    printf("Last Name:");
	     scanf("%s",sname.lname);
	    printf("First Name:");
	     scanf("%s",sname.fname);
	    printf("Middle Inital:");
	     scanf("%s",sname.mi);
	   printf("\t\t\tCourse and Year:");
	    scanf("%s",record.crsNyr);
	   printf("Class Schedule\n");
	    printf("Group No.\tSubjects\tSchedule\tDays\tRoom\tUnits\n");
	    scanf("%d\t%s\t%ld\t%s\t%d\t%d", &sked.grpnum,sked.subj,
	    sked.time,&sked.days,sked.room,&sked.units);


	    insert(ptr);
	    break;

  }      /*do i still need to initialize?and if its needed,do i have to initiliaze */
	 /*all the members in the node*/


 }
     deletelist(head);
     getch();
}

please feel free to make the necessary corrections..,and if theres something to be added or what,please correct it.thank you for helping

I use Turbo C myself, for small programs.

Let me put your program into my compiler and be sure before I comment. You have put up a good deal of code, here.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>


struct node * initnode( char *,char *,unsigned long, char *, int );
void printnode( struct node * );
void displaylist( struct node * );
void add( struct node * );
struct node * findname( struct node *, char * );
struct node * findId(struct node *,unsigned long);
void deletenode( struct node * );
void insertnode( struct node * );
void deletelist( struct node * );
int Segregate(struct node *);


typedef struct node{
 char lname[24];
 char fname[16];
 unsigned long id;
 char course[8];
 int yr;
 int last;
 struct node *next;
};

struct node *head = (struct node *) NULL;
struct node *end = (struct node *) NULL;
struct node * initnode( char *lname, char *fname,unsigned long id, char *course, int yr )
{
   struct node *ptr;
   ptr = (struct node *) calloc( 1, sizeof(struct node ) );
   if( ptr == NULL )
       return (struct node *) NULL;
   else {
       strcpy( ptr->lname, lname );
       strcpy(ptr->fname, fname);

       ptr->id = id;
       strcpy(ptr->course, course);
       ptr->yr=yr;
       return ptr;
   }
}


void printnode( struct node *ptr )
{
   printf("Last Name:%s\n", ptr->lname );
   printf("First Name:%s\n", ptr->fname);
   printf("ID  :%ld\n", ptr->id );
   printf("Course :%s\n", ptr->course);
   printf("Year Level :%d",ptr->yr);
}
void displaylist( struct node *ptr )
{
   while( ptr != NULL )
   {
      printnode( ptr );
      ptr = ptr->next;
   }
}

void add( struct node *new )
{
   if( head == NULL )
       head = new;
   end->next = new;
   new->next = NULL;
   end = new;
}

struct node * findname( struct node *ptr, char *lname )
{
    while( strcmp( lname, ptr->lname ) != 0 ) {
       ptr = ptr->next;
       if( ptr == NULL )
	  break;
    }
    return ptr;
}
struct node * findId(struct node *ptr, unsigned long id)
{
 while(ptr->id!=id)
 {
  ptr=ptr->next;
  if(ptr==NULL)
  {
  printf("Student not found\n");
  break;
  }
 }
}
void deletenode( struct node *ptr )
{
   struct node *temp, *prev;
   temp = ptr;
   prev = head;

   if( temp == prev ) {
       head = head->next;
       if( end == temp )
	  end = end->next;
       free( temp );
   }
   else {
       while( prev->next != temp ) {
	   prev = prev->next;
       }
       prev->next = temp->next;
       if( end == temp )
	   end = prev;
       free( temp );
   }
}

void insertnode( struct node *new )
{
   struct node *temp, *prev;

   if( head == NULL ) {
       head = new;
       end = new;
       head->next = NULL;
       return;
   }

   temp = head;

   while( strcmp( temp->lname, new->lname) < 0 ) {
	  temp = temp->next;
	  if( temp == NULL )
              break;
   }


   if( temp == head ) {
      new->next = head;
      head = new;
   }
   else {
      prev = head;
      while( prev->next != temp ) {
          prev = prev->next;
      }
      prev->next = new;
      new->next = temp;
      if( end == prev )
	 end = new;
   }
}

int Segregate(struct node *ptr)
{
 int i,ctr;
 for(i=ctr=0;i<=ptr->last;i++)
  {
  if(strcmp(ptr->course,"BSIT")==0 && ptr->yr==1)
     ctr++;
  if(strcmp(ptr->course,"BSIT")==0 && ptr->yr==2)
     ctr++;
  if(strcmp(ptr->course,"BSIT")==0 && ptr->yr==3)
     ctr++;
  if(strcmp(ptr->course,"BSIT")==0 && ptr->yr==4)
    ctr++;
  }
 for(i=ctr=0;i<ptr->last;i++)
  {
  if(strcmp(ptr->course,"BSCS")==0 && ptr->yr==1)
     ctr++;
  if(strcmp(ptr->course,"BSCS")==0 && ptr->yr==2)
     ctr++;
  if(strcmp(ptr->course,"BSCS")==0 && ptr->yr==3)
     ctr++;
  if(strcmp(ptr->course,"BSCS")==0 && ptr->yr==4)
     ctr++;
  }
 for(i=ctr=0;i<ptr->last;i++)
  {
  if(strcmp(ptr->course,"BSICT")==0 && ptr->yr==1)
     ctr++;
  if(strcmp(ptr->course,"BSICT")==0 && ptr->yr==2)
     ctr++;
  if(strcmp(ptr->course,"BSICT")==0 && ptr->yr==3)
     ctr++;
  if(strcmp(ptr->course,"BSICT")==0 && ptr->yr==4)
     ctr++;
  }
     return ctr;
}

void deletelist( struct node *ptr )
{
   struct node *temp;

   if( head == NULL )

   if( ptr == head ) {
       head = NULL;
       end = NULL;
   }
   else {
       temp = head;
       while( temp->next != ptr )
           temp = temp->next;
       end = temp;
   }

   while( ptr != NULL ) {
      temp = ptr->next;
      free( ptr );
      ptr = temp;
   }
}

 void main()

{

   char lname[24];
   char fname[16];
   unsigned long id, ch = 1;
   char course[8];
   int yr;
   int b;
   char answer;
   struct node *ptr;

   clrscr();
   while( ch != 0 ) {
      printf("\n");
      printf("\t\tChoose Option\n");
      printf("\t\t\t1 Add a student's record\n");
      printf("\t\t\t2 Delete student's record\n");
      printf("\t\t\t3 list all names \n");
      printf("\t\t\t4 search\n");
      printf("\t\t\t5 Insert \n");
      printf("\t\t\t6 Segregate\n");
      printf("\t\t\t0 Quit\n");
      scanf("%d", &ch );
      switch( ch )
      {
	  case 1:  /* add a name*/
		   printf("Enter in last name: ");
		   scanf("%s", lname );
		   printf("Enter first name:");
		   scanf("%s", fname);
		   printf("Enter in id: ");
		   scanf("%ld", &id);
		   printf("Enter course:");
		   scanf("%s", course);
		   printf("Enter year level:");
		   scanf("%d",&yr);
		   ptr = initnode( lname, fname, id, course, yr );
                   add( ptr );
                   break;
	  case 2:   /* delete name */

		  printf("Enter student's id:");
		   scanf("%ld",&id);
		   ptr=findId(head,id);
		   if(ptr==NULL)
		    {
		     printf("Student %d not found!\n",id);
		    }
		    else
		      deletenode(ptr);
		   break;

	  case 3: /*list all nodes*/
		   displaylist(head);
		   break;


	  case 4: /*search and print*/
		   printf("Enter in lname: ");
		   scanf("%s", lname );
		   ptr = findname( head, lname );
                   if( ptr ==NULL ) {
		       printf("Name %s not found\n", lname );
                   }
                   else
                      printnode( ptr );
                   break;
	  case 5:
		   printf("Enter in last name: ");
		   scanf("%s", lname );
		   printf("Enter first name:");
		   scanf("%s", fname);
		   printf("Enter in id: ");
		   scanf("%ld", &id );
		   printf("Enter course:");
		   scanf("%s", course);
		   printf("Enter year level:");
		   scanf("%d",&yr);
		   ptr = initnode( lname,fname, id, course, yr );
                   insertnode( ptr );
                   break;

	 case 6:

		  printf("\nCourse is %s",Segregate(ptr ));
		  printf("\nYear is %d", Segregate(ptr));
		  printf("\nTotal no.of students:", Segregate(ptr));

		  break;

      default:
	      break;

     }
   }
   deletelist( head );

getch();
 }

please help me with this one,i wonder why the segregate function wont work. asap

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.