您的位置:首页 > 理论基础 > 数据结构算法

数据结构的简单应用

2011-08-08 13:13 323 查看
栈和队列解决球钟问题:

#include <stdio.h>
#include <stdlib.h>

#define N 16

#define S1	4
#define S5  11
#define S60 11

typedef int datatype;

typedef struct _node_
{
datatype data;
struct _node_ *next;
} linknode, *linklist;

typedef struct
{
linklist front, rear;
} linkqueue;

typedef struct
{
datatype data
;
int top;
} sqstack;

sqstack *CreateEmptyStack()
{
sqstack *s;

s = (sqstack *)malloc(sizeof(sqstack));
s->top = -1;

return s;
}

int EmptyStack(sqstack *s)
{
return (-1 == s->top);
}

int FullStack(sqstack *s)
{
return (N-1 == s->top);
}

void PushStack(sqstack *s, datatype x)
{
s->data[++s->top] = x;

return;
}

datatype PopStack(sqstack *s)
{
return s->data[s->top--];
}

datatype GetTop(sqstack *s)
{
return s->data[s->top];
}

linkqueue *CreateEmptyQueue()
{
linkqueue *lq;

lq = (linkqueue *)malloc(sizeof(linkqueue));
lq->front = lq->rear = (linklist)malloc(sizeof(linknode));
lq->front->next = NULL;

return lq;
}

int EmptyQueue(linkqueue *lq)
{
return (lq->front == lq->rear);
}

void EnQueue(linkqueue *lq, datatype x)
{
lq->rear->next = (linklist)malloc(sizeof(linknode));
lq->rear = lq->rear->next;
lq->rear->data = x;
lq->rear->next = NULL;

return;
}

datatype DeQueue(linkqueue *lq)
{
linklist p;

p = lq->front;
lq->front = p->next;
free(p);

return (lq->front->data);
}

void ClearQueue(linkqueue *lq)
{
linklist p;

while (lq->front != lq->rear)
{
p = lq->front;
lq->front = p->next;
free(p);
}

return;
}

int Order(linkqueue *q)
{
linklist p = q->front->next;
while (p->next != NULL)
{
if (p->data > p->next->data) return 0;
p = p->next;
}
return 1;
}

int main()
{
int i, count = 0;
sqstack *s1, *s5, *s60;
linkqueue *q;

q = CreateEmptyQueue();
s1 = CreateEmptyStack();
s5 = CreateEmptyStack();
s60 = CreateEmptyStack();

for (i=1; i<=27; i++)
{
EnQueue(q, i);
}

while ( 1 )
{
count++;
i = DeQueue(q);
/*
if (s1->top != S1 - 1)
{
PushStack(s1, i);
}
else
{
while ( ! EmptyStack(s1) )
{
EnQueue(q, PopStack(s1));
}
if (s5->top != S5 - 1)
{
PushStack(s5, i);
}
else
{
while ( ! EmptyStack(s5) )
{
EnQueue(q, PopStack(s5));
}
if (s60->top != S60 - 1)
{
PushStack(s60, i);
}
else
{
while ( ! EmptyStack(s60) )
{
EnQueue(q, PopStack(s60));
}
EnQueue(q, i);
if (Order(q)) break;
}
}
}*/

if (s1->top != S1 - 1)
{
PushStack(s1, i);
continue;
}

while ( ! EmptyStack(s1) )
{
EnQueue(q, PopStack(s1));
}
if (s5->top != S5 - 1)
{
PushStack(s5, i);
continue;
}

while ( ! EmptyStack(s5) )
{
EnQueue(q, PopStack(s5));
}
if (s60->top != S60 - 1)
{
PushStack(s60, i);
continue;
}

while ( ! EmptyStack(s60) )
{
EnQueue(q, PopStack(s60));
}
EnQueue(q, i);
if (Order(q)) break;
}
printf("%d\n", count);

return 0;
}


循环链表解决Joseph问题:

#include <stdio.h>
#include <stdlib.h>

#define N 	 8
#define STEP 3

typedef int datatype;

typedef struct _node_
{
datatype data;
struct _node_ *next;
} linknode, *linklist;

int main()
{
int i;
linklist h, p;

h = (linklist)malloc(sizeof(linknode));
h->data = 1;
h->next = NULL;
p = h;
for (i=2; i<=N; i++)
{
p->next = (linklist)malloc(sizeof(linknode));
p = p->next;
p->data = i;
}
p->next = h;

for (i=0; i<=N; i++)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");

while (h->next != h)
{
for (i=0; i<STEP-2; i++) h = h->next;
p = h->next;
h->next = p->next;
printf("%d ", p->data);
free(p);
h = h->next;
}
printf("%d\n", h->data);

return 0;
}


栈实现基本表达式求解:

#include <stdio.h>
#include <stdlib.h>

typedef int datatype;

typedef struct _node_
{
datatype data;
struct _node_ *next;
} linknode, *linkstack;

linkstack CreateEmptyStack()
{
linkstack s;

s = (linkstack)malloc(sizeof(linknode));
s->next = NULL;

return s;
}

int EmptyStack(linkstack s)
{
return (NULL == s->next);
}

void PushStack(linkstack s, datatype x)
{
linkstack p;

p = (linkstack)malloc(sizeof(linknode));
p->data = x;
p->next = s->next;
s->next = p;

return;
}

datatype PopStack(linkstack s)
{
datatype x;
linkstack p = s->next;

s->next = p->next;
x = p->data;
free(p);

return x;
}

datatype GetTop(linkstack s)
{
return s->next->data;
}

void ClearStack(linkstack s)
{
linkstack p;

while (s->next != NULL)
{
p = s->next;
s->next = p->next;
free(p);
}

return;
}

int GetPri(char op)
{
switch ( op )
{
case '+' :
case '-' :
return 1;
case '*' :
case '/' :
return 2;
}
}

int Compute(int op1, int op2, char op)
{
switch ( op )
{
case '+' :
return op1 + op2;
case '-' :
return op1 - op2;
case '*' :
return op1 * op2;
case '/' :
return op1 / op2;
}
}

void del_op(linkstack operator, linkstack operand, char op)
{
int op1, op2;
char ch;

while ( ! EmptyStack(operator) && GetPri(op)<= GetPri((char)GetTop(operator)) )
{
ch = (char)PopStack(operator);
op2  = PopStack(operand);
op1 = PopStack(operand);
PushStack(operand, Compute(op1, op2, ch));
}
PushStack(operator, op);

return;
}

int main()
{
char str[128], *p = str;
int sum = 0, op1, op2;
char ch;
linkstack operator, operand;

operator = CreateEmptyStack();
operand = CreateEmptyStack();

scanf("%s", str);
while (*p != '\0')
{
if ((*p >= '0') && (*p <= '9'))
{
while ((*p >= '0') && (*p <= '9'))
{
sum = 10*sum + *p - '0';
p++;
}
PushStack(operand, sum);
sum = 0;
}
else
{
del_op(operator, operand, *p);
p++;
}
}

while ( ! EmptyStack(operator) )
{
ch = (char)PopStack(operator);
op2  = PopStack(operand);
op1 = PopStack(operand);
PushStack(operand, Compute(op1, op2, ch));
}
printf("%s = %d\n", str, GetTop(operand));

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构 struct null qq