zl程序教程

您现在的位置是:首页 >  后端

当前栏目

广义表的创建和遍历

遍历 创建 广义
2023-09-14 08:57:55 时间
for(list string ::iterator i = sl.begin(); i!=sl.end(); ++i){ string s = *i, first, second; int pos = s.find_first_of("="); if(pos!=s.npos){ first = s.substr(0, pos); second = s.substr(pos+1, s.length()); } else { cout "广义表的描述字符串出错!" endl; return ERROR; mp.insert(make_pair(first, second)); void out(GList Lx){ if(!Lx) return; if(Lx- tag==NODE){ cout Lx- node endl; return; for(GList p=Lx; p; p=p- ptr.tp) out(p- ptr.hp); void createGList(GList L, string s){ if(s=="()"){ //创建空表 L = NULL; } else { L = new GLNode;//因为类中有string变量,不能用malloc, 否者赋值无效 if(s.find(",")==s.npos s.find("(")==s.npos s.find(")")==s.npos){//原子结点 if(mp.find(s) == mp.end()){ L- tag = NODE; L- node = s; } else {//当s是只有一个大写字母组成的时候,说明它是一个子表,继续扩展 createGList(L, mp[s]);//这块出了开始bug, 调了好长时间 } else {//非原子结点 L- tag = LIST; GList p = L, q; s = s.substr(1, s.length()-2); string hs; server(s, hs);//分离表头 createGList(p- ptr.hp, hs); q = p; if(s!=""){//表尾不空 p = new GLNode; p- tag = LIST; q- ptr.tp = p; }while(s!=""); q- ptr.tp = NULL; int main(){ freopen("in.txt", "r", stdin); list string string s; while(cin s){ sl.push_back(s); MyGList myGList(sl); myGList.buildGList(); myGList.outGList(); return 0;
LeetCode 25K 个一组翻转链表&26删除排序数组中的重复项 给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。 k 是一个正整数,它的值小于或等于链表的长度。 如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。
链表- 删除排序链表中的重复元素 存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除所有重复的元素,使每个元素 只出现一次 。返回同样按升序排列的结果链表。