链表的插入 删除 排序 倒叙
[cpp] view plaincopy#include <iostream>
using namespace std;
#include <stdio.h>
#include <stdexcept>
#include <conio.h>
#include <string.h>
#include <stack>
struct Node
{
int data;
struct Node *next;
};
Node *Creat()
{
Node *head,*p,*s;
head=(Node *)malloc(sizeof(Node));
p=head;
int n;
while(scanf("%d",&n)&&n)
{
s=(Node *)malloc(sizeof(Node));
s->data=n;
p->next=s;
p=s;
}
head=head->next;
p->next=NULL;
return head;
}
int Length(Node *head)
{
Node *p;
p=head;
int len=0;
while(NULL!=p)
{
p=p->next;
++len;
}
return len;
}
Node *Del(Node *&head,int num)
{
Node *p1,*p2;
p1=head;
while (num!=p1->data&&NULL!=p1->next)
{
p2=p1;
p1=p1->next;
}
if (num==p1->data)
{
if(p1==head)
{
head=p1->next;
free(p1);
}
else
{
p2->next=p1->next;
}
}
else
{
printf("%d cound not be found\n",num);
}
return head;
}
//插入头结点时,这是头结点改变了,所以这里要注意 就是所谓的引用与指针的区别
//函数体内 要修改一个变量的值该怎么做呢???不懂的自己百度
//这种情况老是容易忘记,请仔细注意了
Node *Insert(Node *&head,int num)
{
Node *p0,*p1,*p2;
p1=head;
p0=(Node*)malloc(sizeof(Node));
p0->data=num;
while (num>p1->data&&NULL!=p1->next)
{
p2=p1;
p1=p1->next;
}
if (num<=p1->data)
{
if (head==p1)
{
p0->next=head;
head=p0;
}
else
{
p2->next=p0;
p0->next=p1;
}
}
else
{
p1->next=p0;
p1->next->next=NULL;
}
return head;
}
Node *Sorted(Node *head)//简单的冒泡排序法
{
Node *p1,*p2;
p1=head;
p2=head;
int len=Length(head);
if (NULL==head||NULL==head->next)
return head;
for (int i=0; i<len; ++i)
{
p2=p1;
for (int j=i+1; j<len; ++j)
{
if(p1->data>p2->next->data)
{
p1->data^=p2->next->data;
p2->next->data^=p1->data;
p1->data^=p2->next->data;
}
p2=p2->next;
}
p1=p1->next;
}
return head;
}
Node *Reverse(Node *&head)
{
Node *p1,*p2,*p3;
p1=head;
if (NULL==head||NULL==head->next)
{
return head;
}
p2=p1->next;
while (p2)
{
p3=p2->next;
p2->next=p1;
p1=p2;
p2=p3;
}
head->next=NULL;
head=p1;
return head;
}
void Print(Node *head)
{
Node *p;
p=head;&nbs
补充:软件开发 , C++ ,