数据结构作业1

  1. 设线性表存储在数组A[1..arrsize]的前elenum个分量中,且递增有序。试编写一个算法:在线

    性表中插入元素x,需保持线性表的有序性,并且分析算法的时间复杂度。
    #include<iostream>
    using namespace std;
    //算法1,时间复杂度为O(n) 
    void insert_1(int val,int a[],int arrsize,int &elenum){
    	for(int i=0;i<elenum;i++)
    	{
    		if(val<=a[i])
    		{
    			elenum++;
    			for(int j=elenum;j>i;j--)
    			{
    				a[j]=a[j-1];
    			}
    			a[i]=val;
    			return ;
    			
    			
    		}
    	}
    	elenum++;
    	a[elenum]=val; 
    
    	
    }
    //
    void insert_2(int val,int a[],int arrsize,int &elenum){
    	int left=0;
    	int right=elenum;
    	int mid=(elenum)/2;
    	int i=-1;//i为找的位置 
    	while(left!=mid)
    	{
    		if(val>a[mid])
    		{
    			left=mid;
    			mid=(left+right)/2;
    		}
    		else if(val==a[mid])
    		{
    			i=mid;
    		}
    		else
    		{
    			right=mid;
    			mid=(left+right)/2;
    		}
    		i=right;
    		
    	}
    	elenum++;
    	for(int j=elenum;j>i;j--)
    	{
    	    a[j]=a[j-1];
    	}
    	a[i]=val;
    
    
    		
    	
    }
    int main() 
    {
    	int a[100]={1,4,6,7,8,10,13,15};
    	int arrsize=100;
    	int elenum=8;
    	insert_1(9,a,arrsize,elenum);
    	for(int i=0;i<elenum;i++)
    	{
    		cout<<a[i]<<"  ";
    		if(i==elenum-1){
    			cout<<endl;
    		}
    	}
    	insert_2(5,a,arrsize,elenum);
    	for(int i=0;i<elenum;i++)
    	{
    		cout<<a[i]<<"  ";
    		if(i==elenum-1){
    			cout<<endl;
    		}
    	}
    	
    }

  2. 分别用数组和单链表作存储结构,编写一个实现线性表中元素逆置的算法。

         (1)数组作存储结构

#include<iostream>
using namespace std;
void arrfx(int arr[],int arrlenth)
{
    int n=arrlenth;
	int newarr[n];
	for(int i=0;i<n;i++)
	{
		newarr[i]=arr[n-i-1];
	}
	
	for(int i=0;i<n;i++)
	{
		arr[i]=newarr[i];
		
	} 
	
	
	
}
int main()
{
	int arr[10]={1,23,56,78,99,0,9,8,9,10};
	int n = sizeof(arr) / sizeof(arr[0]);
	arrfx(arr,n);
	for(int i=0;i<n;i++)
	{
		cout<<arr[i]<<"  ";
		if(i==n-1){
			cout<<endl;
		}
	}
	return 0;
} 

