728x90
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *node_add(struct node *head, int add_data);
void list_print(struct node *);
void main(){
struct node a, b, c;
struct node *head;
head=&a;
a.data = 10;
b.data = 20;
c.data = 30;
a.next= &b;
b.next = &c;
c.next = NULL;
list_print(head);
head = node_add(head, 50);
list_print(head);
}
void list_print(struct node *head){
for(; head; head=head->next)
printf(" %d\n", head->data);
}
struct node *node_add(struct node *head, int add_data)
{
struct node *ptr,*tmp,*newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data = add_data;
newnode->next = NULL;
printf("맨 뒤에 %d 추가\n",add_data);
ptr = tmp = head;
while(ptr)
{
tmp = ptr;
ptr = ptr->next;
}
tmp->next=newnode;
return head;
}
'it > programming' 카테고리의 다른 글
| [c++] 구조체 포인터 node 관리 - insert (0) | 2012.11.26 |
|---|---|
| [c++] 구조체 포인터 node 관리 - delete (0) | 2012.11.22 |
| [c++] 포인터 검색 (0) | 2012.11.05 |