想用双向链表写,但是一直报段错误,不知道原因,求大神救救
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct Node{
char data;
struct Node * rLink, * lLink; // 设置前驱结点和后继结点
}DblNode, *DblList;
int main(void){
DblList first;
first = (DblList) malloc (sizeof(DblNode));
first -> rLink = first;
first -> lLink = first;
DblList s, q, tmp = first;
int insert_flag = 1;
char ch;
while((ch = getchar()) != '\n'){
tmp = (DblList)malloc (sizeof(DblNode));
if(ch == '['){
tmp = first -> rLink ;
}else if(ch == ']'){
tmp = first -> lLink;
} else if (ch == '{') {
if (tmp -> lLink)
tmp = tmp -> lLink;
} else if (ch == '}') {
if (tmp -> rLink)
tmp = tmp -> rLink;
} else if (ch == '-') {
insert_flag = insert_flag * (-1);
}else if(ch == '='){
DblList t = tmp;
tmp -> rLink -> lLink = tmp -> lLink;
tmp -> lLink -> rLink = tmp -> rLink;
tmp = tmp -> lLink;
free(t);
}else{
s = (DblList) malloc(sizeof(DblNode));
s -> data = ch;
q = tmp -> rLink;
if(insert_flag < 0){
s -> lLink = tmp -> lLink;
q -> lLink = s;
tmp -> lLink -> rLink = s;
s -> rLink = q;
}else{
s -> lLink = tmp;
q -> lLink = s;
tmp -> rLink = s;
s -> rLink = q;
}
tmp = s;
}
}
// 输出
DblList p;
p = first -> rLink;
while(p != first){
printf("%c", p -> data);
p = p -> rLink;
}
}
|