|
悪いところは括弧の対応ですね。
自分で貼り付けたものと、
コメントを入れたものを
比較してみると分かるかも。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 100
#define NAMESIZE 100
struct CELL {
char name[NAMESIZE];
struct CELL *next;
};
int main(void)
{
char buf[BUFSIZE];
struct CELL *head, *tail;
struct CELL *tmpc;
head = NULL;
tail = NULL;
while (1) {
printf("q) Quit\n");
printf("1) Add data\n");
printf("2) Delete data\n");
printf("3) Show list\n");
printf("Input command -> ");
fgets(buf, BUFSIZE, stdin);
if (strchr(buf, 'q') != NULL) {
break;
} else if(strchr(buf, '1') != NULL) {
printf("Input name -> ");
fgets(buf, BUFSIZE, stdin);
buf[strlen(buf) - 1] = '\0';
tmpc = (struct CELL *)malloc(sizeof(struct CELL));
if (tmpc == NULL) {
printf("memory allocation error!!\n");
exit(EXIT_FAILURE);
}//if (tmpc == NULL)
strcpy(tmpc->name, buf);
if (head == NULL) {
head = tmpc;
} else {
tail->next = tmpc;
}//if (head == NULL)
tail = tmpc;
tail->next = NULL;
} else if (strchr(buf, '2') != NULL) {
if (head != NULL) {
if (head->next == NULL) {
tail = NULL;
}//if (head->next == NULL)
tmpc = head->next;
free(head);
head = tmpc;
}//if (head != NULL)
} else if (strchr(buf, '3') != NULL) {
for (tmpc = head; tmpc != NULL; tmpc = tmpc->next) {
printf("%s\n", tmpc->name);
}//for (tmpc = head; tmpc != NULL; tmpc = tmpc->next)
}// if (strchr(buf, 'q') != NULL)
}//while (1)
return 0;
}
#あまりコメントを入れすぎるのも良くありませんが、
#括弧の中身が長い場合は対応する括弧の跡に
#コメントを入れたりする場合もあります。
#まぁ、そういう場合は殆ど関数化しますが。。。
|