c++ 单链表基本操作---程序员面试宝典

  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6. /*c++实现简单的单链表操作*/  
  7. using namespace std;  
  8. typedef struct student  
  9. {  
  10.         int data;  
  11.         struct student *next;  
  12. }node;  
  13. //建立单链表   
  14. node *creat()  
  15. {  
  16.      node *head,*p,*s;  
  17.      int x,cycle=1;  
  18.      head=(node *)malloc(sizeof(node));  
  19.      p=head;  
  20.      while(cycle)  
  21.      {  
  22.                  printf("\nplease input the data:");  
  23.                  scanf("%d",&x);  
  24.                  if(x!=0)  
  25.                  {  
  26.                          s=(node *)malloc(sizeof(node));  
  27.                          s->data=x;  
  28.                          printf("\n%d",s->data);  
  29.                          p->next=s;  
  30.                          p=s;  
  31.                  }  
  32.                  else  
  33.                  cycle=0;  
  34.      }  
  35.      head=head->next;  
  36.      p->next=NULL;  
  37.      printf("\n   yyy    %d",head->data);  
  38.      return head;  
  39. }  
  40. //单链表插入  
  41. node *insert(node *head,int num)  
  42. {  
  43.      node *p0,*p1,*p2;  
  44.      p1=head;  
  45.      p0=(node *)malloc(sizeof(node));  
  46.      p0->data=num;  
  47.      while(p0->data>p1->data&&p1->next!=NULL)  
  48.      {  
  49.           p2=p1;p1=p1->next;  
  50.      }  
  51.           if(p0->data<=p1->data)  
  52.      {  
  53.            if(head==p1)  
  54.            {  
  55.                        p0->next=p1;  
  56.                        head =p0;  
  57.            }  
  58.            else  
  59.            {  
  60.                        p2->next=p0;  
  61.                        p0->next=p1;  
  62.            }  
  63.      }  
  64.      else  
  65.      {  
  66.          p1->next=p0;  
  67.          p0->next=NULL;  
  68.      }  
  69.      return head;  
  70. }   
  71.   
  72. //单链表测长  
  73. int length(node *head)  
  74. {  
  75.     int n=0;  
  76.     node *p;  
  77.     p=head;  
  78.     while(p!=NULL)  
  79.     {  
  80.                   p=p->next;  
  81.                   n++;  
  82.     }  
  83.     return n;  
  84. }   
  85.   
  86. //单链表删除某个元素   
  87. node *delNode(node *head,int num)  
  88. {  
  89.      node *p1,*p2;  
  90.      p1=head;  
  91.      while(num!=p1->data&&p1->next!=NULL)  
  92.      {  
  93.            p2=p1;  
  94.            p1=p1->next;  
  95.      }  
  96.      if(num==p1->data)  
  97.      {  
  98.                       if(p1==head)  
  99.                       {  
  100.                                   head=p1->next;  
  101.                                   free(p1);  
  102.                       }  
  103.                       else  
  104.                       p2->next=p1->next;  
  105.      }  
  106.      else  
  107.          printf("\n%d could not been found!",num);  
  108.          return head;  
  109.        
  110. }  
  111.   
  112. //单链表逆   
  113. node *reverse(node *head)  
  114. {  
  115.      node *p1,*p2,*p3;  
  116.      if(head==NULL||head->next==NULL)  
  117.      return head;  
  118.      p1=head,p2=head->next;  
  119.      while(p2)  
  120.      {  
  121.               p3=p2->next;  
  122.               p2->next=p1;  
  123.               p1=p2;  
  124.               p2=p3;  
  125.      }   
  126.      head->next=NULL;  
  127.      head=p1;  
  128.      return head;  
  129. }  
  130. //打印单链表  
  131. void print(node *head)  
  132. {  
  133.      node *p;  
  134.      int n;  
  135.      n=length(head);  
  136.      printf("\nNow,These %d record are:\n",n);  
  137.      p=head;  
  138.      if(head!=NULL)  
  139.      while(p!=NULL)  
  140.      {  
  141.                    printf("\n   uuu %d   ",p->data);  
  142.                    p=p->next;  
  143.      }  
  144. }   
  145. int main(int argc, char *argv[])  
  146. {  
  147.     node *list=creat();  
  148.       print(list);  
  149.      list = delNode(list,2);  
  150.      print(list);  
  151.      int x;  
  152.      cin>>x;  
  153.      list=insert(list,x);  
  154.      print(list);  
  155.      list=reverse(list);  
  156.      print(list);  
  157.      system("PAUSE");  
  158.        
  159.     return EXIT_SUCCESS;  
  160. }  

你可能感兴趣的:(C++,学习,C++,学习)