1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| # include <stdio.h>
# include <stdlib.h>
# include <string.h>
struct record * MakeList ( void ); /* prototypes */
struct record * MakeNode ( void );
void LoadNode ( struct record * locn, char str [] );
void Queue ( struct record * x );
struct record {
char command [ 11 ]; /* designe "add" ou bien "delete" */
char word [ 11 ]; /* designe le mot qui devra suivre add et delete */
struct record * next;
};
struct record *first;
int main ( void )
{
struct record * start; /* beginning of the linked list */
start = MakeList ( ); /* Build the list */
Queue ( start ); /* Display the list from the beginning */
return 0;
}
/* Voila la Fonction recherche en question. Elle m'affiche l'erreur meme
si l'element selectionne existe vraiment dans la liste */
struct record *find( char *word ) {
struct record *info;
info = first;
while(info) {
if(!strcmp(word, info->word)) return info;
info = info->next;
}
fprintf(stderr, "\nCannot find item to delete.\n");
return NULL;
}
struct record * MakeList ( void )
{
FILE * sf; /* output file pointer */
char fname[31];
struct record * start = NULL, /* @ of first node in list */
* current, /* @ of where we are at any pt */
* new; /* some pointers to use later */
char buffer [ 81 ]; /* space for string from file */
printf(" Enter script file : "); /* le fichier avec les commandes doit etre pret */
gets(fname); /* pour ou contre ? utiliser un fgets ? */
if ( ( sf = fopen ( fname, "r" ) ) == NULL ) {
printf ( "MAKELIST: Unable to open input file %s!\n", fname );
exit ( 1 );
}
fgets ( buffer, 81, sf ); /* do a 'priming read' */
while ( ! feof ( sf ) ) { /* read & process all file contents */
new = MakeNode ();
if ( start == NULL )
start = current = new; /* start a new list */
else {
current->next = new; /* move pointer to new node */
current = current->next;
}
LoadNode ( current, buffer );
fgets ( buffer, 81, sf ); /* get rady for next test */
}
fclose ( sf );
return start;
}
void LoadNode ( struct record * locn, char str [] )
{ char s[40];
struct record * start;
if (locn->command && locn->word){
sscanf ( str, "%s %s", locn->command, locn->word );
}
if(locn->command && !locn->word){ // process the first commands
sscanf ( str, "%s", locn->command);
}
if(strcmp(locn->command,"delete") == 0 ) {
printf("==%s %s==\n",locn->command, locn->word);
strcpy(s,locn->word);
while(start) {
if(!strcmp(s, start->word)) puts("i ve found it\n");
current = current->next;
}
LoadNode ( current, buffer );
fgets ( buffer, 81, sf ); /* get rady for next test */
}
fclose ( sf );
return start;
}
struct record * MakeNode ( void )
{
struct record * newptr;
newptr = ( struct record * ) calloc ( 1, sizeof ( struct record ) );
newptr->next = NULL;
return newptr;
}
void Queue ( struct record * x )
{
struct record * tmp; /* a temporary ptr used while freeing space */
if ( strcmp(x->command, "queue") != 0) {
fprintf(stderr, "Script file doesn't contain command QUEUE nor STACK.\n");
}
else {
while ( x->next != NULL ) {
if ( strcmp(x->command, "add") == 0 && x->word) {
printf ( "%s\n", x->word);
}
else if ( strcmp(x->command,"pause") == 0 ) {
printf("\nTo continue press RETURN.\n");
getchar();
}
tmp = x;
x = x->next;
free ( tmp );
}
}
return;
} |
Partager