728x90
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
};
struct node *node_insert(struct node *head, int cur_data,int insert_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_insert(head, 20, 5);
list_print(head);
}
void list_print(struct node *head){
for(; head; head=head->next)
printf(" %d\n", head->data);
}
struct node *node_insert(struct node *head, int cur_data, int insert_data)
{
struct node *ptr,*tmp,*newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data = insert_data;
newnode->next = NULL;
ptr=tmp=head;
if(ptr->data==cur_data)
{
newnode->next=ptr;
head=newnode;
}
else
{
while(ptr->data != cur_data)
{
tmp=ptr;
ptr=ptr->next;
}
newnode->next=ptr;
tmp->next=newnode;
}
printf("%d 전에 %d 삽입\n",cur_data,insert_data);
return head;
}
'it > programming' 카테고리의 다른 글
| [c++] 구조체 포인터 node 관리 - find (2) | 2012.12.03 |
|---|---|
| [c++] 구조체 포인터 node 관리 - add (0) | 2012.11.26 |
| [c++] 구조체 포인터 node 관리 - delete (0) | 2012.11.22 |