链表的操作

typedef int DataType;
struct node //单向链表结点
{
	DataType info;
	node* link;
};
node* creatdown()//向后生成链表
{
	DataType data;
	node* head,*tail,*p;//头指针,尾指针,临时插入的结点指针,尾指针始终指向尾部。
    head=new node ;
	head->info=-1;//表头值赋值为-1,带表头
	head->link=NULL;
	tail=head;
	while (cin>>data)
	{
		if (data==0)//输入0循环结束
		{
			break;
		}
		p=new node;
		p->info=data;
		p->link=NULL; 
		tail->link=p;//链接上去
		tail=p;//指针后移动
	}
	return head;
}
node* del(node* head,int num)//删除num
{
	 node* p1,*p2;
	 p2=head;
	 p1=head->link;
	 while (p1->info!=num&&p1->link!=NULL)
	 {
		p2=p1; 
		p1=p1->link;
	 }
     p2->link=p1->link;
	 delete p1;
	 return head;
}
node* insert(node* head,int num,int a)//插a在num值后面
{
	node* p1;
	p1=head->link;
	while (p1->info!=num&&p1->link!=NULL)
	{
		p1=p1->link;
	}
  node* temp=new node;
  temp->info=a;
  temp->link=p1->link;
  p1->link=temp;
  return head;
}
void swap(int &a,int &b)
{
	a=a+b;
	b=a-b;
	a=a-b;
}
int length(node* head)//求链表的长度
{
	int i=0;
	node* p=head->link;
	while (p!=NULL)
	{
	    i++;
		p=p->link;
	}
	return i;
}
void  BubbleSort(node* head)//链表由小到大排序,采用冒泡法
{
	int n=length(head);
	 if (n<2)
	 {
		 return;
	 }
      node* p=head->link;
	  int flag=n;
	  while (flag>1)
	  {
		  int k=flag;
		  flag=0;
		  p=head->link;
		  for (int i=1;i<k;i++)
		  {
			  if ((p->info)>(p->link->info))
			  {
				  swap(p->info,p->link->info);//前后交换
			  } 
			  p=p->link;
			  flag=i;
		  }
	  }
	  p=head->link;
	 while (p!=NULL)
	 {
		 printf("%d \n", p->info);
          p=p->link;
	 }
}
void reverse(node* head)//逆置
{
	int n=length(head);
	if (n<1)
	{
		return;
	}
  node *p1,*p2,*p3;//待逆置的前一个结点,待逆置的结点,待逆置结点的下一个结点
  p1=head->link;//
  p2=p1->link;
  p1->link=NULL;
 while (p2!=NULL)
 {
	 p3=p2->link;
	 p2->link=p1;//指针逆转
	 p1=p2;//右边滑动
	 p2=p3;//右边滑动
 }
  head->link=p1;//头结点指向逆转后的最前面的结点

  p3=head->link;//用于输出
  while (p3!=NULL)
  {
	  printf("%d \n", p3->info);
	  p3=p3->link;//输出逆转后的信息
  }
}
void BubbleSort(int a[], int n) 
{
	int flag=n;
	while (flag>1)
	{	
		int k=flag;//要增加这个变量,flag变化时,k保持一轮排序互换值的最后位置
		flag=0;//一定要赋值为0,否则一个排好序的数组无法退出该循环
		for (int i=1;i<k;i++)
		{
			if (a[i-1]>a[i])
			{ 
				swap(a[i-1],a[i]);
				flag=i;
			}
		}
	}	
	for (int i = 0; i < n; i++) 
		printf("%d \n", a[i]);
}

struct dnode //双向链表结点
{
	DataType data;
	dnode* next;
	dnode* pre;
};
dnode* creat()//双向链表的创建
{
	DataType data;
	dnode* head,*tail,*p;
	head=new dnode ;
	head->data=-1;//表头值赋值为-1
	head->next=NULL;
	tail=head;
	while (cin>>data)
	{
		if (data==0)
		{
			break;
		}
		p=new dnode;
		p->data=data;
		p->next=NULL; 
		tail->next=p;
		p->pre=tail;
		tail=p;
	}
	head=head->next;
	head->pre=NULL;
    tail->next=NULL;

	p=head;
	while (p!=NULL)
	{
		printf("%d \n", p->data);
		p=p->next;
	}
	return head;    
}
void del(dnode* head,int num)双向链表的删除
{
	dnode* p1;
	p1=head;
	while (num!=p1->data&&p1->next!=NULL)
	{
		p1=p1->next;
	}
	if (num==p1->data)
	{
   if (p1==head)
    {
		head=head->next;
		head->pre=NULL;
		delete p1;
    } 
   else if(p1->next==NULL)
   {
       p1->pre->next=NULL;
	   delete p1;
   }
   else
   {
       p1->next->pre=p1->pre;
	   p1->pre->next=p1->next;
	   delete p1;
   }
	} 
	else
	{
		printf("%d could not been found",num);
	}

	p1=head;
	while (p1!=NULL)
	{
		printf("%d \n", p1->data);
		p1=p1->next;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值