C/C++双链表正排序

来源:互联网 发布:国际淘宝什么意思 编辑:程序博客网 时间:2024/05/18 13:42
  1. #include<stdio.h> 
  2. #include<stdlib.h> 
  3. #include<malloc.h> 
  4. struct list{ 
  5.   int data; 
  6.   struct list *next; 
  7.   struct list *pre; 
  8.   }; 
  9. typedef struct list node; 
  10. typedef node *link; 
  11. link front=NULL,rear,ptr,head=NULL; 
  12. link push(int item){ 
  13. link newnode=malloc(sizeof(node)); 
  14.  newnode->data=item; 
  15.  if(head==NULL)
  16.  {
  17.  head=newnode; 
  18.  head->next=NULL; 
  19.  head->pre=NULL;
  20.  rear=head;
  21.  }
  22.  else
  23.  {
  24.  rear->next=newnode;
  25.  newnode->pre=rear;
  26.  newnode->next=NULL;
  27.  rear=newnode;
  28.  }
  29.  return head; 
  30.  } 
  31. void makenull(){           
  32.  front=NULL; 
  33.  rear=NULL; 
  34.  } 
  35. empty(){ 
  36.   if(front==NULL) 
  37.    return 1; 
  38.   else 
  39.    return 0; 
  40.   } 
  41. int tops(){ 
  42.    if(empty()) 
  43.     return NULL; 
  44.    else 
  45.     return rear->data; 
  46.    } 
  47. void pop(){ 
  48.    if(empty()) 
  49.     printf("stack is empty!/n"); 
  50.    else 
  51.     rear=rear->pre; 
  52.    } 
  53. void display(link l){ 
  54.     link p; 
  55.     p=l; 
  56.     while(p!=NULL){ 
  57.       printf("%d->",p->data); 
  58.       p=p->next; 
  59.     } 
  60. void main(){ 
  61. int n,i; 
  62. printf("input your first data!/n"); 
  63. scanf("%d",&n); 
  64. front=push(n); 
  65.    /*another data*/ 
  66. for(i=0;i<3;i++) 
  67.  printf("input:/n"); 
  68.  scanf("%d",&n); 
  69.  push(n); 
  70.  ptr=front; 
  71.  display(ptr); 
  72.  printf("/n Please enter any key to pop"); 
  73. getch();
  74.  pop(); 
  75.  ptr=front; 
  76.  display(ptr); 
  77. getch();

<script type="text/javascript"><!--google_ad_client = "pub-3555979289815451";google_ad_slot = "0437120238";google_ad_width = 468;google_ad_height = 60;//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
原创粉丝点击