最近又动手重写了插入,快速排序,单链表,环状双链表,代码如下

<pre name="code" class="cpp"><pre name="code" class="cpp">#include<iostream>
using namespace std;
 
//线性链表-单链表,实现增删改查功能,仅作为基本的逻辑实现,至于判空什么都不做了
typedef struct list_node
{
	list_node* next;
	int data;
}list_node;

typedef struct list
{
	list_node* head;
	int size;
}list;

static list_node* creat_node(int val)
{
	list_node* temp=(list_node*)malloc(sizeof(list_node));
	temp->data=val;
	temp->next=NULL;
	return temp;
}
static void destroy_node(list_node* &node)
{
	free(node);
	node=NULL;
}

//头结点不存数据,仅存一个指向下一个节点的指针,这样实际的数据存储开始于header->next,这样可以做到增删的逻辑一致性
void init_list(list &List)
{
	List.head=(list_node*)malloc(sizeof(list_node));
	List.head->next=NULL;
	List.size=0;
}

bool insert_data(list &List,int val)
{
	list_node* temp=creat_node(val);

	temp->next=List.head->next;
	List.head->next=temp;
	List.size++;
	return 1;
}

//如果flags为0,表示查找指向当前值所在的节点的指针
//如果flags为1,表示查找指向当前值所在节点的前一个节点的指针
list_node* find_data(list List,int val,int flags)
{
	list_node* temp=List.head;
	list_node* prior=List.head;
	while(temp->data!=val)
	{
		prior = temp;
		temp=temp->next;
	}
	return  flags == 0? temp:prior;
}

bool delete_data(list &List,int val)
{
	list_node* temp=find_data(List,val,1);
	list_node* ready_del_node=temp->next;
	temp->next=ready_del_node->next;
	free(ready_del_node);
	ready_del_node=NULL;
	List.size--;
	return 1;
}
int main()
{
	list List;
	init_list(List);
	insert_data(List,1);
	insert_data(List,2);
	insert_data(List,3);
	insert_data(List,4);
	insert_data(List,5);
	list_node* p=List.head->next;
	for(int i=0;i<5;i++)
	{
		cout<<p->data<<endl;
		p=p->next;
	}
	cout<<endl;
	cout<<List.size<<endl;
	delete_data(List,1);
	p=List.head->next;
	for(int i=0;i<4;i++)
	{
		cout<<p->data;
		p=p->next;
	}
	return 0;
}

#include<iostream>using namespace std;//线性链表-环状双链表typedef struct double_list_node{struct double_list_node* prior;struct double_list_node* next;int data;}double_list_node;typedef struct double_list{double_list_node* header;int size;}double_list;//和单链表相似,这里加一个只存指针值的节点,该header的prior指向数据存储最后一个位置,next指向数据存储最开始一个位置//这样使得增删的逻辑判断表现出一致性void init_double_list(double_list &List){List.header=(double_list_node*)malloc(sizeof(double_list_node));List.header->prior=List.header;List.header->next=List.header;List.size=0;}void insert_data(double_list &List,int val){double_list_node* temp=(double_list_node*)malloc(sizeof(double_list_node));temp->data=val;temp->next=List.header->next;temp->prior=List.header;List.header->next->prior=temp;List.header->next=temp;}//找与当前值相等的值的节点指针double_list_node* find_data(double_list List,int val){double_list_node* temp=List.header->next;while(temp->data!=val){temp=temp->next;}return temp;}void delete_data(double_list &List,int val){double_list_node* temp=find_data(List,val);temp->prior->next=temp->next;temp->next->prior=temp->prior;free(temp);temp=NULL;}int main(){double_list List;init_double_list(List);insert_data(List,5);insert_data(List,4);insert_data(List,3);insert_data(List,2);insert_data(List,1);double_list_node* temp=List.header->next;while(temp!=List.header){cout<<temp->data;temp=temp->next;}cout<<endl;delete_data(List,4);temp=List.header->next;while(temp!=List.header){cout<<temp->data;temp=temp->next;}}
 

插入排序
int main()
{
<span style="white-space:pre">	</span>int ia[]={2,1,23,45,67,24,35,27};
<span style="white-space:pre">	</span>//vector<int>vec(ia,sizeof(ia)/sizeof(int));
<span style="white-space:pre">	</span>for(int i=1;i<sizeof(ia)/sizeof(int);i++)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">			</span>int key=ia[i];
<span style="white-space:pre">			</span>int m=i-1;
<span style="white-space:pre">			</span>while(m>=0 && key<ia[m])
<span style="white-space:pre">			</span>{
<span style="white-space:pre">				</span>ia[m+1]=ia[m];
<span style="white-space:pre">				</span>m=m-1;
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>ia[m+1]=key;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>for(int i=0;i<sizeof(ia)/sizeof(int);i++)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>cout<<ia[i]<<endl;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>return 0;
}

//快速排序,其中start为数组是及开始下标,finish为实际结束下标,即第一个位置为下标0
int part(int a[],int start,int finish)
{
<span style="white-space:pre">	</span>int stand=a[finish];
<span style="white-space:pre">	</span>int i=start-1;//注意边界条件是从start-1开始的
<span style="white-space:pre">	</span>for(int j=start;j<finish;j++)//注意边界条件是从start开始的
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>if(stand>=a[j])
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>//将i加1,然后交换a[j]和a[i+1]
<span style="white-space:pre">			</span>int temp;
<span style="white-space:pre">			</span>i=i+1;
<span style="white-space:pre">			</span>temp=a[i];
<span style="white-space:pre">			</span>a[i]=a[j];
<span style="white-space:pre">			</span>a[j]=temp;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>a[finish]=a[i+1];
<span style="white-space:pre">	</span>a[i+1]=stand;
<span style="white-space:pre">	</span>return i+1;
}


 void quick(int a[],int start,int finish)
{
<span style="white-space:pre">	</span>if(start<finish)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>int index=part(a,start,finish);
<span style="white-space:pre">		</span>quick(a,start,index-1);
<span style="white-space:pre">		</span>quick(a,index+1,finish);
<span style="white-space:pre">	</span>}
}


int main()
{
<span style="white-space:pre">	</span>int ia[]={2,1,23,45,67,24,35,27};<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>quick(ia,0,7);
<span style="white-space:pre">	</span>for(int i=0;i<sizeof(ia)/sizeof(int);i++)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>cout<<ia[i]<<endl;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值