line_double_dirction_edition1.zip

  • 军军66
    了解作者
  • C/C++
    开发工具
  • 1KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2020-04-21 13:59
    上传日期
创建一个双向循环链表,并进行增加、删除、修改、查询链表元素的操作
line_double_dirction_edition1.zip
  • line_double_dirction_edition1.c
    5.6KB
内容介绍
#include<stdio.h> #include<stdlib.h> #include<malloc.h> int num; struct Node{ int date; struct Node* p_next; struct Node* p_pre; }; //创建头节点,表头 ,循环 struct Node* HeadNode(void){ struct Node* p; struct Node* p_tail; p=(struct Node*)malloc(sizeof(struct Node)); p_tail=(struct Node*)malloc(sizeof(struct Node)); p->date=0; p->p_next=p_tail; p->p_pre=p_tail; p_tail->date=0; p_tail->p_next=p; p_tail->p_pre=p; printf("Linked List Is Created!\n"); return p; } //创建链表,根据个数 void create_line(struct Node *p_Head,int n) { struct Node* p; struct Node* p_flag; int i; p_flag=p_Head; for(i=1;i<=n;i++){ p=(struct Node*)malloc(sizeof(struct Node)); p->date=0; p->p_next=p_flag->p_next; p->p_next->p_pre=p; p_flag->p_next=p; p->p_pre=p_flag; p_flag=p_flag->p_next; } return; } //任意位置插入 void Insert(struct Node *p_Head,int Date,int *position) { struct Node* p1; struct Node* p2; int i=0; if(*position>num || *position<=0) { printf("Input error!\nYour Input Should <The Number of Node(%d)\nPlease Input The Position Again:",num); scanf("%d",position); } p2=(struct Node*)malloc(sizeof(struct Node)); p2->date=Date; p1=p_Head->p_next; for(i=1;i<*position;i++) { p1=p1->p_next; } p1->p_pre->p_next=p2; p2->p_pre=p1->p_pre; p2->p_next=p1; p1->p_pre=p2; num++; printf("Insert Successful!\n"); return ; } //遍历 void traverse(struct Node *p_Head){ struct Node* p; if(p_Head->p_next->p_next==p_Head){ printf("This Linked Line Is NULL!\n"); return; } printf("\n"); printf("This Linked Line All Date Is Shown(One Direction):\n"); p=p_Head->p_next; while(p->p_next!=p_Head){ if(p->p_next==p_Head->p_pre) { printf("%d",p->date); p=p->p_next; } else { printf("%d->",p->date); p=p->p_next; } } // printf("%d",p_Head->p_next->date); return; } //查找 void searchline(int*n,struct Node *p_Head) { int i=0; struct Node *p; while(*n>num || *n<=0) { printf("Input Error!\nYour Input Should <The number of Node(%d)\nPlease Input Again:",num); scanf("%d",n); } p=p_Head; while(i!=*n) { p=p->p_next; i++; } if(p->p_pre==p_Head && num>=2) { printf("Date:%d\n",p->date); printf("P_Date:%d\n",p_Head->p_pre->p_pre->date); printf("B_Date:%d\n",p->p_next->date); } else if(p->p_next==p_Head->p_pre && num>=2) { printf("Date:%d\n",p->date); printf("P_Date:%d\n",p->p_pre->date); printf("B_Date:%d\n",p_Head->p_next->date); } else if(num==1) { printf("Date:%d\n",p->date); printf("P_Date:%d\n",p->date); printf("B_Date:%d\n",p->date); } else { printf("Date:%d\n",p->date); printf("P_Date:%d\n",p->p_pre->date); printf("B_Date:%d\n",p->p_next->date); } return; } //删除 void delete(int*n,struct Node *p_Head) { struct Node* p1; struct Node* p2; int i; while(*n>num || *n<=0) { printf("Input Error!\nYour Input Should <The number of Node(%d)\nPlease Input Again:",num); scanf("%d",n); } p2=p_Head; p1=p_Head->p_next; for(i=1;i<*n;i++) { p2=p2->p_next; p1=p1->p_next; } p2->p_next=p1->p_next; p1->p_next->p_pre=p2; free(p1); num--; printf("Delete Success!\n") ; return; } //修改 void change(int*n,struct Node *p_Head,int Date) { struct Node* p; int i; while(*n>num || *n<=0) { printf("Input Error!\nYour Input Should <The number of Node(%d)\nPlease Input Again:",num); scanf("%d",n); } p=p_Head->p_next; for(i=1;i<*n;i++) { p=p->p_next; } p->date=Date; return; } int main() { struct Node* p_head; int x,date,n,position; int flag=0; printf("1 Create Line\n2 Insert(Any Position)\n3 Delete Date\n4 Change Date\n5 Search\n6 Traverse\n7 Exit\n"); printf("Input Your Choice:"); scanf("%d",&x); if(x>7 || x<=0) return 0; do{ switch(x) { case 1: printf("Input The Number of Node:"); scanf("%d",&num); p_head=HeadNode(); create_line(p_head,num); flag=1;break; case 2: if(flag==0) { printf("Linked Line Is NOT Created!\nPlease Create One First!\n"); break; } printf("Input the Date and Node you want to insert(Date Position):"); scanf("%d %d",&date,&position); // scanf("%d",&position); Insert(p_head,date,&position);break; case 3: if(flag==0) { printf("Linked Line Is NOT Created!\nPlease Create One First!\n"); break; } printf("Input The Node You Want To Delete:"); scanf("%d",&n); delete(&n,p_head); break; case 4: if(flag==0) { printf("Linked Line Is NOT Created!\nPlease Create One First!\n"); break; } printf("Input the Node and the Date you want to change(Node Date):"); scanf("%d",&n); scanf("%d",&date); change(&n,p_head,date); break; case 5: if(flag==0) { printf("Linked Line Is NOT Created!\nPlease Create One First!\n"); break; } printf("Input The Node You Want To Search:"); scanf("%d",&n); searchline(&n,p_head); break; case 6: if(flag==0) { printf("Linked Line Is NOT Created!\nPlease Create One First!\n"); break; } traverse(p_head); break; default: break; } printf("\n"); printf("----------------------------------------------------------\n"); printf("----------------------------------------------------------\n"); printf("1 Create Line\n2 Insert(Any Position)\n3 Delete Date\n4 Change Date\n5 Search\n6 Traverse\n7 Exit\n"); printf("Input Your Choice:"); scanf("%d",&x); }while(x<7 &&x>0); return 0; }
评论
    相关推荐
    • 链表
      链表 创建了一个将整数插入链表的函数。 列表按升序排列。 添加测试自动化脚本
    • 链表
      链表
    • 链表
      链表
    • 通用链表
      通用链表
    • 链表
      链表
    • 链表
      链表
    • 链表
      链表 链表数据结构的基本实现。
    • 链表
      简单链表处理 使用节点和链接列表数据结构 向学习
    • 链表数组
      链表数组 使用预设大小的数组实现双链表实现
    • 自适应链表
      使用单链表的形式来构造自适应链表。初始化为空表,表的构造通过在表尾插入新输入的元素来实现。而在表尾插入则需要知道表长(利用单链表头文件里的Insert函数实现),因此在类中添加记录表长的变量。每次搜索到一个...