Dev C++
实现了创建、增、删、改、查、找最大、找最小、置空、数组转链表、链表合并
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef struct LNode
{
int data;
LNode *next;
} LNode;
void Print(LNode *L)
{
cout<<"Print: ";
LNode *p = L->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
return ;
}
void InitList(LNode *&L)
{
L = (LNode*)malloc(sizeof(LNode));
L->next = NULL;
}
//插入到指定位置
void Insert(LNode *&L,int pos,int e)
{
LNode *p = L;
for(int i = 0; i<pos; ++i)
{
if(p==NULL)
{
cout<<"ERROR"<<endl;
return ;
}
p = p->next;
}
LNode *q = (LNode*)malloc(sizeof(LNode));
q->data = e;
q->next = NULL;
q->next = p->next;
p->next = q;
return ;
}
//头插法
void InsertHead(LNode *&L,int e)
{
LNode *p = (LNode*)malloc(sizeof(LNode));
p->next = NULL;
p->data = e;
p->next = L->next;
L->next = p;
return ;
}
//尾插法
void InsertTail(LNode *&L,int e)
{
LNode *r;
LNode *p = L;
while(p->next!=NULL)
p = p->next;
r = p;
LNode *q = (LNode*)malloc(sizeof(LNode));
q->data = e;
q->next = NULL;
r->next = q;
return ;
}
//数组转为链表,flag为0则选用头插法,为1则选用尾插法
void InsertArray(LNode *&L,int A[],int n,int flag)
{
if(flag == 0)
for(int i = 0; i<n; ++i)
InsertHead(L,A[i]);
else if(flag == 1)
for(int i = 0; i<n; ++i)
InsertTail(L,A[i]);
}
//删除指定元素
void Delete(LNode *L,int e)
{
LNode *p = L;
LNode *q;
while(p->next!=NULL)
{
if(p->next->data == e)
{
q = p->next;
p->next = q->next;
free(q);
return ;
}
p = p->next;
}
cout<<"Not found"<<endl;
return ;
}
void MakeEmpty(LNode *&L)
{
LNode *p = L;
LNode *q;
while(p->next!=NULL)
{
q = p->next;
p->next = q->next;
free(q);
}
return ;
}
void isEmpty(LNode *L)
{
if(L->next==NULL)
cout<<"The List is empty"<<endl;
else
cout<<"The List is not empty"<<endl;
}
int Length(LNode *L)
{
LNode *p = L->next;
int len = 0;
while(p != NULL)
{
++len;
p = p->next;
}
return len;
}
void Change(LNode *L,int e,int c)
{
LNode *p = L->next;
while(p!=NULL)
{
if(p->data == e)
{
p->data = c;
return ;
}
p = p->next;
}
cout<<"Not found"<<endl;
return ;
}
int Find(LNode *L,int e)
{
int n = 0;
LNode *p = L->next;
while(p!=NULL)
{
if(p->data == e)
return n;
p = p->next;
++n;
}
// cout<<"Can't found"<<endl;
return -1;
}
int FindMax(LNode *L)
{
LNode *max = L->next;;
LNode *p = L->next;
while(p != NULL)
{
if(p->data > max->data)
max = p;
p = p->next;
}
return max->data;
}
int FindMin(LNode *L)
{
LNode *min = L->next;
LNode *p = L->next;
while(p!=NULL)
{
if(p->data<min->data)
min = p;
p = p->next;
}
return min->data;
}
//L1与L2均为非递减排列,合并为一个非递减链表
void Merge(LNode *L,LNode *L1,LNode *L2)
{
LNode *p = L;
LNode *p1 = L1->next;
LNode *p2 = L2->next;
LNode *q, *r = p;
while(p1!=NULL && p2!=NULL)
{
if(p1->data >= p2->data)
{
q = p2;
p2 = p2->next;
q->next = r->next;
r->next = q;
r = r->next;
}
else
{
q = p1;
p1 = p1->next;
q->next = r->next;
r->next = q;
r = r->next;
}
}
if(p1!=NULL)
r->next = p1;
if(p2 != NULL)
r->next = p2;
return ;
}
int main()
{
LNode *L;
InitList(L);
InsertHead(L,2222);
InsertHead(L,123);
InsertHead(L,456);
Print(L);
cout<<endl;
InsertTail(L,98);
InsertTail(L,97);
Print(L);
cout<<endl;
Delete(L,123);
Print(L);
cout<<endl;
Insert(L,1,678);
Print(L);
Insert(L,0,78);
Print(L);
Insert(L,6,80);
Print(L);
cout<<endl;
Change(L,456,789);
Print(L);
int n,e;
e = 789;
n = Find(L,e);
if(n==-1)
cout<<"Position of "<<e<<": "<<"Can't found"<<endl;
else
cout<<"Position of "<<e<<": "<<n<<endl;
e = 123;
n = Find(L,e);
if(n==-1)
cout<<"Position of "<<e<<": "<<"Can't found"<<endl;
else
cout<<"Position of "<<e<<": "<<n<<endl;
Print(L);
int max;
max = FindMax(L);
cout<<"MAX: "<<max<<endl;
int min;
min = FindMin(L);
cout<<"MIN: "<<min<<endl;
int a[5] = {7,5,4,3,2};
int b[3] = {1,6,8};
LNode *La;
LNode *Lb;
InitList(La);
InitList(Lb);
InsertArray(La,a,5,0);
Print(La);
InsertArray(Lb,b,3,1);
Print(Lb);
LNode *Lc;
InitList(Lc);
Merge(Lc,La,Lb);
Print(Lc);
int len = 0;
isEmpty(Lc);
len = Length(Lc);
cout<<"The length of Lc: "<<len<<endl;
MakeEmpty(Lc);
isEmpty(Lc);
len = Length(Lc);
cout<<"The length of Lc: "<<len<<endl;
return 0;
}