C/C++/MFC

연결리스트 삽입,삭제 구현 예제(스택)

유돌이 2009. 1. 10. 13:41

#include <stdio.h>
struct node{
 int i;
 struct node* link;
}*p;
int insert(int a);
int printe();
int delet(int c);

void main()
{
 p=NULL;
 insert(3);
 insert(4);
 insert(5);
 delet(4);
 printe(p);
}
int insert(int a)
{
 struct node *temp;
 temp=(struct node *)malloc(sizeof(temp));
 temp->i=a;
 temp->link=p;
 p=temp;
 return 0;
}

int printe()
{
 struct node *t;
 t=p;
 while(t!=NULL){
   printf("%d \n", t->i);
   t=t->link;
 }
 return 0;
}

int delet(int c)
{
 struct node *m;
 struct node *n; 

 m=p;
 if(m->i == c)
 {
  p=p->link;
 } 
 else{
  n=m;
  m=n->link;
  while(m!=NULL){
   if(m->i==c){
    n->link=m->link;
    break;
   }
   else{
    n=m;
    m=m->link;
   }
  }
 
  
 }
 return 0;
}