单链表属于线性表的一种,不过与顺序表相比,线性单链表没有空间的限制,往往可以无限开辟空间并使用(除非内存不足)。但正是因为如此,在进行链表的插入、删除、等操作时,也比顺序结构的链表更为复杂,效率也不及顺序表,即时间复杂度高。
链表的节点结构如图:
链表结构:
以下是代码:
头文件List.h
#ifndef _LIST_H
#define _LIST_H
#include<iostream>
#include<assert.h>
using namespace std;
#define ElemType int
typedef struct Node
{
ElemType data;
struct Node *next;
}Node, *PNode;
typedef struct List
{
PNode first;
PNode last;
size_t size;
}List;
//
void InitList(List *list);
bool push_back(List *list, ElemType x);
bool push_front(List *list, ElemType x);
bool pop_back(List *list);
bool pop_front(List *list);
Node* Find(List *list, ElemType key);
bool delete_val(List *list, ElemType key);
void ShowList(List *list);
void clear(List *list);
void destroy(List *list);
bool resver(List *list);
bool insert_val(List *list,ElemType key);
ElemType getvalue(List *list,int x);
bool modify(List *list,ElemType item,ElemType nitem);
bool sort(List *list);
int length(List *list);
ElemType next(List *list,ElemType key);
ElemType prio(List *list,ElemType key);
#endif
各种函数实现List.cpp:
#include"List.h"
void InitList(List *list)
{
Node *s = (Node *)malloc(sizeof(Node));
assert(s != NULL);
s->next = NULL;
list->first = list->last = s;
list->size = 0;
}
bool push_back(List *list, ElemType x)
{
Node *s = (Node*)malloc(sizeof(Node));
if(s == NULL)
return false;
s->data = x;
s->next = NULL;
list->last->next = s;
list->last = s;
list->size++;
return true;
}
bool push_front(List *list, ElemType x)
{
Node *s = (Node *)malloc(sizeof(Node));
if(s == NULL)
return false;
s->data = x;
s->next = list->first->next;
list->first->next = s;
if(list->size == 0)
{
list->last = s;
}
list->size++;
return true;
}
void ShowList(List *list)
{
Node *p = list->first->next;
while(p != NULL)
{
cout<<p->data<<"-->";
p = p->next;
}
cout<<"Nul"<<endl;
}
bool pop_back(List *list)
{
if(list->size == 0)
return false;
Node *pre = list->first;
while(pre->next != list->last)
pre = pre->next;
pre->next = NULL;
free(list->last);
list->last = pre;
list->size--;
return true;
}
bool pop_front(List *list)
{
if(list->size == 0)
return false;
Node *p = list->first->next;
if(list->size == 1)
{
free(p);
list->first->next = NULL;
list->last = list->first;
}
else
{
list->first->next = p->next;
free(p);
}
list->size--;
return true;
}
Node* Find(List *list, ElemType key)//若找到,返回该节点前一节点的地址
{
Node *p = list->first;
while(p->next->next!=NULL && p->next->data!=key)
p = p->next;
if (p->next==list->last&&p->next->data!=key)
{
return NULL;
}
return p;
}
bool delete_val(List *list, ElemType key)
{
Node *p = Find(list,key);
if(p == NULL)
{
cout<<"该值不存在!"<<endl;
return false;
}
Node *s=p->next;
p->next=s->next;
free(s);
return true;
}
void clear(List *list)
{
Node *p = list->first->next;
while(p != NULL)
{
list->first->next = p->next;
free(p);
p = list->first->next;
}
list->last = list->first;
list->size = 0;
}
void destroy(List *list)
{
clear(list);
free(list->first);
list->first = list->last = NULL;
}
bool insert_val(List *list,ElemType key)
{
Node *s=(Node *)malloc(sizeof(Node));
if (s==NULL)
{
return false;
}
s->data=key;
if (list->size==0)
{
list->first->next=s;
list->last->next=s;
s->next=NULL;
}
else
{
Node *p=list->first;
while (p->next!=NULL&&p->next->data<key)
{
p=p->next;
}
s->next=p->next;
p->next=s;
}
list->size++;
return true;
}
ElemType getvalue(List *list,int x)
{
if (list->size==0||x<1||x>list->size)
{
return -1;
}
Node *p=list->first;
for (int i=0;i<x;i++)
{
p=p->next;
}
return p->data;
}
bool modify(List *list,ElemType item,ElemType nitem) //如何精简?
{
if (list->size==0)
{
return false;
}
Node *p=list->first;
Node *s=(Node *)malloc(sizeof(Node));
s->data=nitem;
if (p->next->data==item)
{
s->next=p->next->next;
free(p->next);
p->next=s;
}
while (p->next->next!=NULL&&p->next->data!=item)
{
p=p->next;
}
if (p->next==list->last)
{
if (p->next->data==item)
{
s->next=NULL;
free(p->next);
p->next=s;
list->last=s;
return true;
}
return false;
}
s->next=p->next->next;
free(p->next);
p->next=s;
return true;
/*
if (list->size==0) //应用改变指针的方法来修改
{
return false;
}
Node *p=list->first;
while (p->next!=NULL&&p->data!=item)
{
p=p->next;
}
if (p==list->last)
{
if (p->data==item)
{
p->data=nitem;
}
return false;
}
p->data=nitem;
return true;*/
}
bool sort(List *list)
{
if (list->size==0||list->size==1)
{
return false;
}
Node *p=list->first;
for (;p->next!=NULL;p=p->next)
{
for (Node *q=p->next;q->next!=NULL;)
{
if (p->next->data>q->next->data)
{
Node *ptmp=q->next;
q->next=ptmp->next;
ptmp->next=q;
p->next=ptmp;
/*
ElemType tmp=p->next->data;
p->next->data=p->next->next->data;
p->next->next->data=tmp;*/
}
}
}
return true;
}
bool resver(List *list)
{
if (list->size==0||list->size==1)
{
return false;
}
Node *fp=list->first->next;
Node *p=fp->next;
Node *lp=p->next;
fp->next= NULL;
list->last->next=fp;
if (lp!=NULL)
{
do
{
p->next=fp;
fp=p;
p=lp;
lp=lp->next;
}while(p->next!=NULL);
list->first->next=fp;
}
else
{
list->first->next=p;
p->next=fp;
}
return true;
}
int length(List *list)
{
return list->size;
}
ElemType next(List *list,ElemType key)
{
if (list->size==0||list->size==1)
{
return -1;
}
Node *p=list->first;
while (p->next!=NULL)
{
if (p->next->data==key&&p->next->next!=NULL)
{
p=p->next;
return p->next->data;
}
p=p->next;
}
return -1;
}
ElemType prio(List *list,ElemType key)
{
if (list->size==0||list->size==1)
{
return -1;
}
Node *p=list->first;
if (p->next->data==key)
{
return -1;
}
while (p->next!=NULL)
{
if (p->next->data==key)
{
return p->data;
}
p=p->next;
}
return -1;
}
最后是主函数main.cpp:
#include"List.h"
//#include<vld.h>
void main()
{
List mylist;
InitList(&mylist);
int select = 1;
ElemType item,nitem;
Node *p = NULL;
int pos;
while(select)
{
cout<<"**********************************"<<endl;
cout<<"* [1] push_back [2] push_front *"<<endl;
cout<<"* [3] show_seqlist[0] quit_system*"<<endl;
cout<<"* [4] pop_back [5] pop_front *"<<endl;
cout<<"* [6] insert_pos [7] insert_val *"<<endl;
cout<<"* [8] delete_pos [9] delete_val *"<<endl;
cout<<"* [10] find [11]getvalue *"<<endl;
cout<<"* [12] modify [13]clear *"<<endl;
cout<<"* [14] destroy [15]sort *"<<endl;
cout<<"* [16] resver [17]length *"<<endl;
cout<<"* [18] next [19]prio *"<<endl;
cout<<"**********************************"<<endl;
cout<<"请选择:>";
cin>>select;
switch(select)
{
case 1:
cout<<"请输入要插入的数据(-1结束):>";
while(cin>>item,item!=-1)
{
push_back(&mylist,item);
}
break;
case 2:
cout<<"请输入要插入的数据(-1结束):>";
while(cin>>item,item!=-1)
{
push_front(&mylist,item);
}
break;
case 3:
ShowList(&mylist);
break;
case 4:
pop_back(&mylist);
break;
case 5:
pop_front(&mylist);
break;
case 6:
break;
case 7:
cout<<"请输入要插入的值:>";
cin>>item;
insert_val(&mylist,item);
break;
case 8:
break;
case 9:
cout<<"请输入要删除的值:>";
cin>>item;
delete_val(&mylist,item);
break;
case 10:
cout<<"请输入要查找的值:>";
cin>>item;
p = Find(&mylist,item);
if (p!=NULL)
{
cout<<"该值是:"<<p->next->data<<endl;
}
else
{
cout<<"该值不存在!"<<endl;
}
break;
case 11:
cout<<"请输入要查找的值的序号:>";
cin>>item;
cout<<getvalue(&mylist,item)<<endl;
break;
case 12:///
cout<<"请输入要修改的值:>";
cin>>item;
cout<<"请输入要新的值:>";
cin>>nitem;
modify(&mylist,item,nitem);
break;
case 13:
clear(&mylist);
break;
case 14:
destroy(&mylist);
break;
case 15:/ERROR!
sort(&mylist);
break;
case 16:ERROR
resver(&mylist);
break;
case 17:
cout<<length(&mylist)<<endl;
break;
case 18:
cout<<"请输入一个值:";
cin>>item;
cout<<next(&mylist,item)<<endl;
break;
case 19:
cout<<"请输入一个值:";
cin>>item;
cout<<prio(&mylist,item)<<endl;
break;
default:
break;
}
}
destroy(&mylist);
}