#include <stdio.h>
#include <stdlib.h>
typedef struct node_x {
int data;
struct node_x *next;
}qnode;
typedef struct queue_x {
qnode *head;
qnode *rear;
}queue;
void print_queue(queue *q)
{
printf("the queue:\n");
qnode *p = q->head;
while (p) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int length_queue(queue *q)
{
int len = 0;
qnode *p = q->head;
while (p) {
len++;
p = p->next;
}
return len;
}
int delete_qnode(queue *q)
{
qnode *p;
int data;
if (!q->head)
return;
data = q->head->data;
p = q->head->next;
free(q->head);
q->head = p;
if (!p) q->rear = NULL;
return data;
}
void insert_qnode(queue *q, int data)
{
qnode *p, *s;
s = (qnode*)malloc(sizeof(qnode));
s->data = data;
s->next = NULL;
if (!q->rear) {
q->head = s;
} else {
q->rear->next = s;
}
q->rear = s;
}
queue * creat_queue(void)
{
queue *q;
q = (queue *)malloc(sizeof(queue));
q->head = NULL;
q->rear = NULL;
int data;
while (1) {
printf("insert a num:");
scanf("%d", &data);
if (data == 0) break;
insert_qnode(q, data);
}
return q;
}
int main(void)
{
printf("creat a queue:\n");
queue *q = creat_queue();
print_queue(q);
printf("queue length:%d\n", length_queue(q));
printf("delete a qnode :\n");
delete_qnode(q);
print_queue(q);
printf("insert a qnode 3:\n");
insert_qnode(q, 3);
print_queue(q);
return 0;
}
2008年10月17日星期五
队列:节点插入,节点删除,测长,建立
2008年10月16日星期四
单链表:打印,测长,建立,节点插入,节点删除,逆置
#include <stdio.h> #include <stdlib.h> typedef struct link { int data; struct link *next; }node; void print_link(node *head) { printf("the link:\n"); while (head) { printf("%d ", head->data); head = head->next; } printf("\n"); } int length_link(node *head) { int len = 0; while (head) { len++; head = head->next; } return len; } node * delete_node(node *head, int data) { node *p, *q; if (!head) return head; p = head; if (head->data == data) { head = head->next; free(p); return head; } while (p->next) { q = p->next; if (q->data == data) { p->next = q->next; free(q); return head; } p = q; } return head; } node *insert_node(node *head, int data) { node *p, *s, *q; s = (node*)malloc(sizeof(node)); s->data = data; if (!head) { head = s; head->next = NULL; return head; } p = head; q = NULL; while (p && data >= p->data) { q = p; p = p->next; } if (!q) { s->next = head; head = s; } else { q->next = s; s->next = p; } return head; } node * creat_link(void) { node *head = NULL; int data; while (1) { printf("insert a num:"); scanf("%d", &data); if (data == 0) break; head = insert_node(head, data); } return head; } node * reverse_link(node *head) { node *p, *q, *r; if (!head || !head->next) return head; p = head; q = head->next; while (q) { r = q->next; q->next = p; p = q; q = r; } head->next = NULL; return p; } int main(void) { printf("creat a link:\n"); node *head = creat_link(); print_link(head); printf("link length:%d\n", length_link(head)); printf("delete a node 3:\n"); head = delete_node(head, 3); print_link(head); printf("insert a node 3:\n"); head = insert_node(head, 3); print_link(head); printf("reverse link:\n"); head = reverse_link(head); print_link(head); return 0; }
2008年6月29日星期日
C笔记:键盘输入EOF
来源: 如何在stdin(键盘)中输入EOF呢
ctrl-d -- Unix terminal "End of File" (same as "exit" on many shells)
ctrl-z -- DOS "End of File"
ctrl-d -- Unix terminal "End of File" (same as "exit" on many shells)
ctrl-z -- DOS "End of File"
2008年6月17日星期二
2008年5月23日星期五
短时间重连无法bind
来源:[C/C++] 解決Socket連續Bind同一個Port的問題
s 是 socket, int on 的內容設為 1
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on));
只需要在建立一個Socket之後,使用setsockopt function就可以達到,短時間內連續bind同一個port的功能了!
s 是 socket, int on 的內容設為 1
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on));
只需要在建立一個Socket之後,使用setsockopt function就可以達到,短時間內連續bind同一個port的功能了!
订阅:
博文 (Atom)