#include <stdio.h>
#include <stdlib.h>
typedef int element;
typedef struct ListNode {
element data;
struct ListNode *link;//다른 노드의 주소를 저장하는 포인터변수
}ListNode;
void insertNode(ListNode **phead, ListNode *p, ListNode* newNode)
{
if(*phead == NULL)
{
newNode->link = NULL;//there is only one node, new node
*phead = newNode;//first head is new head
}
else if(p == NULL)//newNode do not want to be second => it won't have previous node
{
newNode->link = *phead;//original first node will follow new node
*phead = newNode;
}
else
{
newNode->link = p->link;//the node which was following pnode will follow newNode
p->link = newNode;
}
}
void removeNode(ListNode **phead, ListNode*p, ListNode *removed)
{
if(p == NULL)//no presending node for removed = removed is first node
{
*phead = (*phead)->link;//following node of removed will be first node
}
else
{
p->link = removed->link;
free(removed);
}
}
int main()
{
ListNode *p1; //declare the pointer first and allocate the memory later -->why?
p1 = (ListNode *)malloc(sizeof(ListNode));
p1->data = 10;
p1->link = NULL;
ListNode *p2;
p2 = (ListNode *)malloc(sizeof(ListNode));
p2->data = 20;
p2->link = NULL;
p1->link = p2; // the way to save the address => *ptr = value
}
/*내가 니 뒤에 있다, 너는 내 앞에 있다 = you are followed by me*/
#include <stdlib.h>
typedef int element;
typedef struct ListNode {
element data;
struct ListNode *link;//다른 노드의 주소를 저장하는 포인터변수
}ListNode;
void insertNode(ListNode **phead, ListNode *p, ListNode* newNode)
{
if(*phead == NULL)
{
newNode->link = NULL;//there is only one node, new node
*phead = newNode;//first head is new head
}
else if(p == NULL)//newNode do not want to be second => it won't have previous node
{
newNode->link = *phead;//original first node will follow new node
*phead = newNode;
}
else
{
newNode->link = p->link;//the node which was following pnode will follow newNode
p->link = newNode;
}
}
void removeNode(ListNode **phead, ListNode*p, ListNode *removed)
{
if(p == NULL)//no presending node for removed = removed is first node
{
*phead = (*phead)->link;//following node of removed will be first node
}
else
{
p->link = removed->link;
free(removed);
}
}
int main()
{
ListNode *p1; //declare the pointer first and allocate the memory later -->why?
p1 = (ListNode *)malloc(sizeof(ListNode));
p1->data = 10;
p1->link = NULL;
ListNode *p2;
p2 = (ListNode *)malloc(sizeof(ListNode));
p2->data = 20;
p2->link = NULL;
p1->link = p2; // the way to save the address => *ptr = value
}
/*내가 니 뒤에 있다, 너는 내 앞에 있다 = you are followed by me*/
댓글
댓글 쓰기