65,211
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define LEN sizeof(struct Nodeple)
typedef struct Nodeple
{
int i;
struct Nodeple *next;
}Node;
Node *creat(void)
{
int n=0;
Node *p1=(Node*)malloc(LEN);
Node *p2=(Node*)malloc(LEN);
Node *head;
printf("Please input the node:\n");
scanf("%d",&p1->i);
while(p1->i!=0)
{
n++;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(Node*)malloc(LEN);
scanf("%d",&p1->i);
}
p2->next=NULL;
free(p1);
p1=NULL;
return head;
}
void Print(Node* head)
{
Node * p;
p=head;
if(head!=NULL)
do
{
printf("%d-->",p->i);
p=p->next;
}while(p!=NULL);
printf("\n");
}
Node* mergAction(Node* head1,Node *head2)//合并
{
Node *p=NULL;
if(head1==NULL&&head2==NULL)
return p;
else if(head1==NULL)
return head2;
else if(head2==NULL)
return head1;
else
{
if(head1->i <= head2->i)
{
p = head1;
if(head1->i < head2->i)
{
p->next = mergAction(head1->next,head2);
}
else
{
p->next = mergAction(head1->next,head2->next);
}
}
else
{
p=head2;
p->next=mergAction(head1,head2->next);
}
return p;
}
}
int main()
{
Node *p=NULL;
Node *head1=creat();
Node *head2=creat();
p=mergAction(head1,head2);
Print(p);
return 0;
}
Please input the node:
1 3 4 5 0
Please input the node:
2 4 5 7 0
1-->2-->3-->4-->5-->7-->
Press any key to continue
#include <iostream>
using namespace std;
struct node{
int data;
struct node *next;
node(int d=0,node *nx=NULL):data(d),next(nx){}
};
struct link{
link():head(NULL),tail(NULL){}
~link(){
node *t=head;
while(t){
node *p=t;
t=t->next;
delete p;
}
head=tail=NULL;
}
void empty(){
if(tail==NULL){
cout <<"this is a empty list" <<endl;
}
else
cout <<"this is not empty list" <<endl;
}
//在尾部插入元素
void push_back(int v){
if(!tail)
head=tail=new node(v);
else{
tail->next=new node(v);
tail=tail->next;
}
}
//在inder位置插入某个元素
void insert(int index,int val){
node *t=head;
for(int i=0;i!=index-1;i++)
t=t->next;
node *s=new node(val);
s->next=t->next;
t->next=s;
}
//删除一个元素
void erase(int index){
node *t=head,*p;
for(int i=0;i!=index;i++){
t=t->next;
}
if(t==NULL) return;
else{
p=t->next;
t->next=p->next;
delete p;
}
}
//删除一段元素
void erase(int beg,int end){
node *s=head;
for(int i=0;i!=beg;i++){
s=s->next;
}
node *t=head;
for(int j=0;j!=end;j++){
t=t->next;
}
if(t==NULL||s==NULL) return;
else{
s->next=t->next;
}
}
//删除头元素(出队)
void pop_front(){
node *t=head;
if(head==tail)
head=tail=NULL;
else
head=head->next;
delete t;
}
//获得首元素
int get_front(){
return head->data;
}
//获得尾元素
int get_back(){
return tail->data;
}
//查找指定地址元素
int get_value(int index){
int i=0;
node *t=head;
while(i <index&&t!=NULL){
t=t->next;
i++;
}
if(t==NULL) return 0;
else return t->data;
}
//按元素查找第一次出现的地址
int get_idex(int val){
node *t=head;
int i=0;
while(t!=NULL&&t->data!=val){
i++;
t=t->next;
}
if(t==NULL) return 0;
return i;
}
bool has(int data)
{
node* cur = head;
while (cur)
{
if (cur->data == data)
return true;
cur = cur->next;
}
return false;
}
void display(){
node* cur = head;
while (cur) {
cout <<cur->data;
cur = cur->next;
if (cur)
cout <<" -> ";
}
cout <<endl;
}
node *head;
node *tail;
};
// 交
link* intersection(link* link1, link* link2)
{
link* new_link = new link;
node* cur = link1->head;
while (cur)
{
if (link2->has(cur->data))
new_link->push_back(cur->data);
cur = cur->next;
}
return new_link;
}
// 并
link* combine(link* link1, link* link2)
{
link* new_link = new link;
node* cur = link1->head;
while (cur)
{
new_link->push_back(cur->data);
cur = cur->next;
}
cur = link2->head;
while (cur)
{
new_link->push_back(cur->data);
cur = cur->next;
}
return new_link;
}
// 差
link* difference(link* link1, link* link2)
{
link* new_link = new link;
node* cur = link1->head;
while (cur)
{
if (!link2->has(cur->data))
new_link->push_back(cur->data);
cur = cur->next;
}
return new_link;
}
int main()
{
link* link1 = new link;
link* link2 = new link;
for (int i = 0; i < 10; ++i)
{
link1->push_back(i);
link2->push_back(i + 5);
}
cout<<"link 1 :";
link1->display();
cout<<"link 2 :";
link2->display();
link* inter = intersection(link1, link2);
cout<<"交 :";
inter->display();
link* comb = combine(link1, link2);
cout<<"并 :";
comb->display();
link* diff = difference(link1, link2);
cout<<"差 :";
diff->display();
delete link1;
delete link2;
delete comb;
delete inter;
delete diff;
return 0;
}
------------
link 1 :0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9
link 2 :5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> 13 -> 14
交 :5 -> 6 -> 7 -> 8 -> 9
并 :0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 5 -> 6 -> 7 -> 8 -> 9 -> 1
0 -> 11 -> 12 -> 13 -> 14
差 :0 -> 1 -> 2 -> 3 -> 4
请按任意键继续. . .