【数据结构上机】习题1_小明打字
题目:

思路:
一开始没想到用双向链表,单纯数组处理的很麻烦ojz
然后改用双向链表,头尾之间是正在键入的内容,temp为当前光标位置,然后根据读入的字符来判断进行什么操作
昨晚上没写出来的时候跟同学交流,她在处理[ 的时候新开了一个数组来存储之后的内容,这点感觉很麻烦,不过我一开始也是这个想法()
还有一个朋友是先把整个输入读入成字符串之后再遍历进行处理,也用到了双向链表,感觉也挺不错的xxx
代码:
#include <stdio.h>
#include <stdlib.h>
/*
jilin[i lofe{{-v-} ] universiti=y
*/
typedef struct LIST {
char ch;
struct LIST* left;
struct LIST* right;
}*link, node;
int jd = 1;
link add(link& temp, link tail, char a, int jd)
{
if (jd)
{
link p;
p = (link)malloc(sizeof(node));
p->ch = a;
p->left = temp;
p->right = temp->right;
temp->right = p;
p->right->left = p;
temp = temp->right;
}
else
{
if (temp->right != tail)
{
temp = temp->right;;
temp->ch = a;
}
else
add(temp, tail, a, !jd);
}
return temp;
}
void del(link& temp)
{
temp->left->right = temp->right;
temp->right->left = temp->left;
temp = temp->left;
}
int main()
{
char ch;
link head, tail, temp;
head = (link)malloc(sizeof(node));
tail = (link)malloc(sizeof(node));
head->left = NULL;
head->right = tail;
tail->left = head;
tail->right = NULL;
temp = head;
while (scanf("%c",&ch) && ch != '\n')
{
if (ch == '[')
{
temp = head;
}
if (ch == ']')
{
temp = tail->left;
}
if (ch == '{')
{
if (temp != head)
temp = temp->left;
}
if (ch == '}')
{
if (temp != tail->left)
temp = temp->right;
}
if (ch == '-')
jd = !jd;
if (ch == '=')
{
if (temp != head)
del(temp);
}
if ((ch >= 'a' && ch <= 'z') || ch == ' ')
temp = add(temp, tail, ch, jd);
}
for (temp = head->right; temp != tail; temp = temp->right)
printf("%c",temp->ch);
return 0;
}

浙公网安备 33010602011771号