
刚好这两天也在玩链表,想写个模板.给你一段代码吧
#include<stdio.h>#include<stdlib.h>
#include<malloc.h>
typedef int DataType//数据类型,如果这里更改了类型,
//要注意更改链表输出函数中输出格式.
struct _Node
{
DataType data
struct _Node *next
}Node
typedef struct _Node list_single
list_single *Node_Create(DataType data)//新建结点
{
list_single *p=NULL
p=(list_single *)malloc(sizeof(list_single))
if(p==NULL)
{
printf("malloc fair!\n")
exit(1)
}
p->data=data
p->next=NULL
//printf("data=%d\n", p->data)
return p
}
void Push_Back(list_single *head, DataType data)//表尾添加
{
list_single *p= NULL
if (head==NULL)
{
head = Node_Create(data)
p=head
}
else
{
p=head
while (p->next)
{
p= p->next
}
}
p->next = Node_Create(data)
}
void List_Print(list_single *head)//打印链表
{
list_single *p=head
printf("Head")
while(p->next)
{
p=p->next
//printf("%3c",p->data)
printf(" ->%d",p->data )//注意保持这个输出格式与DataType一致
}
printf("\n")
}
int List_Count(list_single *head)//表长
{
int k=0
list_single *p=head
while(p->next)
{
p=p->next
k++
}
return k
}
list_single *List_Create(void)//建表
{
int x
list_single *head=NULL
list_single *p=NULL
head=Node_Create(NULL)//建头结点
p=head
if(p==NULL)
{
printf("malloc fair!\n")
exit(1)
}
printf("输入一个数x\n")//追加结点
scanf("%d",&x)
while(x!=-1)
{
Push_Back(p,x)//追加结点到表尾,或者只用下面两句代码,效果一样
//p->next = Node_Create(x)
//p=p->next
scanf("%d",&x)
}
return head//返回表头
}
int main()
{
list_single *p
p=List_Create()//建表
List_Print(p)//输出
printf("Size:%d\n",List_Count(p))//输出表长
return 0
}
/*你提供的那个代码是伪代码,我帮你写成了
C
的代码。
你提供的伪代码,我用注释写到了对应的行的后面。
根据你提供的代码还只能转换含有一个字母或
[0,9]
区间内的数字的解析式。
补充了一点栈的基本算法
执行后输出:
原式子(中缀表达式):
a+b*c
新式子(后缀表达式):
a
b
c
*
+
代码使用
Linux
+
GCC
3.4.5
编译通过
July.28th,2009
14:30pm.
main()
在最下边
*/
#include
struct
_stack
{
char
__stack[1024]
int
__top
}
//
模拟栈
inline
init_stack(
struct
_stack
*
p_stack
)
//
初始化栈
{
int
i
=
0
for(
i
=
0
i
<=
1024
i++
)
p_stack->__stack[i]
=
'\0'
p_stack->__top
=
0
}
inline
void
push(
struct
_stack
*
p_stack,
char
p_op
)
//
进栈
{
p_stack->__stack[p_stack->__top]
=
p_op
(
p_stack->__top
)++
}
inline
char
pop(
struct
_stack
*
p_stack
)
//
出栈
{
(
p_stack->__top
)--
return
p_stack->__stack[p_stack->__top]
}
inline
int
precedence(
char
p_char
)
//
比较优先级
{
if(
p_char
==
'+'
||
p_char
==
'-'
)
return
1
else
if(
p_char
==
'/'
||
p_char
==
'*'
)
return
2
else
return
0
}
char
postString[1024]
char
*
infix_to_postfix(
char
*inString
)
{
int
l
=
strlen(
inString
)
struct
_stack
stack
char
p
=
'\0'
int
i
=
0
char
c
=
'\0'
l
=
strlen(
inString
)
//
l
<-
Length
of
inString
for(
i
=
0
i
<=
1024
i++
)
postString[i]
=
'\0'
//
postString
<-
empty
string
init_stack(
&stack
)
//
stack
<-
Empty
stack
for(
i
=
0
i
<=
l
i++
)
//
for
i
<-
0
to
l
{
c
=
inString[i]
//
c
<-
inString[i]
if(
(
c
>=
'0'
&&
c
<=
'9'
)||(
c
>=
'a'
&&
c
<=
'z'
)||(
c
>=
'A'
&&
c
<=
'Z'
)
)
//
if
c
is
an
operand
{
if(
postString[0]
==
'\0'
){postString[0]
=
ccontinue}
sprintf(
postString,
"%s
%c",
postString,
c
)
//
add
c
at
the
end
of
postString
}//
end
if
if(
c
==
'+'
||
c
==
'-'
||
c
==
'*'
||
c
==
'/'
)
//
if
c
is
an
operator
{
if(
stack.__top==0
||
precedence(
stack.__stack
[stack.__top]
)<
precedence(
c
)
)
//
if
stack
is
empty
OR
if
precedence(
stack_top
)<
precedence(
c
)
{
push(
&stack,
c
)
//
push
c
into
stack
}
else
{
while(
precedence(
stack.__stack
[stack.__top]
)>=
precedence(
c
)
)
//
while
precedence(
stack_top
)>=
precedence(
c
)
{
p
=
pop(
&stack
)
//
p
<-
pop
from
stack
sprintf(
postString,
"%s
%c",
postString,
p
)
//
add
p
at
the
end
of
postString
}
//
end
while
push(
&stack,
c
)
//
push
c
into
stack
}
//
end
if
}
//
end
if
}
//
end
for
while(
stack.__top
)
//
while
stack
is
not
empty
{
p
=
pop(
&stack
)
//
p
<-
pop
from
stack
sprintf(
postString,
"%s
%c",
postString,
p
)
//
add
p
at
the
end
of
postString
}
//
end
while
return
postString
}
int
main(
)
{
char
exp[]
=
"a+b*c"
char
*
p
=
0
p
=
infix_to_postfix(
exp
)
printf(
"原式子(中缀表达式):\n\t%s\n新式子(后缀表达式):\n\t%s\n",
exp,
p
)
return
0
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)