(2)单链表作存储结构

  1. #include<iostream>
    using namespace std;
    
    struct node
    {
    	int data;
    	node *next;
    	
    };
    
    node* creatlist()
    {
    	node *head=new node;
    	head->next=NULL;
    	return head;
    }
    void addnode(node *head,int _data)
    {
    	node *p=new node;
    	p=head;
    	node *newnode =new node;
    	newnode->data=_data;
    	newnode->next=NULL;
    	while(p->next!=NULL)
    	{
    		p=p->next;
    	}
    	p->next=newnode;
    	return ;		
    }
    
    void InsertHead(node *head,int _data)
    {
    	node *tempnode=new node;
    	node *newnode =new node;
    	newnode->data=_data;
    	newnode->next=NULL;
    	
    	if(head->next==NULL)
    	{
    		head->next=newnode;
    	}
    	else{
    		tempnode=head->next;
    		head->next=newnode;
    		newnode->next=tempnode;
    		
    	}
    	return ;		
    }
    void showlist(node* head)
    {
    	node *p=head->next;
    	while(p)
    	{
    		cout<<p->data<<"  ";
    		p=p->next; 
    	}
    	cout<<endl;
    }
    int main()
    {
    	//创建链表并实例化 
    	node *head=new node;
    	head=creatlist();
    	addnode(head,5);
    	addnode(head,7);
    	addnode(head,8);
    	addnode(head,18);
    	addnode(head,1);
    	showlist(head);
    	//
    	
    	 
    	//逆置算法
    	
    	node *p=new node;
    	node *t=new node;
    	p=head->next;
    	head->next=NULL; 
    	while(p)
    	{
    	    t=p;
    		InsertHead(head,t->data);
    		p=p->next;
    		
    	}
    	showlist(head);
    	//
    	return 0;
    	
    	
    }

  2. 试编写一个算法,找出一个循环链表中的最小值并删除

    #include<iostream>
    using namespace std;
    
    struct node
    {
    	int data;
    	node *next;
    	
    };
    
    node* creatlist()
    {
    	node *head=new node;
    	head->next=head;
    	return head;
    }
    void addnode(node *head,int _data)
    {
    	node *p=new node;
    	p=head;
    	node *newnode =new node;
    	newnode->data=_data;
    	newnode->next=head;
    	while(p->next!=head)
    	{
    		p=p->next;
    	}
    	p->next=newnode;
    	return ;		
    }
    
    		
    
    void showlist(node* head)
    {
    	node *p=head->next;
    	while(p!=head) 
    	{
    		cout<<p->data<<"  ";
    		p=p->next; 
    	}
    	cout<<endl;
    }
    int main()
    {
    	//创建链表并实例化 
    	node *head=new node;
    	head=creatlist();
    	addnode(head,9);
    	addnode(head,23);
    	addnode(head,3);
    	addnode(head,8);
    	addnode(head,5);
    	addnode(head,6);
    	addnode(head,213);
    	showlist(head);
    	//
    	
    	 
    	//算法
    	int min=head->next->data;
    	node *p=head->next;
    	while(p!=head)
    	{
    		if(min>=p->data)
    		{
    			min=p->data;
    		}
    		p=p->next;
    	}
    	cout<<min;
    	
    	
    	
    	//
    	return 0;
    	
    	
    }

  3. 已知线性表中的元素以值递增有序排列,并以单链表作存储结构。试写一算法,删除表中所有大于x且小于y的元素(若表中存在这样的元素)同时释放被删除结点空间

    #include<iostream>
    using namespace std;
    
    struct node
    {
    	int data;
    	node *next;
    	
    };
    
    node* creatlist()
    {
    	node *head=new node;
    	head->next=NULL;
    	return head;
    }
    void addnode(node *head,int _data)
    {
    	node *p=new node;
    	p=head;
    	node *newnode =new node;
    	newnode->data=_data;
    	newnode->next=NULL;
    	while(p->next!=NULL)
    	{
    		p=p->next;
    	}
    	p->next=newnode;
    	return ;		
    }
    
    void showlist(node* head)
    {
    	node *p=head->next;
    	while(p)
    	{
    		cout<<p->data<<"  ";
    		p=p->next; 
    	}
    	cout<<endl;
    }
    int main()
    {
    	//创建链表并实例化 
    	node *head=new node;
    	head=creatlist();
    	addnode(head,1);
    	addnode(head,1);
    	addnode(head,3);
    	addnode(head,4);
    	addnode(head,5);
    	addnode(head,6);
    	addnode(head,7);
    	showlist(head);
    	//
    	
    	 
    	//算法
    	int x=3,y=7 ;
    	node *p=new node;
    	p=head;
    	
    	while(p->next)
    	{
    		int ifdel=0;
    		node *del=new node;
    		if(p->next->data>x&&p->next->data<y)
    		{
    			node *del=new node;
    			del=p->next;
    			p->next=p->next->next;
    			delete del;
    			ifdel=1;
    			
    			
    		}
    		if(ifdel==1)continue;
    		p=p->next;
    	}
    	showlist(head);
    	
    	//
    	return 0;
    	
    	
    }

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值