(3) double-linked list: insque remque
来源:互联网 发布:冰岛语消失知乎 编辑:程序博客网 时间:2024/04/29 10:45
1.
/** * The insque and remque subroutines manipulate queues built from double-linked lists. * Each element in the queue must be in the form of a qelem structure. The next and * prev elements of that structure must point to the elements in the queue immediately * before and after the element to be inserted or deleted. * * The insque subroutine inserts the element pointed to by the Element parameter into * a queue immediately after the element pointed to by the Pred parameter. * The remque subroutine removes the element defined by the Element parameter from a queue. * * #include <search.h> * Inserts or removes an element in a queue. * void insque(void *elem, void *prev); * void remque(void *elem); *struct qelem {struct qelem *q_forw;struct qelem *q_back;};void insque (struct qelem *elem, struct qelem *pred){ elem -> q_forw = pred -> q_forw; pred -> q_forw -> q_back = elem; elem -> q_back = pred; pred -> q_forw = elem;}void remque (struct qelem *elem){ elem -> q_forw -> q_back = elem -> q_back; elem -> q_back -> q_forw = elem -> q_forw;}*/#ifndef _GNU_SOURCEstruct qelem {struct qelem *q_forw;struct qelem *q_back;char *element;};#endif#include <search.h>#include <stdlib.h>#include <stdio.h>#include <string.h>void *xmalloc(unsigned n) {void *p;p = malloc(n);if (p) return p;fprintf(stderr, "malloc failure, insufficient memory.\n");exit(1);}struct qelem *create_head(){struct qelem *head = (struct qelem *)xmalloc(sizeof(struct qelem));head->q_forw = head;head->q_back = head;return head;}int main(int argc, char **argv){struct qelem *head = create_head();//head's element is blankstruct qelem *prev = head;struct qelem *tmp;//insque, actually insert the first element into ahead of headint i;for(i = 1; i < argc; i++){struct qelem *node = (struct qelem *)xmalloc(sizeof(struct qelem));node->element = argv[i];insque(node, prev);prev = node;}//traversing the listtmp = prev;puts("traversing the list:");while(tmp != head){puts(tmp->element);tmp = tmp->q_back;}//remque the first element after headputs("after remque, go through:");remque(head->q_forw);tmp = head->q_forw;while(tmp != head){puts(tmp->element);tmp = tmp->q_forw;}return 0;}
2
$ ./a.out -c a b c Traversing completed list: a b c That was a circular list
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <search.h> struct element { struct element *forward; struct element *backward; char *name; }; static struct element * new_element(void) { struct element *e; e = malloc(sizeof(struct element)); if (e == NULL) { fprintf(stderr, "malloc() failed\n"); exit(EXIT_FAILURE); } return e; } int main(int argc, char *argv[]) { struct element *first, *elem, *prev; int circular, opt, errfnd; /* The "-c" command-line option can be used to specify that the list is circular */ errfnd = 0; circular = 0; while ((opt = getopt(argc, argv, "c")) != -1) { switch (opt) { case 'c': circular = 1; break; default: errfnd = 1; break; } } if (errfnd || optind >= argc) { fprintf(stderr, "Usage: %s [-c] string...\n", argv[0]); exit(EXIT_FAILURE); } /* Create first element and place it in the linked list */ elem = new_element(); first = elem; elem->name = argv[optind]; if (circular) { elem->forward = elem; elem->backward = elem; insque(elem, elem); } else { insque(elem, NULL); } /* Add remaining command-line arguments as list elements */ while (++optind < argc) { prev = elem; elem = new_element(); elem->name = argv[optind]; insque(elem, prev); } /* Traverse the list from the start, printing element names */ printf("Traversing completed list:\n"); elem = first; do { printf(" %s\n", elem->name); elem = elem->forward; } while (elem != NULL && elem != first); if (elem == first) printf("That was a circular list\n"); exit(EXIT_SUCCESS); }
- (3) double-linked list: insque remque
- Double Circular Linked List
- bst double Linked List
- 双链表 double linked list
- List(double linked)
- malloc2 double linked list
- double linked list of kernel
- smallbin double linked list corrupted
- BST to Double Linked List
- Double Linked List--Data Structure
- list实现ALDS1_3_C;double Linked List
- 段错误:smallbin double linked list corrupted
- Implement Double Linked List from Stack
- double linked list双向链表
- Flatten BST to Double Linked List
- Convert Binary Search Tree to an Ordered Double Linked List
- No. 01 - Binary Search Tree and Double-linked List
- *** glibc detected *** /home/app: corrupted double-linked list: 0x08b08918 ***
- jfreechart生成柱状图写到jsp页面并打印
- 忧雨的伤感空间日志发布:左手夹烟,右手夹寂寞
- Redis
- java 数据库连接池 c3p0 使用及参数详解,支持重连
- 教你写一个可以运行java的bat文件
- (3) double-linked list: insque remque
- android4.0.4平台关闭自动休眠功能-基于x210ii开发板
- local index and global index [转]
- 揭开嵌入式C面试题背后的玄机
- Openstack虚拟机在计算节点之间迁移,挂载glusterfs的情况下
- select的使用
- JAVA的多态性
- Android开发笔记--操纵字符串中的字符
- jdbc批量导入数据