面试题整理-链表的几个微操
2012-10-17 08:59
447 查看
typedef struct _node { int data; struct _node *next; }node;
1 在链表未尾插入一个结点
node *push_back(node **head, int value) { node *p = *head; if (NULL == head) return NULL; if (NULL == *head) { *head = apply(); (*head)->data = value; return *head; } while (p->next) p = p->next; p->next = apply(); p->next->data = value; return *head; }要注意对于头指针的处理。
2、删除一个找到的值
node *remove_node(node **head, int value) { node *p = *head, *front = NULL; if (NULL == p || NULL == head) return NULL; while (p && p->data != value) { front = p; p = p->next; } if (!p) return *head; if (front) front = p->next; else *head = p->next; free(p); return *head; }
3 逆序输出链表
typedef struct _xnode { node *data; node *next; }xnode; void push(xnode **stack, node *p) { xnode *t = NULL; if (!stack) return; t = (xnode *) malloc(sizeof(xnode)); if (!*stack) { (*stack) = t; (*stack)->data = p; (*stack)->next = NULL; } else { t->data = p; t->next = *stack; (*stack) = t; } } node *pop(xnode **stack) { xnode *t = NULL; node *p = NULL; if (!stack || !(*stack)) return NULL; t = (*stack)->next; p = (*stack)->data; free(*stack); *stack = t; return p; } void rprint(node *head) { xnode *stack = NULL; node *p = head; while (p) { push(&stack, p); p = p->next; } while ((p = pop(&stack))) { printf("->%d", p->data); } }
相关文章推荐
- 链表面试题整理
- 面试题整理13 合并排序链表去重
- 【IT笔试面试题整理】删除无序链表中重复的节点
- [分类整理II]微软等100题系列V0.1版:链表面试题集锦
- [分类整理II]微软等100题系列V0.1版:链表面试题集锦
- 【IT笔试面试题整理】给定二叉树,给每层生成一个链表
- 近期面试题整理(二叉树的中序遍历、合并排序链表数组)
- [分类整理II]微软等100题系列V0.1版:链表面试题集锦
- [分类整理II]微软等100题系列V0.1版:链表面试题集锦
- 几个常见的链表面试题<一>
- 2014年5月整理java笔试题及几个小面试题
- 面试题整理 2:求链表倒数第 k 个结点
- 【IT笔试面试题整理】二叉搜索树转换为双向链表
- 链表面试题整理
- 整理几个有想法的面试题
- 【IT笔试面试题整理】反转链表
- 面试题整理-链表形式大数求和
- 整理几个常用的Java面试题
- 【IT笔试面试题整理】判断链表是否存在环路,并找出回路起点
- 整理的Java面试题