//..... SClist.h
#ifndef _SCLIST_H_
#define _SCLIST_H
#include<stdio.h>
#include<malloc.h>
#include<assert.h>
#include<stdlib.h>
#define ElemType int
typedef struct Node
{
ElemType data;
struct Node *next;
}Node,*PNode;
typedef struct List
{
PNode first;
PNode last;
int size;
}List;
Node * buynode(ElemType x);
void InitSCList(List *list);
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);
void quit_system(List *list,int *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);
#endif
//...............SClist.cpp
#include"SCList.h"
void InitSCList(List *list)
{
Node *s=(Node*)malloc(sizeof(Node));
assert(s!=NULL);
list->first=list->last=s;
list->last ->next=list->first ;
list->size=0;
}
Node* buynode(ElemType x)
{
Node *s=(Node*)malloc(sizeof(Node));
assert(s!=NULL);
s->data =x;
s->next=NULL;
return s;
}
void push_back(List *list, ElemType x)
{
Node *s=buynode(x);
list->last->next =s;
list->last=s;
list->last ->next =list->first ;
list->size ++;
}
void push_front(List *list, ElemType x)
{
Node *s=buynode(x);
s->next=list->first ->next ;
list->first ->next =s;
if(list->first ==list->last )//插入的结点是第一个节点
{
list->last=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 =list->first ;
list->size--;
}
void pop_front(List *list)
{
if(list->size ==0)
return ;
Node *p=list->first ->next;
list->first->next =p->next ;
free(p);
if(list->size ==1) //删除的是最后一个结点
{
list->last=list->first;
}
list->size--;
}
void insert_val(List *list, ElemType x)
{
Node *p=list->first ;
while(p->next!=list->last && p->next ->data <x)
{
p=p->next ;
}
if(p->next ==list->last && p->next->data <x)
{
push_back(list,x);
}
else
{
Node *s=buynode(x);
s->next=p->next;
p->next =s;
list->size++;
}
}
Node* find(List *list, ElemType x)
{
if(list->size==0)
return NULL;
Node *p=list->first->next ;
while(p!=list->first&&p->data!=x)
p=p->next ;
if(p==list->first ) //找了一圈还没找到
return NULL;
return p;
}
int length(List *list)
{
return 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 ) //找到的是最后一个节点
{
pop_back(list);
}
else
{
Node *q=p->next;
p->data =q->data ; //覆盖
p->next =q->next;
free(q);
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 ->next =NULL; //不循环
list->last=s;
list->last->next=list->first ; //链表后面断开
while(q!=NULL)
{
s=q;
q=q->next;
Node *p=list->first ;
while(p->next !=list->last &&p->next ->data <s->data)
{
p=p->next;
}
if(p->next==list->last &&p->next->data <s->data)
{
s->next =list->last ->next ;
list->last ->next =s;
list->last =s;
}
else
{
s->next =p->next ;
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->next =NULL;
list->last =p;
list->last ->next =list->first ;
while(q!=NULL)
{
p=q;
q=q->next;
p->next =list->first ->next ;
list->first ->next =p;
}
}
void clear(List *list)
{
Node *p=list->first ->next ;
while(p!=list->first )
{
list->first ->next=p->next ;
free(p);
p=list->first ->next ;
}
list->last =list->first ;
list->last ->next =list->first ;
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!=list->first ) //不为头,则链表未结束
{
printf("%d-->",p->data);
p=p->next;
}
printf("null.\n");
}
//......List.cpp
#include"SCList.h"
int main()
{
List mylist;
InitSCList(&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-06-02 21:40:48 发布