C语言自写链表list
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include <math.h>
#include <malloc.h>
struct Point1;
typedef struct Point1 *List1;
typedef struct Point1 *position1;
typedef int ElementType;
#define Llength sizeof(struct Point1)
struct Point1 // 整个.c文件的缺陷,不能用查找和交换函数,查找或替换两个及以上的相同数据域的结点
{
ElementType value;
position1 next;
};
List1 list1_set() // 用动态链表的方法创建一个新链表
{
int n=0;
ElementType TheEnd=999;
List1 head,p1,p2;
p1=p2=(List1)malloc(Llength);
printf("\n正在创建一个动态链表...(以999结束创建):\n\n");
head=NULL;
while(p1->value!=TheEnd) // 输入数字 999时链表创建结束
{
n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(List1)malloc(Llength);
printf("请输入一个链表结点:\n");
scanf("%d",&p1->value);
}
p2->next=NULL;
return head;
}
void DeleteList1(List1 begin) //释放一个链表的空间
{
position1 p,temp;
p=begin->next;
begin->next=NULL;
while(p!=NULL)
{
temp=p->next;
free(p);
p=temp;
}
printf("\n\nThe List are free!\n");
}
void PrintfList1(List1 Beg) // 打印一个链表的所有数据域
{
position1 p,temp;
p=Beg->next;
printf("\n 现在链表里所存储的数据为:\n\n");
while(p!=NULL)
{
temp=p->next;
printf("\t%d \t-> ",p->value);
p=temp;
}
if(p==NULL) printf("NULL");
putchar(10);
}
position1 find1(ElementType x,List1 begin) // 查找链表中的一个结点的数据域
{
unsigned int n=0;
position1 p1;
p1=begin->next;
while(p1!=NULL && p1->value!=x)
{
p1=p1->next;
n=1;
}
if(n) return p1;
else return NULL;
}
void TailInsert(ElementType x,List1 Beg) // 尾插法
{
position1 e,p=Beg;
while(p!=NULL)
{
e=p;
p=p->next;
}
p=(List1)malloc(Llength);
p->value=x;
p->next=NULL;
e->next=p;
}
/*List1 HeadInsert(ElementType x,List1 Beg) \\头插法
{
List1 Beg;
position1 p;
Beg=(List1)malloc(Llength);
Beg->next=NULL;
p=(List1)malloc(Llength);
p->value=x;
p->next=Beg->next;
Beg->next=p;
} */
void PositionSwap1(ElementType a_pro_value,ElementType a_last_value,List1 begin) // 将链表之中任意顺序的两个结点进行在实际链表中的交换 第 1 代
{
unsigned int n1=0,n2=0,key1=0,key2=0;
position1 a_pro_pro,a_last_pro,a_pro,a_last,fine_value;
fine_value=begin->next;
if(fine_value->value==a_pro_value ) a_pro_pro=begin; // 判断是否是第一个结点进行交换
else
while(fine_value!=NULL && fine_value->value!=a_pro_value) // 查找第一个要交换结点的前一个结点的位置
{
a_pro_pro=fine_value;
fine_value=fine_value->next;
}
if(fine_value!=NULL) n1=1;
if(!n1) printf("\n没有找到输入的第一个数!\n");
fine_value=begin->next;
if(fine_value->value==a_last_value ) a_last_pro=begin;
else
while(fine_value!=NULL && fine_value->value!=a_last_value) // 查找第二个要交换结点的前一个结点的位置
{
a_last_pro=fine_value;
fine_value=fine_value->next;
}
if(fine_value!=NULL) n2=1;
if(!n2) printf("\n没有找到输入的第二个数!\n");
/*****************************使用指针交换结点数据域的过程*************************/
if( a_pro_pro->next==a_last_pro || a_last_pro->next==a_pro_pro ) // 相邻结点的交换
{
a_pro=a_pro_pro->next;
a_last= a_last_pro->next;
a_pro->next=a_last->next;
a_last->next=a_pro_pro->next;
a_pro_pro->next=a_last;
}
else if(n1 && n2) // 不相邻结点的交换
{
fine_value=a_pro_pro->next->next;
a_pro_pro->next->next=a_last_pro->next->next;
a_last_pro->next->next=fine_value;
fine_value=a_last_pro->next;
a_last_pro->next=a_pro_pro->next;
a_pro_pro->next=fine_value;
}
}
void PositionSwap2(ElementType target1,ElementType target2,List1 L)// 将链表之中任意顺序的两个结点进行在实际链表中的交换 第 2 代
{
position1 P1,P2,La1,La2,temp,temp1,temp2;
P1=find1(target1,L);
P2=find1(target2,L);
if(P1!=NULL && P2!=NULL)// 判断是否是两个结点都在链表中
if(P1->next->value!=target2 ) // 不相邻结点的交换
{
temp=L;
while(temp->next!=NULL && temp->next->value!=target1)
temp=temp->next;
P1=temp;
La1=temp->next->next;
temp1=P1->next;
temp=L;
while(temp->next!=NULL && temp->next->value!=target2)
temp=temp->next;
P2=temp;
La2=temp->next->next;
temp2=P2->next;
temp1->next=La2;
temp2->next=La1;
P1->next=temp2;
P2->next=temp1;
}
else // 相邻结点的交换
{
temp=L;
while(temp->next!=NULL && temp->next->value!=target1)
temp=temp->next;
P1=temp;
P2=P1->next;
La1=P1->next->next;
La2=P2->next->next;
P2->next=La2;
La1->next=P2;
P1->next=La1;
}
else printf("Have One Targe Of Two Swap Num Don't Find!\n");
}
int main()
{
unsigned int n=0,h=0;
ElementType c1=0,c2=0,ch=9999;
ElementType b;
List1 begin,fineresult;
begin=list1_set();
PrintfList1(begin);
while(1)
{
printf("\n***************《菜单》***************\n");
printf("\n1.查找一个数据");
printf("\n2.交换前后两个数据");
printf("\n3.在链表后面增加一个数据");
printf("\n**************************************\n\n");
scanf("%d",&n);
if(n==1)
{
printf("\n请输入需要查找的数字项:\n");
scanf("%d",&b);
if((fineresult=find1(b,begin)) !=NULL) printf("the fine result is: %d\n",fineresult->value);
else printf("No Found!!!\n");
}
else if(n==2)
{
printf("\n请输入需要交换的两个链表数字项:\n");
scanf("%d%d",&c1,&c2);
PositionSwap2(c1,c2,begin);
PrintfList1(begin);
}
else if(n==3)
{
printf("\n请输入需要插入的数:\n");
scanf("%d",&ch);
if(ch==9999) printf("插入数据失败!\n");
else
{
TailInsert(ch,begin);
PrintfList1(begin);
}
}
else printf("\n\n没有该功能项!!\n");
printf("\n是否继续?\n");
printf("1.是\n");
printf("2.否\n");
scanf("%d",&h);
if(h==2) break;
}
getchar();
DeleteList1(begin);
return 0;
}
C++语言STL库链表list使用
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
//学习网址 https://www.xuebuyuan.com/1335998.html
bool rmp(const int& num)
{
return num<3;
}
inline void ouput_list(list<int>& temp)
{
for(list<int>::iterator i(temp.begin());i!=temp.end();++i)
{
cout<<*i<<" ";
}
cout<<endl;
}
inline list<int>::iterator list_node(list<int>& temp,int num)
{
list<int>::iterator p=temp.begin();
for(int i(0);i<num;i++)
{
p++;
}
return p;
}
int main()
{
list<int>lst1(3,2);
ouput_list(lst1);
lst1.push_back(6);
lst1.push_front(6);
ouput_list(lst1);
lst1.remove_if(rmp);
ouput_list(lst1);
lst1.insert(list_node(lst1,1),3);
ouput_list(lst1);
return 0;
}