#include "stdio.h"
#include "malloc.h"
#include "assert.h"
#define NULL 0
#ifdef STR_REVERSE
char* reverse(char* str)
{
int len = strlen(str);
int i = 0;
char tmp = '\0';
for(;i < len/2;i++)
{
tmp = str[i];
str[i] = str[len-i-1];
str[len-i-1] =tmp;
}
return str;
}
char* fun(char* str1,char* str2)
{
char* str_tmp = NULL;
char* str_result = NULL;
str_tmp = reverse(str2);
str_result = strcat(str1,str_tmp);
return str_result;
}
int main()
{
char str1[20] = {0};
char str2[20] = {0};
char* str = NULL;
printf("Please input the first string:\n");
gets(str1);
printf("Please input the second string:\n");
gets(str2);
str = fun(str1,str2);
printf("result is %s\n",str);
getchar();
return 0;
}
#endif
/*********************************************************************************
上面的函数实现了将字符串str2翻转,然后与字符串str1拼接在一起,
需要注意strlen获取的是'\0'之前的字符的个数,不包括'\0'字符。
翻转函数使用的方法是:第i个元素与第len-i-1个元素交换,直到i=len/2结束。
即沿数组的中线(奇数为中间元素)对折。
gets函数遇到回车或EOF结束。关于库函数strlen和gets的实现和使用的注意事项,
再次复习的时候可以作为扩展的内容呈现。欢迎一起讨论学习。
**********************************************************************************************/
#ifdef ARRAY_POINTER
int main()
{
int p = 5,p2 = 6,*page,*page2;
int *ptr[5];
int Test[2][3] ={{1,2,3},{4,5,6}};
int Test2[3] = {1,2,3};
page = &p;
page2 = &p2;
ptr[0] = &p;
ptr[1] = &p2;
int (*A)[3],(*B)[3];
A = &Test[1];
B = &Test2;
printf("%d\n",*page);
printf("%d,%d,%d\n",(*A)[0],(*A)[1],(*A)[2]);
printf("%d\n",(*B)[2]);
printf("%d\n",*(*B+2));
printf("hello world!\n");
getchar();
return 0;
}
#endif
#ifdef POINTER_ARRAY
void strsort(char **p,int n); //函数声明
int main()
{ int i;
char *s[]={ //初始化
"C",
"Basic",
"Foxpro",
"Visual Studio"
};
strsort(s,sizeof(s)/4); //排序
printf("\n排序后的数据:\n");
for(i=0;i<sizeof(s)/4;i++) //输出
{
printf("%s\n",s[i]);
}
getchar();
return 0;
}
void strsort(char**p,int n) //自定义函数
{
int i,j;
char *pstr;
for(i=0;i<n-1;i++)
{
for (j=i+1;j<n;j++)
{
if (strcmp(*(p+i),*(p+j))>0)//比较
{
pstr=*(p+j);
*(p+j)=*(p+i);
*(p+i)=pstr;
}
}
}
}
#endif
#ifdef Test_64
int main()
{
int *pi = 0;
*pi = 5;
return 0;
}
#endif
#ifdef Test_52
typedef struct node1
{
int a;
char b;
int c;
}s1;
int main()
{
int a;
char b;
int c;
s1 s;
printf("%0#x\n",&a);
printf("%0#x\n",&b);
printf("%0#x\n",&c);
printf("%0#x\n",&s.a);
printf("%0#x\n",&s.b);
printf("%0#x\n",&s.c);
getchar();
return 0;
}
#endif
#ifdef Test_44
#define SUB(x,y) (x-y)
#define ACCESS_BEFORE(element,offset,value) *SUB(&element,offset) = value
int main()
{
int i;
int array_t[10] = {1,2,3,4,5,6,7,8,9,10};
ACCESS_BEFORE(array_t[5],4,6);
for(i=0;i<10;++i)
{
printf("%d ",array_t[i]);
}
getchar();
return 0;
}
#endif
#ifdef Test_31_1
int func(int x)
{
int count = 0;
while(x)
{
count++;
x=x&(x-1);
}
return count;
}
int main()
{
printf("%d\n",func(9999));
getchar();
return 0;
}
#endif
#ifdef Test_31_2
int main()
{
int a,x;
for(a=0,x=0;a<=1 && !x++;/*a++*/)
{
a++;
}
printf("%d %d\n",a,x);
getchar();
return 0;
}
#endif
#ifdef Test_32
int main()
{
int b = 3;
int arr[] = {6,7,8,9,10};
int *ptr = arr;
*(ptr++)+=123;
printf("%d,%d\n",*ptr,*(++ptr));
getchar();
return 0;
}
#endif
#ifdef Test_180
/*常用数据结构--单链表--创建、遍历、删除、插入等操作*/
typedef struct student
{
int data;
struct student *next;
}node,*pLinkList;
node* CreateSingleLink()
{
int cycleFlag = 1,seqNo = 1,elemValue = 0;
node *pNode = NULL,*pHead = NULL,*pTmp = NULL;
pHead = (node*)malloc(sizeof(node));
assert(pHead != NULL);
pHead->data = 0;
pHead->next = NULL;/*建立头节点*/
pTmp = pHead;
while(cycleFlag)
{
printf("请输入第%d个结点的元素值\n",seqNo);
scanf("%d",&elemValue);
if(elemValue != 0)
{
pNode = (node*)malloc(sizeof(node));
assert(pNode != NULL);
pNode->data = elemValue;
pTmp->next = pNode;
pTmp = pNode;
seqNo++;
}
else
{
cycleFlag = 0;
}
pTmp->next = NULL;
}
return pHead;
}
int GetLength(pLinkList L)
{
int listLen = 0;
node *pNode = NULL;
if(NULL == L)
return 0;
pNode = L->next;
while(pNode)
{
listLen++;
pNode = pNode->next ;
}
return listLen;
}
void listPrint(pLinkList L)
{
node *pNode = NULL;
if(NULL == L || NULL == L->next)
printf("链表为空\n");
pNode = L->next;
while(pNode)
{
printf("%d\n",pNode->data );
pNode = pNode->next;
}
}
/*按数据删除*/
node* listDel(pLinkList L,int num)
{
node *p = NULL,*q = NULL;
if(NULL == L)
printf("链表为空\n");
p = L->next ;
q = L;
/*whiel(p)寻找value==num的结点
{
if(p->data == num)
}*/
while(NULL != p && p->data != num)
{
q = p;
p = p->next ;
}
if( NULL == p)
{
printf("链表中没有这个结点。\n");
}
else
{
q->next = p->next;
free(p);
}
return L;
}
/*按索引删除*/
node* listDel_Index(pLinkList L,int index)
{
int j = 0;
node *pNode = NULL,*qNode = NULL;
pNode = L->next;
qNode = L;
while(NULL != pNode && j < index - 1)
{
j++;
qNode = pNode;
pNode = pNode->next;
}
if(NULL == pNode || j > index - 1)
printf("链表中没有这个结点。\n");
else
{
qNode->next = pNode->next;
free(pNode);
}
return L;
}
node *listInsert(pLinkList L,int i,int value)
{
int j = 0;
node *pNode = NULL,*qNode = NULL;
pNode = L;
if(NULL == pNode)
printf("链表为空\n");
while(NULL != pNode && j < i - 1)
{
j++;
pNode = pNode->next;
}
if(NULL == pNode || j > i - 1)
printf("插入位置不对,位置小于1或者位置大于表长+1\n");
else
{
qNode = (node *)malloc(sizeof(node));
assert(qNode != NULL);
qNode->data = value;
qNode->next = pNode->next;
pNode->next = qNode;
}
return L;
}
/*不对称边界*/
/*将相邻的两个数比较,小的调到前头*/
/*n个数要进行n-1趟比较*/
/*第i趟,需要比较的次数j为n-i*/
node *Bubble_Sort(pLinkList L)
{
node *pNode = NULL;
int i = 0 ,j = 0,tmp = 0;
int len = GetLength(L);
pNode = L->next;
for(i = 0;i<len-1;i++)
{
pNode = L->next ;
for(j=0;j<len-i-1;j++)
{
if(pNode->data > pNode->next->data)
{
tmp = pNode->data;
pNode->data = pNode->next->data;
pNode->next->data = tmp;
}
pNode = pNode->next;
}
}
return L;
}
/*n个数,需要进行n-1次选择*/
/*需要比较i+1到n*/
void Select_Sort(int arr[],int len)
{
int i = 0,j = 0,min = 0;
int tmp = 0;
for(i = 0;i < len - 1;i++)
{
min = i;
/*要特别留意下面语句的执行范围*/
for(j = i+1;j<len;j++)
if(arr[min]>arr[j])
min = j;
tmp = arr[i];
arr[i] = arr[min];
arr[min] = tmp;
}
}
void PrintArray(int arr[],int n)
{
int i = 0;
for(i=0;i<n;i++)
printf("%2d",arr[i]);
}
int main()
{
int listLen = 0;
int arr[5] = {3,4,1,2,5};
pLinkList linkList = NULL;
linkList = CreateSingleLink();
listLen = GetLength(linkList);
printf("%d\n",listLen);
listPrint(linkList);
linkList = listDel_Index(linkList,1);
listPrint(linkList);
linkList = listDel_Index(linkList,1);
listPrint(linkList);
linkList = listDel_Index(linkList,1);
listPrint(linkList);
linkList = listInsert(linkList,1,10);
linkList = listInsert(linkList,2,200);
linkList = listInsert(linkList,1,300);
listPrint(linkList);
linkList = Bubble_Sort(linkList);
listPrint(linkList);
Select_Sort(arr,5);
PrintArray(arr,5);
getchar();
getchar();
return 0;
}
#endif
#ifdef Test_186_1
typedef struct node//节点存放一个数据和指向下一个节点的指针
{
int data;
struct node* pnext;
} Node;
Node *link_create(int n)//创建n个节点的循环链表
{
//先创建第1个节点
Node *p,*q,*head;
int i ;
p = (Node *)malloc(sizeof(Node));
head = p;
p->data = 1;
for(i = 2;i<=n;i++)//再创建剩余节点
{
q = (Node *)malloc(sizeof(Node));
q->data = i;
p->pnext = q;
p = q;
}
p->pnext = head;//最后一个节点指向头部,形成循环链表
return head;
}
void link_process(Node *head,int k,int m)//从编号为k(1<=k<=n)的人开始报数,数到m的那个人出列;
{
int i;
Node *p = head,*tmp1;
while(p->data != k)//p指向的节点data为k
p = p->pnext;
while(p->pnext != p)
{
for(i=0;i<m-1;i++)
{
tmp1 = p;
p = p->pnext;
}
printf("%d ",p->data);
tmp1->pnext= p->pnext;
free(p);
p = tmp1->pnext;
}
printf("%d ",p->data);
free(p);
}
int main()
{
Node *head = link_create(6);
link_process(head,1,5);
getchar();
return 0;
}
#endif
#ifdef Test_186_2
/*有点问题*/
typedef struct LNode{
int data;
struct LNode *link;
}LNode,*LinkList;
void JOSEPHUS(int n,int k,int m)
{
LinkList p,r,list,curr;
p = (LinkList)malloc(sizeof(LNode));
p->data = 0;
p->link = p;
curr = p;
for(int i = 1;i<n;i++)
{
LinkList t =(LinkList)malloc(sizeof(LNode));
t->data = i;
t->link = curr->link ;
curr->link = t;
curr = t;
}
r= curr;
while(k--) r=p,p=p->link ;
while(n--)
{
for(int s = m-1;s--;r=p,p=p->link);
r->link = p->link ;
printf("%d->",p->data );
free(p);
p= r->link ;
}
}
int main()
{
JOSEPHUS(6,1,5);
getchar();
return 0;
}
#endif
#ifdef Test_queue
typedef struct node
{
int data;
struct node *next;
}Node,*pQueue;
typedef struct
{
pQueue front;
pQueue rear;
}LinkQueue;
void InitQueue(LinkQueue *Q)
{
Q->rear = Q->front = (pQueue)malloc(sizeof(Node));
assert(Q->front != NULL);
Q->front->next = NULL;
}
int EnQueue(LinkQueue *Q,int elem)
{
Node *pNode = (pQueue)malloc(sizeof(Node));
assert(pNode);
pNode->data = elem;
pNode->next = NULL;
Q->rear->next = pNode;
Q->rear = pNode;
return 0;
}
int DeQueue(LinkQueue *Q,int *elem)
{
Node *p = NULL;
if(Q->front == Q->rear )
return 1;
p = Q->front ->next ;
*elem = p->data;
Q->front ->next = p->next;
if(Q->rear == p)
Q->rear = Q->front ;
free(p);
return 0;
}
int main()
{
LinkQueue Q;
int elem = 0;
InitQueue(&Q);
EnQueue(&Q,1);
EnQueue(&Q,2);
EnQueue(&Q,3);
DeQueue(&Q,&elem);
printf("%d\n",elem);
DeQueue(&Q,&elem);
printf("%d\n",elem);
DeQueue(&Q,&elem);
printf("%d\n",elem);
getchar();
return 0;
}
#endif
#define Test_sequence_queue
#ifdef Test_sequence_queue
/*约定Q->front=Q->rear时队列为空,(Q->rear+1)%MAXSIZE = Q->front时队列为满*/
#define MAXSIZE 20
typedef struct
{
int *base;
int front;
int rear;
}SqQueue;
void InitQueue(SqQueue *Q)
{
Q->base = (int*)malloc(MAXSIZE*sizeof(int));
assert(Q->base );
Q->front = Q->rear =0;
}
int QueueLen(SqQueue *Q)
{
return (Q->rear - Q->front +MAXSIZE)%MAXSIZE;
}
int EnQueue(SqQueue *Q,int e)
{
if((Q->rear + 1)%MAXSIZE == Q->front )//队列满
return -1;
Q->base [Q->rear ]= e;
Q->rear = (Q->rear + 1)%MAXSIZE;
return 0;
}
int DeQueue(SqQueue *Q,int *e)
{
if(Q->rear ==Q->front )
return -1;
*e = Q->base [Q->front ];
Q->front = (Q->front + 1)%MAXSIZE;
return 0;
}
void Print(SqQueue *q) //打印队列
{
if(q->front == q->rear)
return;
else if(q->rear < q->front)
{
for(int i = q->front;i < MAXSIZE;++i)
printf("%d ",q->base[i]);
for(int i = 0;i < q->rear;++i)
printf("%d ",q->base[i]);
printf("\n");
}
else{
for(int i = q->front;i < q->rear;++i)
printf("%d ",q->base[i]);
printf("\n");
}
}
int main()
{
SqQueue q;
InitQueue(&q);
for(int i = 1;i < 20;++i)
EnQueue(&q,i);
Print(&q);
int k;
DeQueue(&q,&k);
EnQueue(&q,30);
Print(&q);
getchar();
return 0;
}
#endif