//...........DList.h
#ifndef _LIST_H_
#define _LIST_H_
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<assert.h>
#define ElemType int
typedef struct Node
{
ElemType data;
struct Node *prio;
struct Node *next;
}Node,*PNode;
typedef struct List
{
PNode first;
PNode last;
int size;
}List;
void InitList(List *list);
Node* buynode(ElemType x);
void show_list(List *list);
void push_back(List *list, ElemType x);
void push_front(List *list, ElemType x);
void pop_back(List *list);
void pop_front(List *list);
void insert_val(List *list, ElemType x);
Node* find(List *list, ElemType x);
int length(List *list);
void delete_val(List *list, ElemType x);
void sort(List *list);
void reverse(List *list);
void clear(List *list);
void destory(List *list);
void quit_system(List *list,int *x);
#endif
//.......DList,cpp
#include "DList.h"
void InitList(List *list)
{
Node *s=(Node *)malloc(sizeof(Node));//申请头结点
assert(s!=NULL);
list->first =list->last =s;
list->last ->next =NULL;
list->first->prio =NULL;
list->size =0;
}
Node* buynode(ElemType x) //购买节点
{
Node *s=(Node*)malloc(sizeof(Node));
assert(s!=NULL);
s->data=x;
s->next =s->prio =NULL;
return s;
}
void push_back(List *list, ElemType x)
{
Node *s=buynode(x);
s->prio=list->last ;
list->last ->next =s;
list->last =s;
list->size ++;
}
void push_front(List *list, ElemType x)
{
Node *s=buynode(x);
//判断添加的结点是否是第一个节点
if(list->first ==list->last )
{
list->last =s;
}
else
{
s->next =list->first ->next ;
s->next ->prio =s;
}
s->prio =list->first ;
list->first ->next =s;
list->size ++;
}
void pop_back(List *list)
{
if(list->size ==0)
return ;
Node *p=list->first ;
while(p->next !=list->last )
p=p->next;
free(list->last );
list->last =p;
list->last ->next =NULL;
list->size --;
}
void pop_front(List *list)
{
if(list->size ==0)
return ;
Node *p=list->first->next ;
if(list->first ->next ==list->last) //只有一个结点
{
list->last =list->first ;
list->last ->next =NULL;
}
else
{
p->next ->prio =list->first ;
list->first ->next =p->next ;
}
free(p);
}
void insert_val(List *list, ElemType x)
{
Node *p=list->first ;
while(p->next !=NULL&&p->next ->data <x)
p=p->next ;
if(p->next ==NULL)
{
push_back(list,x);
}
else
{
Node *s=buynode(x);
s->next =p->next ;
s->next ->prio =s;
s->prio =p;
p->next =s;
list->size ++;
}
}
void delete_val(List *list, ElemType x)
{
if(list->size ==0)
return ;
Node *p=find(list,x);
if(p==NULL)
{
printf("要删除的值不存在.\n");
return ;
}
if(p==list->last ) //删除最后一个节点
{
list->last =p->prio ;
list->last ->next =NULL;
}
else
{
p->next ->prio =p->prio;
p->prio ->next =p->next ;
}
free(p);
list->size--;
}
Node* find(List *list, ElemType x)
{
Node *p=list->first ->next ;
while(p!=NULL&&p->data !=x) //不可交换顺序 若p为空则不会有data域
p=p->next ;
return p;
}
int length(List *list)
{
return list->size;
}
void sort(List *list)
{
if(list->size ==0||list->size ==1)
return ;
Node *s=list->first ->next ;
Node *q=s->next ;
list->last =s;
list->last ->next =NULL;
while(q!=NULL)
{
s=q;
q=q->next ;
Node *p=list->first ;
while(p->next !=NULL&&p->next ->data <s->data )
p=p->next;
if(p->next ==NULL)
{
s->next =NULL;
s->prio =list->last ;
list->last ->next =s;
list->last =s;
}
else
{
s->next =p->next ;
s->next ->prio =s;
s->prio =p;
p->next =s;
}
}
}
void reverse(List *list)
{
if(list->size ==0||list->size ==1)
return ;
Node *p=list->first ->next ;
Node *q=p->next ;
list->last =p;
list->last ->next =NULL;
while(q!=NULL)
{
p=q;;
q=q->next ;
p->next =list->first ->next ;
p->next ->prio =p;
p->prio =list->first ;
list->first ->next =p;
}
}
void clear(List *list)
{
if(list->size ==0)
return ;
Node *p=list->first->next ;
while(p!=NULL)
{
if(p==list->last )
{
list->last =list->first ;
list->last ->next =NULL;
}
else
{
p->next->prio =list->first ;
list->first ->next =p->next ;
}
free(p);
p=list->first ->next ;
}
list->size =0;
}
void destory(List *list)
{
clear(list);
free(list->first );
list->first =list->last =NULL;
}
void quit_system(List *list,int *x)
{
*x=0;
}
void show_list(List *list)
{
Node *p=list->first ->next ;
while(p!=NULL)
{
printf("%d-->",p->data );
p=p->next;
}
printf("null.\n");
}
//.............main.cpp
#include"DList.h"
int main()
{
List mylist;
InitList(&mylist);
ElemType Item;
Node *p=NULL;
int select=1;
Node* pos;
while(select)
{
printf("*********************************\n");
printf("[0] quit_system [1] show_list *\n");
printf("[2] push_front [3] push_back *\n");
printf("[4] pop_front [5] pop_back *\n");
printf("[6] insert_val [7] delete_val *\n");
printf("[8] find_val [9] length *\n");
printf("[10] clear [11] destory *\n");
printf("[12] reverse [13] sort *\n");
printf("*********************************\n");
printf("please input select:>");
scanf("%d",&select);
switch(select)
{
case 0:
quit_system(&mylist,&select);
case 1:
show_list(&mylist);
break;
case 2:
printf("please input data:");
while(scanf("%d",&Item),Item!=-1)
{
push_front(&mylist,Item);
}
break;
case 3:
printf("please input data:");
while(scanf("%d",&Item),Item!=-1)
{
push_back(&mylist,Item);
}
break;
case 4:
pop_front(&mylist);
break;
case 5:
pop_back(&mylist);
break;
case 6:
printf("please input the insert val:");
scanf("%d",&Item);
insert_val(&mylist,Item);
break;
case 7:
printf("please input the delete data:");
scanf("%d",&Item);
delete_val(&mylist,Item);
break;
case 8:
printf("please input the find data:");
scanf("%d",&Item);
pos=find(&mylist,Item);
if(pos== NULL)
{
printf("can't find this data.");
}
break;
case 9:
printf("the list's length is:%d",length(&mylist));
printf("\n");
break;
case 10:
clear(&mylist);
break;
case 11:
destory(&mylist);
break;
case 12:
reverse(&mylist);
break;
case 13:
sort(&mylist);
break;
default:
break;
}
system("pause");
system("cls ");
}
return 0;
}
双链表的实现以及插入删除等操作
最新推荐文章于 2023-07-28 22:19:22 发布