非循环双链表应用

来源:互联网 发布:知商金融活动 编辑:程序博客网 时间:2024/06/05 21:00
/*******功能:实现非循环双链表的新建、删除、插入等***作者:mlkk***时间:2017.06.10***备注 函数 insert_data 只是验证了思路的正确性,程序严谨性不足。****/        #include <stdio.h>        #include <stdlib.h>        #include <string.h>#define  LEN_NAME  20typedef struct student{int  age ;char name[20] ;}stu;typedef struct  node {stu  data;struct node  * pnext;struct node  * ppre;} NODE, * PNODE;int print_link(PNODE list);/****创建一个双链表*****/PNODE create_dou_link( void ){PNODE list = NULL;PNODE L = NULL ;PNODE p = NULL;int age = 0;char *name = NULL ;list = (PNODE)malloc(sizeof(NODE));list->pnext = NULL;L=list;name = (char *)malloc(LEN_NAME);while(1){printf("please input  name :\n ");scanf("%s",name);if( !( strcmp(name,"Q") ) ){printf("quit \n");break;}p=(PNODE)malloc(sizeof(NODE));printf("Please input the age:\n");scanf("%d",&age);strcpy(p->data.name,name);p->data.age = age;p->pnext = L->pnext;L->pnext=p;L=p;}free(name);L->pnext = NULL;return list;}/**删除双链表中的元素x***/PNODE   Delete_x_dou_link(PNODE list, char *name){PNODE p = NULL; /***定义两个指针,来指向链表的current和pre节点***/PNODE pn = NULL;char flag = 0 ;if (list == NULL){printf("The list is empty.\n");return NULL;}p =list;pn=list->pnext;while ( pn != NULL ){if( !( strcmp(pn->data.name,name) ) ){printf("name = %s \n",pn->data.name);if(pn->pnext == NULL){printf("the end is NULL \n");p->pnext = NULL;//pn = NULL;}else{p->pnext=pn->pnext;pn->pnext->ppre=p;}free(pn);pn=NULL;flag =1;break;}p=pn;pn=pn->pnext;}if (!flag){printf("Not found !!! \n");return NULL;}return list;}PNODE Delete_Dou(PNODE list , int age){int flag = 0;PNODE p = NULL;PNODE pn = NULL;p = list;pn = list->pnext;if(NULL == list){printf("The list is empty.\n");return NULL;}while (NULL != pn){if(pn->data.age == age){if(pn->pnext == NULL){printf("p->pnext is NULL \n");p->pnext = NULL;}else{p->pnext=pn->pnext;pn->pnext->ppre=p;}free(pn);pn = NULL;break;}p=pn;printf("age = %d \n",pn->data.age);pn=pn->pnext;}return  list;}/*向链表pos位置插入新节点项**/PNODE insert_data(PNODE list,int pos){PNODE p  = NULL ;PNODE pn = NULL;PNODE q  = NULL;char *name = NULL;int cnt = 0;int age =0 ;int i = 0;p=list;pn=p->pnext;name = (char *)malloc(LEN_NAME);while(p != NULL){cnt++;p=p->pnext;}if(cnt){printf("the pos belong to 0--%d.\n",cnt);if(pos > cnt){printf("the pos is over .\n");printf("max is %d .\n",cnt);return NULL;}for(i=0;i<pos;i++){p=pn;pn=pn->pnext;}}else{p=list;pn=p->pnext;}if( !(pos > 0 && pos < cnt)){printf("Your input is over.\n");return NULL;}if (p->data.age)printf("age = %d .\n",p->data.age);printf("Please input the name .\n");scanf("%s",name);printf("Please input the age.\n");scanf("%d",&age);q = (PNODE)malloc(sizeof(NODE));strcpy(q->data.name,name);q->data.age=age;q->pnext=p->pnext;q->ppre=p;p->pnext=q;pn->ppre=q;return list;}/**删除整个链表**/int Delete_all_dou(PNODE list){PNODE p = NULL;PNODE pn = NULL;if(list == NULL){printf("list is empty.\n");return -1;}p=list;pn=p->pnext;while( pn != NULL){if(pn->pnext == NULL)  /*判断是否是末尾*/{p->pnext=NULL;pn=NULL;}else{p->pnext = pn->pnext;pn->pnext->ppre=p;}free(pn);pn = NULL;p=list;pn=p->pnext;}return 0;}/***删除所有相同age的链表项***/PNODE  delete_dou_all(PNODE list, int age){PNODE p = NULL;PNODE pn = NULL;char flag = 0; /*重新开始标记位**/if(NULL == list){printf("The list is empty.\n");return NULL;}p = list;pn = list->pnext;while(NULL != pn){if(pn->data.age == age){if(pn->pnext == NULL){printf("p->pnext is NULL \n");p->pnext = NULL;}else{p->pnext=pn->pnext;pn->pnext->ppre=p;}free(pn);pn = NULL;  //删除一个结束p = list;pn = list->pnext;flag = 1;}if (!flag){p=pn;pn=pn->pnext;}flag = 0 ;}return  list;}int print_link(PNODE list){PNODE p = NULL;p=list;p=p->pnext;if(NULL == list){printf("The list is empty.\n");return -1;}while(p != NULL){printf(" List: name :%s  age:%d   \t \n",p->data.name,p->data.age);p=p->pnext;}return 0;}void main(){PNODE list = NULL;PNODE Delete_Result = NULL;PNODE Delete_Result_age = NULL;PNODE Insert_Result = NULL;char *name;int age = 0;int pos = 0;int ret = 0;name = (char *)malloc(LEN_NAME);printf("Welcom to test the double link list .\n");/***创建链表***/list=create_dou_link();print_link(list);#if 0/**根据name删除特定链表项***/printf("Input the name to delete:\n");scanf("%s",name);Delete_Result = Delete_x_dou_link(list,name);print_link(Delete_Result);free(name);/***根据age删除特定链表项***/printf("Input the age to delete one. \n ");scanf("%d",&age);Delete_Result_age = Delete_Dou(list,age);print_link(Delete_Result);/**删除所有相同age的链表项*/printf("Please input the age to delete all the same.\n");scanf("%d",&age);Delete_Result_age= delete_dou_all(list,age);print_link(Delete_Result);/***向链表位置pos,插入新节点数***/printf("Please input the pos to insert.\n");scanf("%d",&pos);Insert_Result =  insert_data(list,pos);print_link(Insert_Result);#endifprintf("Delete all of the list.\n");ret = Delete_all_dou(list);if(list->pnext == NULL)printf("The list is empty\n");if ( ret == 0 ){        printf("It's is OK.\n");}while(1);}

原创粉丝点击