反转链表:
需要稍加变通,如果直接反转结点的指针方向后,会发现丢失了原来的地址,所有我们需要一个临时进行储存。
struct node* fan(List*list)
{
struct node*pre=NULL;
struct node*p=list->head;
struct node*temp;
while(p)
{
temp=p->next;
p->next=pre;
pre=p;
p=temp;
}
return pre;
}
我们需要返回的时pre(一开始的NULL)因为最后的p会时NULL。
释放链表:
void freelist(List*list)
{
struct node*p;
struct node*q;
for(q=NULL,p=list->head;p;p=q)
{
q=p->next;
free(p);
}
}
p1739 括号匹配
只能写写简单题了甚至栈都用不到
#include<stdio.h>
int main()
{
char a[300];
scanf("%s",a);
int i,top=-1;
for(i=0;a[i]!='@';i++)
{
if(a[i]==')'&&top==-1){printf("NO");return 0;}
if(a[i]=='(')++top;
if(a[i]==')')top--;
}
if(top==-1)printf("YES\n");
else printf("NO\n");
}