链表、查找、排序

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

//顺序查找和折?半ã?查¨¦找¨°
//折半查找两种?都?可¨¦以°?
#include<stdarg.h>
#include<stdio.h>
#include<conio.h>
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
int Seqsearch(int a[],int n, int key)
{
int i = 0;
while(i < n)
{
if(a[i] == key)
{
return (i+1);
break;
}
i++;
}
return 0;
}
/*int Binarysearch(int a[], int n, int key)
{
int low,high,mid;
low = 0;
high = n-1;
mid =(int)((low + high)/2);
while(a[mid] != key)
{
if(key > a[mid])
{
low = mid +1;
mid =(int)((low + high)/2);
}
else
{
high = mid -1;
mid =(int)((low + high)/2);
}
if(low == high && n != 1)
{
// return 0;
break;
}
}
if(a[mid] == key)
{
return(mid +1);
}
else return 0;
}*/
int Binarysearch(int a[], int n, int key) {
int low,high,mid;
low = 0;
high = n-1;
mid =(int)((low + high)/2);
while(low <= high)
{
if(key > a[mid])
{
low = mid +1;
mid =(int)((low + high)/2);
}
else if(key < a[mid])
{
high = mid -1;
mid =(int)((low + high)/2);
}
else return(mid + 1);
}
return 0;
}
int main()
{
while(true)
{
// int y[20];
int data;
int n,i;
int key;
int flag;
cout<<"pls input the array's length:"<<endl;
cin>>n;
int *y = new int[n];
for(i = 0;i < n; i++)
{
cout<<"pls input the"<<i+1<<"data :"<<endl;
cin>>data;
y[i] = data;
}
for(i = 0;i<n;i++)
{
cout<<y[i]<<"\t";
}
cout<<endl;
cout<<"pls input the array's keydata:"<<endl;
cin>>key;
// flag = Seqsearch(y,n,key);
flag = Binarysearch(y,n,key);
cout<<flag<<endl;
system("pause");
}
}
//单Ì£¤向¨°链¢¡ä表À¨ª
#include<iostream>
#include<string>
#include<stdio.h>
#include<conio.h>
#include<stdarg.h>
using namespace std;
typedef struct stu
{
int data;
struct stu *next;
}node;
node *creat()
{
node *head,*p,*s;
int x,cycle = 1;
head = (node *)malloc(sizeof(node));
p = head;
while(cycle)
{
cout<<"pls input the data"<<endl;
cin>>x;
if(x != 0)
{
s = (node *)malloc(sizeof(node));
s->data = x;
p->next = s;
p = s;
}
else
{
cycle = 0;
}
}
head = head->next;
p->next = NULL;
//cout<<head->data<<endl;
return(head);
}
void print(node *text)
{
node *q;
q = text;
while(q != NULL)
{
cout<<q->data<<endl;
q = q->next;
}
}
int length(node *text)
{
node *p;
int i = 0;
p = text;
while(p->next != NULL)
{
p = p->next;
i++;
}
return i;
}
node *del(node *text, int num)
{
int i = 0;
int j = 0;
node *p1,*p2;
bool del;
del = false;
p1 = text;
while(p1 && p1->next)
{
if(text->data == num)
{
text = p1->next;
j = 1;
}
if(del == false)
{
p2 = p1;
}
p1 = p1->next;
del = false;
if(p1->data == num)
{
p2->next = p1->next;
// free(p1);
i++;
del = true;
}
}
if(i == 0)
{
cout<<"链¢¡ä表À¨ª里¤?面?没?有®D这a个?数ºy据Y"<<endl;
}
else
{
cout<<"已°?删¦?除y的Ì?"<<num<<"的Ì?个?数ºy是º?"<<i+j<<endl;
}
return(text);
}
node *insert(node *text,int num)
{
node *p0,*p1,*p2;
p1 = text;
p0 = (node *)malloc(sizeof(node));
p0->data = num;
while(p1->data != num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if(p1->next == NULL)
{
p1->next = p0;
p0->next = NULL;
}
else if(text->data == num)
{
p0->next = text;
text = p0;
}
else
{
p2->next = p0;
p0->next = p1;
}
return (text);
}
node *sort(node *text)
{
node *p;
int n;
int temp;
n = length(text);
/* while(p->next == NULL || p == NULL) {
p = text;
}*/
for(int i = 0; i < n;i++)
{
p = text;
for(int j = 1;j < n - i;j++)
{
if(p->data>p->next->data)
{
temp = p->data;
p->data = p->next->data;
p->next->data = temp;
}
p = p->next;
}
}
return text;
}
node *reverse(node *text)
{
node *p1,*p2,*p3;
if(text == NULL || text->next == NULL)
{
return text;
}
p1 = text;
p2 = p1->next;
// text->next = NULL;
while(p2)
{
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
// if(p3->next != NULL)
// {
// p3 = p3->next;
// }
}
//p2->next = p1;
text->next = NULL;
text = p1;
return text;
}
node *merge(node *a, node *b)
{
node *c=NULL;
node *cur=NULL;
node *head=NULL;
//c = (node *)malloc(sizeof(node));
while(a && b)
{
if(a->data < b->data)
{
c = a;
a = a->next;
}
else
{
c = b;
b = b->next;
}
if(head == NULL)
{
cur = c;
head = cur;
}
else
{
cur->next = c;
cur = c;
}
}
if(a == NULL)
{
c = b;
}
else
{
c = a;
}
while(c)
{
cur->next = c;
c = c->next;
}
//head = cur;
cur->next = NULL;
return head;
}
int main()
{
//cout<<head->data<<endl;
while(true)
{
int i,num;
node *head;
node *a,*b;
/* head = creat();
cout<<"指?针?的Ì?数ºy据Y是º?:
êo"<<endl;
print(head);
i = length(head);
cout<<"指?针?的Ì?长¡è度¨¨是º?:
êo"<<i<<endl;
cout<<"请?输º?入¨?欲®?删¦?除y的Ì?数ºy据Y"<<endl;
cin>>num;
head = del(head,num);
cout<<"请?输º?入¨?欲®?插?入¨?的Ì?数ºy据Y"<<endl;
cin>>num;
head = insert(head,num);
i = length(head);
cout<<"指?针?的Ì?长¡è度¨¨是º?:
êo"<<i<<endl;
head = sort(head);*/
a = creat();
cout<<"指?针?的Ì?数ºy据Y是º?:
êo"<<endl;
print(a);
b = creat();
cout<<"指?针?的Ì?数ºy据Y是º?:êo"<<endl;
print(b);
head = merge(a,b);
cout<<"指?针?的Ì?数ºy据Y是º?:êo"<<endl;
print(head);
system("pause");
}
}
//双?向¨°链¢¡ä表À¨ª的Ì?相¨¤关?知a识º?
#include<iostream>
#include<string>
#include<stdio.h>
#include<conio.h>
#include<stdarg.h>
using namespace std;
typedef struct student
{
int data;
struct student *next;
struct student *pre;
}dnode;
dnode *creat()
{
dnode *head,*p,*s;
head = (dnode *)malloc(sizeof(dnode));
int x,cycle;
x =1;
cycle = 1;
p = head;
while(x)
{
cout<<"pls input the data"<<endl;
cin>>x;
if(x != 0)
{
s = (dnode
*)malloc(sizeof(dnode));
s->data = x;
p->next = s;
s->pre = p;
p = s;
}
else
{
cycle = 0;
}
}
head = head->next;
p->next = NULL;
// head->pre;
return(head);
}
void print(dnode *head)
{
dnode *p;
p = head;
while(p != NULL)
{
cout<<p->data<<endl;
p = p->next;
}
}
dnode *insert(dnode *head,int num)
{
dnode *p,*s;
s =(dnode *)malloc(sizeof(dnode));
s->data = num;
p = head;
while(s->data > p->data && p->next != NULL)
{
p = p->next;
}
if(s->data <= p->data)
{
if(head == p)
{
s->next = p;
p->pre = s;
s->pre = NULL;
head = s;
}
else
{
p->pre->next = s;
p->pre = s;
s->next = p;
s->pre = p->pre;
}
}
else
{
p->next = s;
s->pre = p;
s->next = NULL;
}
return (head);
}
dnode *del(dnode *head, int num)
{
dnode *p;
p = head;
//for(p = head;p->next != NULL;p = p->next)
while(p->next)
{
/* if(p->data == num )
{
if(p == head)
{
p->next->pre = NULL;
head = head->next;
p = p->next;
}
else if(p->next != NULL)
{
p->pre->next = p->next;
p->next->pre = p->pre;
p = p->next;
}
else
{
p->pre->next = NULL;
free(p);
}
}
else if(p->data != num &&
p->next !=NULL)
{
p = p->next;
}*/
p = p->next;
if(head->data == num)
{
head = head->next;
head->pre = NULL;
}
if(p->data == num && p->next != NULL)
{
p->next->pre = p->pre;
if(p != head)
{
p->pre->next = p->next;
}
}
if(p->data == num && p->next == NULL)
{
p->pre->next = NULL;
}
}
// while(p && p->next)
// {
// if(p->data)
// }
return(head);
}
int main()
{
while(true)
{
dnode *head;
int num;
head = creat();
cout<<"the created list is:"<<endl;
print(head);
// cout<<"pls input the insert num:"<<endl;
// cin>>num;
// head = insert(head,num);
// print(head);
cout<<"pls input the delete num:"<<endl;
cin>>num;
head = del(head,num);
print(head);
system("pause");
}
}
//循-环¡¤链¢¡ä表À¨ª
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<string>
#include<math.h>
using namespace std;
typedef struct node
{
int data;
struct node *link;
}node;
node *creat(int m,int n)
{
int k = n;
int i=1;
node *head,*p,*s;
head = (node *)malloc(sizeof(node));
p = head;
while(i!=k+1)
{
s = (node *)malloc(sizeof(node));
s->data = i;
p->link = s;
p = s;
i++;
}
head = head->link;
p->link = head;
return head;
}
void cycle_list(int m,int n)
{
node *head,*p,*s;
int k,j,o;
bool aa = false;
int i = 0;
k = n;
j = m;
head = creat(j,k);
p = head;
while(p->link != p)
{
i++;
if(aa == false)
{
s = p;
p = p->link;
}
aa = false;
if(i == j)
{
cout<<p->data<<endl;
o=p->data;
o=s->data;
s->link = p->link;
o=s->link->data;
p = s->link;
o=p->data;
i = 0;
aa = true;
}
}
}
void print(node *head)
{
node *p;
p = head;
bool flag;
flag = true;
while(flag == true)
{
cout<<p->data<<endl;
p = p->link;
if(p == head)
flag = false;
}
}
int main()
{
// node *head;
int m,n;
m = 2;
n = 6;
// head = creat(m,n);
// print(head);
cycle_list(m,n);
system("pause");
}
链表逆置
#include <iostream>
#include <string>
#include <stdio.h>
#include <conio.h>
#include <stdarg.h>
using namespace std;
typedef struct stu
{
int data;
struct stu *next;
}node;
node *creat()
{
node *head,*p,*s;
int x,cycle = 1;
head = (node *)malloc(sizeof(node));
p = head;
while(cycle)
{
cout<<"pls input the data"<<endl;
cin>>x;
if(x != 0)
{
s = (node *)malloc(sizeof(node));
s->data = x;
p->next = s;
p = s;
}
else
{
cycle = 0;
}
}
head = head->next;
p->next = NULL;
//cout<<head->data<<endl;
return(head);
}
void print(node *text)
{
node *q;
q = text;
while(q != NULL)
{
cout<<q->data<<endl;
q = q->next;
}
}
int length(node *text)
{
node *p;
int i = 0;
p = text;
while(p->next != NULL)
{
p = p->next;
i++;
}
return i+1;
}
node *rev(node *head)
{
int nlen,k;
node *r,*p,*s;
bool aa = false;
nlen = length(head);
// cout<<nlen<<endl;
k = 1;
p = head;
s = p;
while(k != nlen)
{
if(aa == false)
{
r = p;
p = p->next;
s = p->next;
r->next = NULL;
}
if(k == nlen-1)
{
p->next = r;
//p = s;
}
else
{
p->next = r;
r = p;
p = s;
if(s != NULL)
s = s->next;
}
aa = true;
k++;
}
head = p;
return head;
}
int main()
{
//cout<<head->data<<endl;
while(true)
{
int i,num;
node *head;
node *a,*b;
/* head = creat();
cout<<"指针的数据是:"<<endl;
print(head);
i = length(head);
cout<<"指针的长度是:"<<i<<endl;
cout<<"请输入欲删除的数据"<<endl;
cin>>num;
head = del(head,num);
cout<<"请输入欲插入的数据"<<endl;
cin>>num;
head = insert(head,num);
i = length(head);
cout<<"指针的长度是:"<<i<<endl;
head = sort(head);
a = creat();
cout<<"指针的数据是:"<<endl;
print(a);
b = creat();
cout<<"指针的数据是:"<<endl;
print(b);
head = merge(a,b);
cout<<"指针的数据是:"<<endl;*/
a = creat();
cout<<"指针的数据是:"<<endl;
print(a);
b = rev(a);
cout<<"指针的数据是:"<<endl;
print(b);
// print(head);
system("pause");
}
}
约瑟夫环
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <string>
#include <math.h>
using namespace std;
typedef struct node
{
int data;
struct node *link;
}node;
node *creat(int m,int n)
{
int k = n;
int i=1;
node *head,*p,*s;
head = (node *)malloc(sizeof(node));
p = head;
while(i!=k+1)
{
s = (node *)malloc(sizeof(node));
s->data = i;
p->link = s;
p = s;
i++;
}
head = head->link;
p->link = head;
return head;
}
void cycle_list(int m,int n)
{
node *head,*p,*s;
int k,j,o;
bool aa = false;
int i = 0;
k = n;
j = m;
head = creat(j,k);
p = head;
while(p->link != p)
{
i++;
if(aa == false)
{
s = p;
p = p->link;
}
aa = false;
if(i == j)
{
cout<<p->data<<endl;
o=p->data;
o=s->data;
s->link = p->link;
o=s->link->data;
p = s->link;
o=p->data;
i = 0;
aa = true;
}
}
} void print(node *head)
{
node *p;
p = head;
bool flag;
flag = true;
while(flag == true)
{
cout<<p->data<<endl;
p = p->link;
if(p == head)
flag = false;
}
}
int main()
{
// node *head;
int m,n;
m = 2;
n = 6;
// head = creat(m,n);
// print(head);
cycle_list(m,n);
system("pause");
}。

相关文档
最新文档