c언어를 활용한 단순 연결 리스트(문자열)

임승혁·2021년 2월 9일
0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
	char name[100];
}element;

typedef struct ListNode {
	element data;
	struct Listnode* link;
}ListNode;

void error(char* message) {
	fprintf(stderr, "%s\n", message);
	exit(1);
}

ListNode* insertFirst(ListNode* head, element value) {
	ListNode* p = (ListNode*)malloc(sizeof(ListNode));
	p->data = value;
	p->link = head;
	head = p;
	return head;
}

void printList(ListNode* head) {
	for (ListNode* p = head; p != NULL; p = p->link) {
		printf("%s->", p->data.name);
	}
	printf("NULL\n");
}

int main(void) {
	ListNode* head = NULL;
	element data;

	strcpy(data.name, "APLLE");
	head = insertFirst(head, data);
	printList(head);

	strcpy(data.name, "KIWI");
	head = insertFirst(head, data);
	printList(head);

	strcpy(data.name, "BANANA");
	head = insertFirst(head, data);
	printList(head);
	return 0;
}

설명 : 정수가 아닌 문자열을 데이터로 작성한 단순 연결 리스트

profile
한성공대생

0개의 댓글