双向链表的基本操作

/*
 * 双向链表的基本操作
 */
#include<stdio.h>
#include<stdlib.h>

typedef struct Node
{
  int data;
  struct Node *prior;
  struct Node *next;
}Node;

int main()
{
Node *creat();
Node *insert(Node *head);
Node *insert2(Node *head);
Node *del(Node *head);
Node *show(Node *head);
int surface();
  char a;
  Node *pt;

  while(1){
    surface();
    scanf("%c",&a);
    switch(a){
     case '1':{pt=creat();break;}
     case '2':{pt=insert(pt);break;}
     case '3':{pt=insert2(pt);break;}
     case '4':{pt=del(pt);break;}
     case '5':{pt=show(pt);break;}
     case '0':{return;}
    }
  }

}
//创建空链表
Node *creat()
{
   setbuf(stdin, NULL);//使stdin输入流由默认缓冲区转为无缓冲区
   Node *head;
   head = (Node*)malloc(sizeof(Node));
   head->prior = NULL;
   head->next = NULL;
   printf("链表初始化完成!!任意健继续!");
   getchar();
   return head;

}

//插入节点(尾插法)
Node *insert(Node *head)
{
   Node *p,*q;
   //使q指向最后一个节点
   for(q=head;q->next!=NULL;q=q->next);

   while(1)
{
   //使指针p指向新开辟节点,并对其数据域赋值
        p = (Node*)malloc(sizeof(Node));

        printf("输入新插入学生成绩");
        scanf("%d",&p->data);

   if(p->data==0){
     break;
      }
   p->next = NULL;
   q->next = p;
   p->prior=q;
   q = p;

}
  setbuf(stdin, NULL);//使stdin输入流由默认缓冲区转为无缓冲区  
  printf("节点插入完成!!任意健继续!");
  getchar();
  return head;
}
//插入节点(头插法)
Node *insert2(Node *head)
{
   Node *p,*q;

       while(1)
       {
        //使指针p指向新开辟节点,并对其数据域赋值
        p = (Node*)malloc(sizeof(Node));

            printf("输入新插入学生成绩");
            scanf("%d",&p->data);

             if(p->data==0){
                    break;
                            }
             //如果链表为空
             if(head->next==NULL){
                p->next = head->next;
                head->next = p;
                p->prior = head;

              }
             else{
                p->next = head->next;
                head->next->prior = p;
                head->next = p;
                p->prior = head;
               }
         }

         setbuf(stdin, NULL);//使stdin输入流由默认缓冲区转为无缓冲区  
         printf("节点插入完成!!任意健继续!");
         getchar();
         return head;
}

//删除节点
Node *del(Node *head)
{
   Node *p,*q;
   int a;

   printf("输入要删除的成绩");
   scanf("%d",&a);
   for(p = head;p->next!=NULL;p=p->next){
       //寻找前趋节点
       if(p->next->data==a){
            break;
            }
   }
    if (p->next == NULL) return head;

    if(p->next->next==NULL){
             q = p ->next;
             p->next = q ->next;
             free(q);
             return head;
          }
        q = p->next;

        p->next = q->next;
        q->next->prior = p;
        free(q);


 printf("节点删除完成!!任意健继续!");
 setbuf(stdin, NULL);//使stdin输入流由默认缓冲区转为无缓冲区 
 getchar();
 return head;
}
//输出所有节点数据
Node *show(Node *head)
{
   Node *p;

   for(p=head->next;p!=NULL;p=p->next){
      printf("%4d",p->data);       

   }
   printf("\n按任意健继续!");
   getchar();
   getchar();
   return head;
}
//主界面
int surface()
{
  system("clear");
  printf("**双向链表的基本操作**\n");
  printf("1.初始化空链表\n");
  printf("2.插入节点(尾插法)\n");
  printf("3.插入节点(头插法)\n");
  printf("4.删除节点\n");
  printf("5.打印所有数据\n");
  printf("0.退出系统\n");
  printf("输入选项:");
}

猜你喜欢

转载自blog.csdn.net/zuiziyoudexiao/article/details/80373644