컴공 일기180
게시글 주소: https://orbi.kr/00058328310

비선형자료구조만 하다보면, 또 선형에 대한 감각을 상실하기 때문에.
간단하게 오늘도 단일 연결리스트 구현..
#include <stdio.h>
#include <stdlib.h>
typedef struct UserNode
{
int Data;
struct UserNode* NextNode;
}Node;
Node* SLL_CreateNode(int NewData)
{
Node* NewNode = (Node*)malloc(sizeof(Node));
NewNode->Data = NewData;
NewNode->NextNode = NULL;
return NewNode;
}
void SLL_AppendNode(Node** Head, Node* NewNode)
{
if ((*Head) == NULL)
{
(*Head) = NewNode;
}
else
{
Node* Tail = *Head;
while (Tail->NextNode != NULL)
{
Tail = Tail->NextNode;
}
Tail->NextNode = NewNode;
}
}
Node* SLL_SearchNode(Node* Head, int Location)
{
Node* Current = Head;
while (Current != NULL && (--Location) > 0)
{
Current = Current->NextNode;
}
return Current;
}
void SLL_DestroyNode(Node* Node)
{
free(Node);
}
void SLL_RemoveNode(Node** Head, Node* RemoveNode)
{
if ((*Head) == RemoveNode)
{
*Head = RemoveNode->NextNode;
}
else
{
Node* Current = *Head;
while (Current != NULL && Current->NextNode != RemoveNode)
{
Current = Current->NextNode;
}
if (Current != NULL)
{
Current->NextNode = RemoveNode->NextNode;
}
}
}
void SLL_PrintAllNodes(Node* Head)
{
Node* Current = Head;
while (Current != NULL)
{
printf("%d\n", Current->Data);
Current = Current->NextNode;
}
}
unsigned int SLL_GetCount(Node* Head)
{
int Count = 0;
Node* Current = Head;
while (Current != NULL)
{
Current = Current->NextNode;
Count++;
}
return Count;
}
int main()
{
int i = 0;
int Count = 0;
Node* Current = NULL;
Node* List = NULL;
for (i = 0; i < 5; i++)
{
Current = SLL_CreateNode(i);
SLL_AppendNode(&List, Current);
}
printf("Printing all the Nodes...\n");
SLL_PrintAllNodes(List);
printf("Removing third Node...\n");
Current = SLL_SearchNode(List, 3);
SLL_RemoveNode(&List, Current);
SLL_PrintAllNodes(List);
printf("deleting the memories of all the Nodes to prevent memory leak...\n");
Count = SLL_GetCount(List);
for (i = 0; i < Count; i++)
{
Current = SLL_SearchNode(List, 0);
if (Current != NULL)
{
SLL_RemoveNode(&List, Current);
SLL_DestroyNode(Current);
}
}
return 0;
}
0 XDK (+0)
유익한 글을 읽었다면 작성자에게 XDK를 선물하세요.
근데 분홍오리비는 어떻게 하는 거에용..? 짱 예뿌당
옯모티콘 맨 오른쪽!

악 찾아따!!!코드몽키냐
그게 머임?
'Hello world!'
전 코딩은 이거밖에 모름
세계야 안뇽
설마 그 옛날에 공주임?
맞아용
개오랜만이네 ㅋㅋ 인스타 맞팔도 햇엇는대.. 컴공가셨네
잘 맞는 것 같아 다행이에요 :) 예전 옯스타 하던 시절에 뵈었던 분이겠네용 ㅎㅎ
자구가 재밌는거 보니 진짜 잘맞긴 하신듯 ㅋㅋ
멋지네요