farへの単一チェーンテーブルコード
純記念
typedefを使用した後
#include <stdio.h>
#include <stdlib.h>
struct student
{
char name[20];
struct student *next;
};
//
struct student* createNode()
{
struct student* p = (struct student *)malloc(sizeof(struct student));
p->next = NULL;
return p;
}
// , ,
struct student* addHeadNode(struct student *p)
{
struct student *head = createNode();
head->next = p;
return head;
}
//struct student * addTailNode(struct student *p)
// size
struct student* addSizeNode(struct student *head, int size)
{
int i = 0;
for (i=0; i < size; i++)
{
head = addHeadNode(head);
}
return head;
}
//
int getSize(struct student *p)
{
int size = 0;
while (p != NULL)
{
size++;
p = p->next;
}
return size;
}
//
void setNode(struct student *p)
{
gets(p->name);
}
//
void setList(struct student *head)
{
while (head != NULL)
{
setNode(head);
head = head->next;
}
}
//
void printList(struct student *head)
{
while (head != NULL)
{
printf("name = %s
", head->name);
head = head->next;
}
}
int main(int argc, char *argv[])
{
//
struct student* head;
head = createNode();
printf("link list size = %d
", getSize(head));
head = addSizeNode(head, 1);
printf("link list size = %d
", getSize(head));
setList(head);
printList(head);
return 0;
}
typedefを使用した後
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char name[20];
struct student *next;
} * Student;
//
Student createNode()
{
Student p = (struct student *)malloc(sizeof(struct student));
p->next = NULL;
return p;
}
// , ,
Student addHeadNode(Student p)
{
struct student *head = createNode();
head->next = p;
return head;
}
//struct student * addTailNode(Student p)
// size
Student addSizeNode(Student head, int size)
{
int i = 0;
for (i=0; i < size; i++)
{
head = addHeadNode(head);
}
return head;
}
//
int getSize(Student p)
{
int size = 0;
while (p != NULL)
{
size++;
p = p->next;
}
return size;
}
//
void setNode(Student p)
{
gets(p->name);
}
//
void setList(Student head)
{
while (head != NULL)
{
setNode(head);
head = head->next;
}
}
//
void printList(Student head)
{
while (head != NULL)
{
printf("name = %s
", head->name);
head = head->next;
}
}
int main(int argc, char *argv[])
{
//
Student head;
head = createNode();
printf("link list size = %d
", getSize(head));
head = addSizeNode(head, 1);
printf("link list size = %d
", getSize(head));
setList(head);
printList(head);
return 0;
}