考研初试算法题合集1线性表与链表
算法书上的代码都是伪代码,为了便于记忆,参考着资料实现了一遍,一切为了理解。
#include <iostream>
#include <stdio.h>
#include<stdlib.h>
#include<mm_malloc.h>
using namespace std;
typedef int Status;
#define ElemType int
#define error 0
#define OK 1
#define LISTINCREAMENT 10
#define OVERFLOW -2
typedef struct sqlist{
ElemType *elem;
int listsize;
int length;
}Sqlist;
Status ListInsert_Sq(Sqlist &L,int i,ElemType x)
{
ElemType *newbase,*p,*q;
if(i<1||i>L.length+1) return error;
if(L.length>=L.listsize)
{
newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREAMENT)*sizeof(ElemType));
if(!newbase)exit(OVERFLOW);
L.elem=newbase;
L.listsize+=LISTINCREAMENT;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p)
*(p+1)=*p;
*q=x;
L.length++;
return OK;
}
Status ListDelete_sq(Sqlist &L,int i,ElemType &e)
{
ElemType *p,*q;
if((i<1)||(i>L.length))return error;
p=&(L.elem[i]);
e=*p;
q=L.elem+L.length-1;
for(++p;p<=q;++p)
*(p-1)=*p;
--L.length;
}
void MergeList_Sq(Sqlist La,Sqlist Lb,Sqlist &Lc)
{
ElemType *pa,*pb,*pc,*pa_last,*pb_last;
pa=La.elem;
pb=Lb.elem;
Lc.listsize=Lc.length=La.length+Lb.length;
pc=Lc.elem=(ElemType*)malloc(Lc.listsize*sizeof(ElemType));
if(!Lc.elem)exit(OVERFLOW);
pa_last=La.elem+La.length-1;
pb_last=Lb.elem+Lb.length-1;
while(pa<=pa_last&&pb<=pb_last)
{
if(*pa<*pb)
*pc++=*pa++;
else
*pc++=*pb++;
}
while(pa<=pa_last)*pc++=*pa++;
while(pb<=pb_last)*pc++=*pb++;
}
typedef struct Lnode{
ElemType data;
struct Lnode *next;
}Lnode,*LinkList;
Status GetElem_L(LinkList L,int i,ElemType &e)
{
LinkList p;
p=L->next;int k=1;
while(p&&k<i)
{
p=p->next;
++k;
}
if(!p||k>i)
return error;
e=p->data;
return OK;
}
Status ListInsert(LinkList &L,int i,ElemType e)
{
Lnode *p,*s;
p=L; int k=0;
while(p&&k<i-1)
{
p=p->next;
++k;
}
if(!p||k>i-1)
return error;
if(!(s=(LinkList)malloc(sizeof(Lnode))));
return OVERFLOW;
s->data=e;
s->next=p->next;
p->next=s;
return OK;
}
Status ListDelete_L(LinkList &L,int i,ElemType &e)
{
Lnode *p,*q;
p=L; int j=0;
while(p->next&&j<i-1)
{
p=p->next;
j++;
}
if(!(p->next)||j>i-1)
return error;
q=p->next;
p->next=q->next;
e=q->data;free(q);
return OK;
}
void Create_L(LinkList &L,int n)
{
Lnode *p;
L=(LinkList)malloc(sizeof(Lnode));
L->next=NULL;
for(int i=n;i>0;--i)
{
p=(LinkList)malloc(sizeof(Lnode));
scanf("%d",&p->data);
p->next=L->next;L->next=p;
}
}
LinkList CreateL1()
{
LinkList h;
h = (LinkList)malloc(sizeof(Lnode));
LinkList p,r;
r=h;
int n;
while(scanf("%d",&n)!=EOF)
{
p=(Lnode*)malloc(sizeof(Lnode));
p->data=n;
r->next=p;
r=p;
}
r->next=NULL;
return h;
}
void MergeList_L(LinkList &La,LinkList &Lb,LinkList &Lc)
{
Lnode *pa,*pb,*pc;
pa=La->next;
pb=Lb->next;
while(pa&&pb){
if(pa->data<=pb->data){
pc->next=pa;
pc=pa;
pa=pa->next;
}
else {
pc->next=pb;
pc=pb;
pb=pb->next;
}
}
pc->next=pa?pa:pb;
free(Lb);
}
#include<iostream>
using namespace std;
const int N=20;
typedef struct node{
int id;
struct node *next;
struct node *pre;
}Node,*pNode;
pNode RingConstruct(int n)
{
int i;
pNode h,p,q;
h=(pNode)malloc(sizeof(Node));
h->id=1;
p=h;
for(i=2;i<=n;i++)
{
q=(pNode)malloc(sizeof(Node));
q->id=i;
p->next=q;
q->pre=p;
p=p->next;
}
p->next=h;
h->pre=p;
return h;
}
int boundMachine(int order){
int boundList[4]={3,5,7,13};
return boundList[(order-1)%4];
}
pNode count(pNode first,int bound){
pNode q;
q=first;
for(int i=2;i<=bound;i++){
q=q->next;
}
return q;
}
pNode removeNode(pNode currentNode)
{
pNode first = currentNode->next;
first->pre=currentNode->pre;
cout<<currentNode->id<<"";
free(currentNode);
return first;
}
int main(){
pNode first,toRemove;
int i;
first=RingConstruct(N);
for(int i=1;i<=N;i++){
toRemove=count(first, boundMachine(i));
first=removeNode(toRemove);
}
return 0;
}
|