使用需明白,我提供的代码并不是给大家抄的,而是给大家一个思路来做题。如果你一开始就会意错了的话(只会抄的话),你的水平永远不会得到大的飞跃,如果你还对自己有严格的要求的话,就可以略微参考一下。代码有什么不好或不对的地方欢迎提出。有什么不懂的地方可以尽可能的提出来,我会做解答。可以当面叫我解答(提供给我小组的成员)。(天啊!说的好官方啊!快被自己唬住了!)
数据结构实验之链表一:顺序建立链表
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include <iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};
int main()
{
int i, n, m;
struct node *head, *p, *q;
scanf ( "%d", &n );
head = NULL;
for ( i = 1;i <= n; i++ )
{
scanf ( "%d", &m );
p = (struct node *)malloc(sizeof(struct node));
p->data = m;
p->next = NULL;
if(head == NULL)
head = p;
else
q->next = p;
q = p;
}
p = head;
while(p != NULL)
{
printf ( p->next == NULL ? "%d\n" : "%d " , p->data );
p = p->next;
}
return 0;
}
数据结构实验之链表二:逆序建立链表
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include <iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};
int main()
{
int i, n, m;
struct node *head, *p;
scanf ( "%d", &n );
head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
for ( i = 1;i <= n; i++ )
{
scanf ( "%d", &m );
p = (struct node *)malloc(sizeof(struct node));
p->data = m;
p->next = head->next;
head->next = p;
}
p = head->next;
while(p != NULL)
{
printf ( p->next == NULL ? "%d\n" : "%d " , p->data );
p = p->next;
}
return 0;
}
数据结构实验之链表三:链表的逆置
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include <iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};
int main()
{
int m;
struct node *head, *p;
head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
while(~scanf ( "%d", &m ) && m != -1)
{
p = (struct node *)malloc(sizeof(struct node));
p->data = m;
p->next = head->next;
head->next = p;
}
p = head->next;
while(p != NULL)
{
printf ( p->next == NULL ? "%d\n" : "%d " , p->data );
p = p->next;
}
return 0;
}
数据结构实验之链表四:有序链表的归并
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
struct node
{
int data;
struct node *next;
};
struct node *create(int n)
{
int i;
struct node *head, *tail, *p;
head = (struct node*)malloc(sizeof(struct node));
head->next = NULL;
tail = head;
for ( i = 0;i <= n; i++ )
{
p = (node *)malloc(sizeof(node));
if ( i != n )
scanf ( "%d", &p->data );
else
p->data = (1 << 30 );
p->next = NULL;
tail->next = p;
tail = p;
}
// p->next->data = (1 << 30);
// tail = p->next;
// tail->next = NULL;
return head;
}
struct node *sumList(node *head1, node *head2, int num)
{
node *p1, *p2, *p, *head, *tail;
p1 = head1->next;
p2 = head2->next;
head = (struct node*)malloc(sizeof(struct node));
head->next = NULL;
tail = head;
while ( num-- )
{
if (p1->data < p2->data)
{
p = (struct node*)malloc(sizeof(struct node));
p->data = p1->data;
p->next = NULL;
tail->next = p;
tail = p;
p1 = p1->next;
}
else
{
p = (struct node*)malloc(sizeof(struct node));
p->data = p2->data;
p->next = NULL;
tail->next = p;
tail = p;
p2 = p2->next;
}
}
return head;
}
void display(struct node *sum)
{
node *p;
p = sum->next;
while ( p != NULL )
{
printf ( p->next != NULL ? "%d " : "%d\n", p->data );
p = p->next;
}
}
int main()
{
int n, m;
struct node *head1, *head2, *sum;
scanf ( "%d %d", &n, &m );
head1 = create(n);
head2 = create(m);
sum = sumList(head1, head2, n+m);
display(sum);
}
数据结构实验之链表五:单链表的拆分
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
struct node
{
int data;
struct node *next;
};
int num1, num2;
struct node *head1, *head2;
struct node *create(int n)
{
int i;
struct node *head, *tail, *p;
head = (struct node*)malloc(sizeof(struct node));
head->next = NULL;
tail = head;
for ( i = 0;i < n; i++ )
{
p = (node *)malloc(sizeof(node));
scanf ( "%d", &p->data );
p->next = NULL;
tail->next = p;
tail = p;
}
return head;
}
void List(node *head, int n)
{
node *tail1, *tail2, *p, *q;
head1 = (struct node*)malloc(sizeof(struct node));
head1->next = NULL;
tail1 = head1;
head2 = (struct node*)malloc(sizeof(struct node));
head2->next = NULL;
tail2 = head2;
p = head->next;
while ( n-- )
{
if (p->data%2 == 0 )
{
num1++;
q = (struct node*)malloc(sizeof(struct node));
q->data = p->data;
q->next = NULL;
tail1->next = q;
tail1 = q;
p = p->next;
}
else
{
num2++;
q = (struct node*)malloc(sizeof(struct node));
q->data = p->data;
q->next = NULL;
tail2->next = q;
tail2 = q;
p = p->next;
}
}
}
void display()
{
node *p;
p = head1->next;
printf ( "%d %d\n", num1, num2 );
while ( p != NULL )
{
printf ( p->next != NULL ? "%d " : "%d\n", p->data );
p = p->next;
}
p = head2->next;
while ( p != NULL )
{
printf ( p->next != NULL ? "%d " : "%d\n", p->data );
p = p->next;
}
}
int main()
{
int n;
num1 = num2 = 0;
struct node *head;
scanf ( "%d", &n );
head = create(n);
List(head, n);
display();
}
数据结构实验之链表六:有序链表的建立
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
struct node
{
int data;
struct node *next;
};
int main()
{
int n, i, j;
struct node *head, *tail, *p, *q;
head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
tail = head;
scanf ( "%d", &n );
for ( i = 0;i < n; i++ )
{
p = (struct node *)malloc(sizeof(struct node));
scanf ( "%d", &p->data );
p->next = NULL;
tail->next = p;
tail = p;
}
for ( p = head->next;p != NULL;p = p->next )
{
for ( q = head->next;q != p;q = q->next )
{
if ( p->data < q->data )
{
j = p->data;
p->data = q->data;
q->data = j;
}
}
}
p = head->next;
while( p )
{
printf ( p->next != NULL ? "%d " : "%d\n" , p->data );
p = p->next;
}
}
数据结构实验之链表七:单链表中重复元素的删除
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
struct node
{
int data;
struct node *next;
};
int main()
{
int n, i, j;
struct node *head, *tail, *p, *q, *r;
head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
tail = head;
scanf ( "%d", &n );
for ( i = 0;i < n; i++ )
{
p = (struct node *)malloc(sizeof(struct node));
scanf ( "%d", &p->data );
p->next = NULL;
p->next = head->next;
head->next = p;
}
printf ( "%d\n", n );
p = head->next;
while( p )
{
printf ( p->next != NULL ? "%d " : "%d\n" , p->data );
p = p->next;
}
p = head->next;
while ( p->next != NULL )
{
q = p;
while ( q->next != NULL )
{
if (p->data == q->next->data)
{
r = q->next;
q->next = r->next;
free(r);
n--;
}
else
q = q->next;
}
p = p->next;
}
printf ( "%d\n", n );
p = head->next;
while( p )
{
printf ( p->next != NULL ? "%d " : "%d\n" , p->data );
p = p->next;
}
}
数据结构实验之链表八:Farey序列
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
struct node
{
int erator;
int minator;
struct node *next;
};
node *head = new node;
void First_inti()
{
node *tail;
tail = head;
for ( int i = 0;i < 2; i++ )
{
node *p = new node;
p->erator = i;
p->minator = 1;
tail->next = p;
tail = p;
}
}
void create(int i)
{
node *p, *q;
p = head->next;
while ( p->next )
{
q = p->next;
if ( p->minator+q->minator <= i )
{
node *tail = new node;
tail->minator = p->minator+q->minator;
tail->erator = p->erator+q->erator;
p->next = tail;
tail->next = q;
}
p = p->next;
}
}
void display(struct node *head)
{
int num = 0;
node *p;
p = head->next;
while ( p )
{
num++;
printf ( num != 10 ? "%d/%d\t" : "%d/%d", p->erator, p->minator );
p = p->next;
if ( num == 10 )
{
printf ( "\n" ) ;
num = 0;
}
}
}
int main()
{
int n;
First_inti();
scanf ( "%d", &n );
for ( int i = 2;i <= n; i++ )
{
create( i );
}
display( head );
return 0;
}
数据结构实验之链表九:双向链表
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
struct node
{
int num;
struct node *next, *pro;
};
int main()
{
int n, m;
scanf ( "%d %d", &n, &m );
node *tail, *q, *p;
node *head = new node;
tail = head;
while ( n-- )
{
p = new node;
scanf ( "%d", &p->num );
p->next = NULL;
p->pro = NULL;
tail->next = p;
p->pro = tail;
tail = p;
}
while ( m-- )
{
int h;
scanf ( "%d", &h );
if ( head->next->num == h )
{
printf ( "%d\n", head->next->next->num );
}
else if ( tail->num == h )
{
printf ( "%d\n", tail->pro->num );
}
else
{
q = head->next;
while ( q->next )
{
if ( q->num == h )
{
printf ( "%d %d\n", q->pro->num, q->next->num );
break;
}
q = q->next;
}
}
}
}
代码菜鸟,如有错误,请多包涵!!!
如有帮助记得支持我一下,谢谢!!!