728x90
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *create2();
struct node *node_del(struct node *head, int del);
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_del(head,10);
list_print(head);
head = node_del(head,20);
list_print(head);
head = node_del(head,30);
list_print(head);
}
void list_print(struct node *head)
{
int i=0;
for(; head; head=head->next)
{
printf(" [%d] %d\n",i, head->data);
i++;
}
}
struct node *node_del(struct node *head, int del)
{
struct node *tmp, *ptr;
tmp = ptr = head;
if(ptr->data==del)
{
head=ptr->next;
printf("\ndel %d\n",*ptr);
return head;
}
while(ptr->data != del)
{
tmp = ptr;
ptr = ptr->next;
printf("\ndel %d\n",*ptr);
}
tmp->next = ptr->next;
return head;
}
'it > programming' 카테고리의 다른 글
| [c++] 구조체 포인터 node 관리 - add (0) | 2012.11.26 |
|---|---|
| [c++] 포인터 검색 (0) | 2012.11.05 |
| [c++] 구조체 배열 실습 (0) | 2012.11.05 |