作者在 2013-09-25 21:32:43 发布以下内容
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct node
{
int date;
struct node * next;
} *PNODE;
PNODE ListInit();
void ListInsert(PNODE,int &,int &);
void ListTraverse(PNODE);
int main(void)
{
int a[]={
2,6,3,4,8,9,45
};
PNODE Address=ListInit();
for(int i=1;i<=7;i++)
{
ListInsert(Address,i,a[i-1]);
}
printf("L=");
ListTraverse(Address);
return 0;
}
PNODE ListInit()
{
PNODE f=(PNODE)malloc(sizeof(node));
f->next=NULL;
return f;
}
void ListInsert(PNODE L,int &pos,int &val)
{
int j=0;
PNODE u=L;
while(NULL!=u&&j<pos-1)
{
u=u->next;
j++;
}
if(NULL==u && j>pos)
exit(-1);
PNODE newnode=(PNODE)malloc(sizeof(node));
newnode->date=val;
newnode->next=u->next;
u->next=newnode;
}
void ListTraverse(PNODE L)
{
PNODE v=L->next;
while(NULL!=v)
{
printf("%d ",v->date);
v=v->next;
}
}
#include<stdio.h>
#include<malloc.h>
#define max 10
typedef struct node
{
int date;
struct node* next;
}* PNODE;
PNODE InitList();
void InsertList(PNODE,int &);
void ListTraverse(PNODE);
int main(void)
{
int i;
int a[max]={
2,65,98,78,63,1,23,74,49,65
};
PNODE address=InitList();
for(i=1;i<=max;i++)
{
InsertList(address,a[i-1]);
}
printf("L=");
ListTraverse(address);
printf("\n");
return 0;
}
PNODE InitList()
{
PNODE p=(PNODE)malloc(sizeof(node));
PNODE tail=(PNODE)malloc(sizeof(node));
p->next=tail;
tail->date=-1;
tail->next=NULL;
return p;
}
void InsertList(PNODE l,int &val)
{
PNODE q=(PNODE)malloc(sizeof(node));
q->date=val;
PNODE r=l->next;
q->next=r;
l->next=q;
}
void ListTraverse(PNODE l)
{
PNODE h=l->next;
while(NULL!=h&&-1!=h->date)
{
printf("%d ",h->date);
h=h->next;
}
}
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct node
{
int date;
struct node* next;
}*PNODE;
PNODE create_list();
void insert_list(PNODE,int ,int);
void transt_list(PNODE);
void sort_list(PNODE);
int changdu(PNODE );
int main(void)
{
PNODE list=NULL;
list=create_list();
printf("all is:\n");
transt_list(list);
printf("\n");
insert_list(list,3,200);
printf("all is:\n");
transt_list(list);
printf("\n");
//changdu(list);
sort_list(list);
printf("all is:\n");
transt_list(list);
printf("\n");
//delete_list(list);
return 0;
}
PNODE create_list()
{
int i=0;
int n;
printf("please input a number:");
scanf("%d",&n);
int a[n];
printf("\nplease input :\n");
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
PNODE head=(PNODE)malloc(sizeof(node));
PNODE tail=head;
tail->next=NULL;
while(i<n)
{
PNODE body=(PNODE)malloc(sizeof(node));
body->date=a[i];
tail->next=body;
body->next=NULL;
tail=body;
i++;
}
return head;
}
void insert_list(PNODE list,int pos,int n)
{
int i=0;
PNODE h=(PNODE)malloc(sizeof(node));
h->date=n;
PNODE v=list;
while(NULL!=v&&i<pos-1)
{
v=v->next;
i++;
}
h->next=v->next;
v->next=h;
}
void transt_list(PNODE p)
{
PNODE v=p->next;
while(NULL!=v)
{
printf("%d ",v->date);
v=v->next;
}
}/*
void delete_list(PNODE list)
{
free(list);
}*/
/*
int changdu(PNODE list)
{
int i=0;
PNODE s=list->next;
while(NULL!=s)
{
i++;
s=s->next;
}
printf("changdu is:%d",i);
return i;
}
*/
/*
void sort_list(PNODE list)
{
int i,j;
PNODE first = list->next;
PNODE second= first->next;
PNODE f=first;
PNODE s=second;
PNODE l=list;
for(i=0;i<changdu(list);i++)
{
for(j=0;j<changdu(list)-i;j++)
{
/*
if(first->date>second->date)
{
int c;
c=second->date;
second->date=first->date;
first->date=c;
}*//*
if((first->date)>(second->date))
{
l=second;
second=first;
first=l;
}
first=first->next;
second=first->next;
}
first=f;
second=s;
}
}
*/
/*
void sort_list(PNODE list)
{
PNODE hh=(PNODE)malloc(sizeof(node));
hh->next=list;
PNODE s=list->next;
PNODE r=list->next;
PNODE flag=list;
flag->next=NULL;
PNODE l=list;
PNODE ll=list;
while(NULL!=s)
{
for(;flag->next!=r->next;r=r->next)
{
if(r->date>r->next->date)
{
int c=r->next->date;
r->next->date=r->date;
r->date=c;
}
hh=hh->next;
}
flag=hh;
s=s->next;
}
free(hh);
}*/
void sort_list(PNODE pHead)
{
PNODE pEnd = NULL;//ÖÕÖ¹Ö¸Õë
PNODE pTemp;//ÁÙʱ´æ·ÅÖÕÖ¹Ö¸Õë
PNODE p1 = (PNODE)malloc(sizeof(node));
p1->next = pHead;
pHead = p1;
PNODE p2;
while(pEnd != pHead)
{
pTemp = pHead;
for(p1 = pHead; p1->next->next != pEnd; p1 = p1->next)
{
if(p1->next->date > p1->next->next->date)
{
p2 = p1->next->next;
p1->next->next = p2->next;
p2->next = p1->next;
p1->next = p2;
pTemp = p1->next->next;
}
}
pEnd = pTemp;
}
p1 = pHead;
pHead = pHead->next;
free(p1);
p1 = NULL;
}
/*
void PopStack(PSTACK,int *);
void ClearStack(PSTACK);
*/
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct node
{
int date;
struct node* next;
}*PNODE;
typedef struct stack
{
PNODE pTop;
PNODE pBottom;
}STACK,*PSTACK;
void InitStack(PSTACK);
void push(PSTACK,int);
void TraverseStack(PSTACK);
int LengthStack(PSTACK);
int main(void)
{
STACK s;
InitStack(&s);
push(&s,2);
push(&s,3);
push(&s,4);
push(&s,5);
printf("L=");
TraverseStack(&s);
printf("\n");
return 0;
}
int LengthStack(PSTACK Ps)
{
if(Ps->pTop==Ps->pBottom)
return 0;
else
{
int i=0;
PNODE v=Ps->pTop;
while(v!=NULL&&v!=Ps->pBottom)
{
i++;
v=v->next;
}
return i;
}
}
void InitStack(PSTACK Ps)
{
Ps->pTop=(PNODE)malloc(sizeof(node));
if(NULL==Ps->pTop)
{
exit(-1);
}
Ps->pBottom=Ps->pTop;
Ps->pTop->next=NULL;
}
void push(PSTACK Ps,int val)
{
PNODE h=(PNODE)malloc(sizeof(node));
if(NULL==h)
{
exit(-1);
}
h->date=val;
h->next=Ps->pTop;
Ps->pTop=h;
}
void TraverseStack(PSTACK Ps)
{
if(LengthStack(Ps)>0)
{
PNODE v=Ps->pTop;
while(v!=NULL&&v!=Ps->pBottom)
{
printf("%d ",v->date);
v=v->next;
}
}
else
{
printf("The sheet is empty!");
exit(-1);
}
}
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct node
{
int date;
struct node* next;
}* PNODE;
typedef struct queue
{
PNODE front;
PNODE tear;
}Queue,* PQueue;
void InitQueue(PQueue );
void RuDui(PQueue ,int );
void TraverseQueue(PQueue );
int main(void)
{
Queue s;
InitQueue(&s);
RuDui(&s,5);
RuDui(&s,6);
RuDui(&s,7);
RuDui(&s,8);
RuDui(&s,9);
printf("Queue=");
TraverseQueue(&s);
return 0;
}
void InitQueue(PQueue s)
{
s->tear=(PNODE)malloc(sizeof(node));
if(NULL==s->tear)
{
exit(-1);
}
s->front=s->tear;
s->tear->next=NULL;
}
void RuDui(PQueue s,int val)
{
PNODE pnew=(PNODE)malloc(sizeof(node));
if(NULL==pnew)
{
exit(-1);
}
pnew->date=val;
s->tear->next=pnew;
pnew->next=NULL;
s->tear=pnew;
}
void TraverseQueue(PQueue s)
{
PNODE h=s->front->next;
while(h!=NULL)
{
printf("%d ",h->date);
h=h->next;
}
}
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define MAX 10
typedef struct XQueue
{
int * pointer;
int front;
int tear;
}Xq,*XQ;
void chushihua(XQ);
void rudui(XQ ,int);
void chudui(XQ,int *);
void Traverse(XQ );
bool empty(XQ );
bool full(XQ );
int main(void)
{
Xq s;
int val1,val2;
chushihua(&s);
rudui(&s,2);
rudui(&s,3);
rudui(&s,4);
rudui(&s,5);
rudui(&s,6);
rudui(&s,7);
rudui(&s,8);
rudui(&s,9);
// Go on ,See it whether is right all the time.
rudui(&s,20);
printf("X-Queue1=");
Traverse(&s);
chudui(&s,&val1);
chudui(&s,&val2);
printf("\nchu dui is:%d %d\n",val1,val2);
printf("X-Queue2=");
Traverse(&s);
return 0;
}
bool empty(XQ s)
{
if(s->front==s->tear)
return true;
else
return false;
}
bool full(XQ s)
{
if(s->front==(s->tear+1)%MAX)
return true;
else
return false;
}
void chushihua(XQ s)
{
s->pointer=(int*)malloc(sizeof(int)*MAX);
if(NULL==s->pointer)
{
exit(-1);
}
s->front=s->tear=0;
}
void rudui(XQ s,int val)
{
if(full(s))
{
printf("Full");
exit(-1);
}
else
{
s->pointer[s->tear]=val;
s->tear=(s->tear+1)%MAX;
}
}
void Traverse(XQ s)
{
if(empty(s))
{
printf("Empty!");
exit(-1);
}
else
{
int i=s->front;
while(i!=s->tear)
{
printf("%d ",s->pointer[i]);
i=(i+1)%MAX;
}
}
}
void chudui(XQ s,int *va)
{
if(empty(s))
{
printf("Empty!");
exit(-1);
}
else
{
*va=s->pointer[s->front];
s->front=(s->front+1)%MAX;
}
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXSTRLEN 255
typedef char SString[MAXSTRLEN+1];
int StrAssign(SString ,char *);
void StrCopy(SString ,SString );
void StrPrint(SString );
int StrCompare(SString ,SString );
int ConCat(SString ,SString ,SString );
int main(void)
{
int k;
char s,a[MAXSTRLEN+1];
SString s1,s2,T;
printf("please input s1:\n");
gets(a);
StrAssign(s1,a);
StrCopy(s2,s1);
printf("the string is:\n");
StrPrint(s2);
printf("please input s2:\n");
gets(a);
k=StrAssign(s2,a);
if(!k)
{
printf("chao chu chuang chang [%d].\n",MAXSTRLEN);
exit(-1);
}
if(StrCompare(s1,s2)>0)
printf("s1 > s2");
else if(StrCompare(s1,s2)<0)
printf("s1 < s2");
else
printf("s1 = s2");
ConCat(T,s1,s2);
printf("\nT is:");
StrPrint(T);
return 0;
}
int StrCompare(SString S,SString T)
{
int i;
for(i=1;i<=S[0]&&i<=T[0];++i)
if(S[i]!=T[i])
return S[i]-T[i];
return S[0]-T[0];
}
int ConCat(SString T,SString S1,SString S2)
{
int i;
if(S1[0]+S2[0]<=MAXSTRLEN)
{
for(i=1;i<=S1[0];i++)
T[i]=S1[i];
for(i=1;i<=S2[0];i++)
T[S1[0]+i]=S2[i];
T[0]=S1[0]+S2[0];
return 1;
}
else
{
for(i=1;i<=S1[0];i++)
T[i]=S1[i];
for(i=1;i<=MAXSTRLEN-S1[0];i++)
T[S1[0]+i]=S2[i];
T[0]=MAXSTRLEN;
return 0;
}
}
int StrAssign(SString T,char *chars)
{
int i;
if(strlen(chars)>MAXSTRLEN)
return 0;
else
{
T[0]=strlen(chars);
for(i=1;i<=T[0];i++)
T[i]=*(chars+i-1);
return 1;
}
}
void StrCopy(SString T,SString S)
{
int i;
for(i=0;i<=S[0];i++)
T[i]=S[i];
}
void StrPrint(SString T)
{
int i;
for(i=1;i<=T[0];i++)
printf("%c",T[i]);
printf("\n");
}