3-1
#include <stdio.h>
void change(int* a,int n);
int main(){
int a[100];
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(int i=0;i<n;i++){
printf("%d ",a[i]);
}
change(a,n);
for(int i=0;i<n;i++){
printf("%d ",a[i]);
}
}
void change(int *a,int n){
int temp;
//int k=n;
for(int i=0;i<n/2;i++){
temp=a[i];
a[i]=a[n-1-i];
a[n-1-i]=temp;
//n--;
}
}
3-3
#include <stdio.h>
int Min(int*a,int n);
int main(){
int n;
scanf("%d",&n);
int a[100];
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
int position=Min(a,n);
printf("最小元素位置为:%d",position);
return 0;
}
int Min(int*a,int n){//时间复杂度为O(n)
int min=a[0];
int temp=0;
for(int i=0;i<n;i++){
if(a[i]<min){
min=a[i];
temp=i;
}
}
return temp+1;
}
3-8
#include <stdio.h>
#include <malloc.h>
struct Cell{
int data;
Cell* next;
};
Cell* Create();
Cell* Change(Cell* head);
void print(Cell*);
int main(){
Cell* a=Create();
print(a);
Cell* b=Change(a);
print(b);
}
Cell* Create(){
Cell *head,*tmp,*tail;
head=NULL;
tmp=NULL;
tail=NULL;
int n;
scanf("%d",&n);
while(n!=-1){
tmp=(Cell*)malloc(sizeof(struct Cell));
tmp->data=n;
if(head==NULL){
head=tmp;
}
else{
tail->next=tmp;
}
tmp->next=NULL;
tail=tmp;
scanf("%d",&n);
}
return head;
}
Cell* Change(Cell* head){
Cell* p;
p=head;
int n=1;
while(p->next!=NULL){
n++;//共有n个结点
p=p->next;//最后一个节点
}
Cell* tail=p;//尾结点
//p=head;
//printf("%d",n);
Cell* tmp[n];
tmp[0]=head;
for(int i=1;i<n;i++){//思路:先删除,后添加
tmp[i]=head->next;
head->next=tmp[i]->next;
tmp[i]->next=tmp[i-1];
//head=head->next;
}
return p;
}
void print(Cell* head){
Cell* p;
p=head;
//p=p->next;
while(p!=NULL){
printf("%d ",p->data);
p=p->next;
}
}
3-11
#include <stdio.h>
#include <malloc.h>
struct Cell{
int data;
Cell* next;
};
Cell* Create();
void AL(Cell* head,int mink,int maxk,int data);
void print(Cell* head);
int main(){
Cell *a=Create();
print(a);
AL(a,5,10,2);
print(a);
}
Cell* Create(){//创建链表
Cell *head,*tmp,*tail;
head=NULL;
tmp=NULL;
tail=NULL;
int n;
scanf("%d",&n);
while(n!=-1){
tmp=(Cell*)malloc(sizeof(struct Cell));
tmp->data=n;
if(head==NULL){
head=tmp;
}
else{
tail->next=tmp;
}
tmp->next=NULL;
tail=tmp;
scanf("%d",&n);
}
return head;
}
void AL(Cell* head,int mink,int maxk,int data){//时间复杂度O(a*b*b)?
int a=mink/data;//距离头节点a+1个
int b=(maxk-mink)/data;//删除b个结点
Cell* tmp[b];
Cell* p=head;
for(int i=0;i<a;i++){//到达要删除结点的前一个结点
p=p->next;
}
Cell* q=p;
for(int i=0;i<=b;i++){
q=q->next;
if(i<b){
tmp[i]=q;
//free(tmp[i]);
}
}
p->next=q;
for(int i=0;i<b;i++){
free(tmp[i]);
}
}
void print(Cell* head){
Cell* p;
p=head;
p=p->next;
while(p!=NULL){
printf("%d ",p->data);
p=p->next;
}
}
#include <stdio.h>
#include <malloc.h>
struct Cell{
int data;
Cell* next;
};
Cell* Create();
void AL(Cell* head,int mink,int maxk,int data);
void print(Cell* head);
int main(){
Cell *a=Create();
print(a);
AL(a,5,10,2);
print(a);
}
Cell* Create(){//创建链表
Cell *head,*tmp,*tail;
head=NULL;
tmp=NULL;
tail=NULL;
int n;
scanf("%d",&n);
while(n!=-1){
tmp=(Cell*)malloc(sizeof(struct Cell));
tmp->data=n;
if(head==NULL){
head=tmp;
}
else{
tail->next=tmp;
}
tmp->next=NULL;
tail=tmp;
scanf("%d",&n);
}
return head;
}
void AL(Cell* head,int mink,int maxk,int data){//时间复杂度O(a*b)?
int a=mink/data;//距离头节点a+1个
int b=(maxk-mink)/data;//删除b个结点
Cell* tmp[b];
Cell* p=head;
for(int i=0;i<a;i++){//到达要删除结点的前一个结点
p=p->next;
}
Cell* q=p;
q=q->next;
for(int i=0;i<b;i++){
tmp[i]=q;
p->next=q->next;
q=q->next;
free(tmp[i]);
}
//p->next=q;
}
void print(Cell* head){
Cell* p;
p=head;
p=p->next;
while(p!=NULL){
printf("%d ",p->data);
p=p->next;
}
}
3-13
#include <stdio.h>
#include <malloc.h>//双向链表没有哨兵
struct Cell{
int data;
int freq;
Cell* prior;//前指针
Cell* next;//后指针
};
Cell* Create();
Cell* Locate(Cell* head,int k);
void print(Cell*);
int main(){
Cell* a=Create();
Cell* b=Locate(a,3);
print(b);
}
Cell* Create(){
Cell *head,*tmp,*tail;
head=NULL;
tmp=NULL;
tail=NULL;
int n;
scanf("%d",&n);
while(n!=-1){
tmp=(Cell*)malloc(sizeof(struct Cell));
tmp->data=n;
tmp->freq=0;
if(head==NULL){
head=tmp;
tmp->prior=NULL;
}
else{
tmp->prior=tail;
tail->next=tmp;
}
tmp->next=NULL;
tail=tmp;
scanf("%d",&n);
}
return head;
}
Cell* Locate(Cell* head,int k){//元素值为k的结点的freq+1
Cell* p=head;
Cell* p1=head;
while(p->data!=k){//找到元素
p1=p;//p1为p前一个结点,便于进行删除
p=p->next;
}
p->freq=p->freq+1;//freq+1
//Cell* q=head;
if((p->freq)>=(head->freq)){//头结点情况
p1->next=p->next;
p->next->prior=p1;//删除操作,得到p这个结点
p->prior=NULL;
p->next=head;
head->prior=p;
head=p;
}
else{//中间插入
p1->next=p->next;
p->next->prior=p1;//删除操作,得到p这个结点
Cell* front=head;
Cell* behind=head->next;
while(1){//将p插在front与behind之间
if((front->freq)>(p->freq)&&(behind->freq)<=(p->freq)){
break;
}
else{
front=front->next;
behind=behind->next;
}
}
front->next=p;//插入操作
p->next=behind;
behind->prior=p;
p->prior=front;
}
}
void print(Cell* head){
//head=head->right;//去掉哨兵结点
while(head!=NULL){
printf("%d ",head->data);
head=head->next;
}
}