Hi guy, I have been teaching myself how to write C programming code, right now am learning the Round Robin Sheduling algorithm, below is the code that i have managed to write, its the basic but i could do with your help or your direction with how to do the following.
I want my program to have an option via a menu to request the user input of Number of processes N and then for each process the Arrival time AT, the number of units of CPU time required NUT. Basically i want to be able to
1.function read data file that will add a node onto a linklist
2.move node toplist to bottom of linklist
3.move node from one list
4.function scan list to display
5.Remove the node.

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

struct nodedef{

char label;
int NUT;
char status;
int prio;
struct nodedef *next;
};

typedef struct nodedef procdef;

main(){

 FILE *fptr;
 int AT;
 procdef *new, *curr;

 int i,nl, np;


fptr = fopen("data.txt","r");

 fscanf(fptr,"%d",&np);
 fscanf(fptr,"%c",&nl);

 new = (procdef *) malloc(sizeof(procdef));
 fscanf(fptr,"%c",&new->label);
 fscanf(fptr,"%d",&new->NUT);
 fscanf(fptr,"%d",&AT);
 fscanf(fptr,"%c",&nl);

 new->status = 'W';
 new->prio = 0;
 new->next = NULL;

 curr = new;
 //loop

 for(i=0;i<np-1;i++){
 curr->next = (procdef *) malloc(sizeof(procdef));
 curr = curr->next; //move to next node

 fscanf(fptr,"%c",&curr->label);
 fscanf(fptr,"%d",&curr->NUT);
 fscanf(fptr,"%d",&AT);
 fscanf(fptr,"%c",&nl);
 curr->status = 'W';
 curr->prio = 0;
 curr->next = NULL;
 }


 curr = new; //points to 1st node
 for(i=0;i<np;i++){
     printf("%c %d %c %d %p\n",curr->label,curr->NUT,curr->status,curr->prio,curr->next);
     curr = curr->next;
 }




 }

You need to start learning to organize your programs so that they contain functions that perform specific tasks. For example wrire a function that does nothing more (or less) than adding a node to the linked list. When you get that coded and working correctly move on to write another function that moves a node from one position to another. You can write separate functions for each of the 5 requirements that you listed.

line 15: You need to define the return type of main(). That is a somewhat special function in that the value returned by main() is captures by the operating system. You should ALWAYS declare main() like this: int main() or like this:int main(int argc, char* argv[]); Anything else is technically not acceptable and unportable, although some compilers let you declare void main()

int main(void) is also acceptable and portable.
I think you should even prefer it to int main() if the intent is to not have any parameters.

Thank U guys..@ Ancient Dragon, the problem I am having is how to organise my functions, i do not know which one comes first or last. Am pretty new to programming. If you dont mind , could you please write me an example and hopefully i will carry on from there.Thank You :)

The way I organize a program is something like this

  1. All header files
  2. structures and other defines
  3. function prototypes
  4. actual functions

It doesn't really matter in which order the functions appear in the *.c file(s) when you provide function prototypes. If your program consists of two or more *.c files then you will probably want to put the function prototypes in a header file and include that header file in each of the *.c files.

Thank You so much, U've been of great help.

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.