c/c++练习–17
来源:互联网 发布:卡盟系统官网源码 编辑:程序博客网 时间:2024/06/07 17:46
c/c++练习–17
- 习题来源:C语言经典编程282例
161.创建单向链表
创建一个简单的链表,并将这个链表中的数据输出到窗体上
#include <stdio.h> #include <stdlib.h>#include <string.h>struct List_my { int num; struct List_my* next;};struct List_my* create_list(void){ struct List_my *head,*next,*new_node; head = (struct List_my *)calloc(1,sizeof(struct List_my)); if(!head) exit(0); printf("please input a number:"); scanf("%d",&(head->num)); next = head; for(int i=0;i<4;i++){ new_node = (struct List_my *)calloc(1,sizeof(struct List_my)); printf("please input a number:"); scanf("%d",&(new_node->num)); new_node->next=NULL; next->next = new_node; next = new_node; } return(head);}int main(void){ struct List_my *head,*p; p=head = create_list(); while(p){ printf("%d ",p->num); head = p->next; free(p); p=head; } return(EXIT_SUCCESS);}
162.创建双向链表
本实例实现创建一个双向链表,并将这个链表中的数据输出到窗体上,输入要查找的学生姓名,将查找的学生姓名从链表中删除,并显示删除后的链表
#include <stdio.h> #include <stdlib.h>#include <string.h>struct List_my { char name[20]; struct List_my *next, *per;};struct List_my* create_list(void){ struct List_my *head,*next,*new_node; head = (struct List_my *)calloc(1,sizeof(struct List_my)); if(!head) exit(0); printf("please input a number:"); scanf("%s",&(head->name)); next = head; head->per = NULL; for(int i=0;i<4;i++){ new_node = (struct List_my *)calloc(1,sizeof(struct List_my)); printf("please input a number:"); scanf("%s",new_node->name); new_node->next=NULL; new_node->per = next; next->next = new_node; next = new_node; } return(head);}int find_name( struct List_my *head, char *n ){ struct List_my *pre=head, *next=head; while(strcmp(next->name, n)){ pre = next; next=pre->next; } pre->next = next->next; next->next->per = pre; free(next); return(0);}int main(void){ struct List_my *head,*p; p=head = create_list(); while(p){ printf("%s ",p->name); p = p->next; } find_name(head,"bc"); p=head; while(p){ printf("%s ",p->name); head = p->next; free(p); p=head; } return(EXIT_SUCCESS);}
163.创建循环链表
本实例实现创建一个循环链表,这里只创建一个简单的循环链表来演示链表的创建和输出方法
#include <stdio.h> #include <stdlib.h>#include <string.h>struct List_my { char name[20]; struct List_my *next;};struct List_my* create_list(void){ struct List_my *head,*next,*new_node; head = (struct List_my *)calloc(1,sizeof(struct List_my)); if(!head) exit(0); printf("please input a number:"); scanf("%s", head->name); next = head; for(int i=0;i<4;i++){ new_node = (struct List_my *)calloc(1,sizeof(struct List_my)); printf("please input a number:"); scanf("%s",new_node->name); new_node->next = head; next->next = new_node; next = new_node; } return(head);} int main(void){ struct List_my *head,*p,*n; p=head = create_list(); printf("%s ",p->name); p=p->next; while(p!=head){ printf("%s ",p->name); p=p->next; } p = head->next; while(p!=head){ n=p->next; free(p); p=n; } free(head); return(EXIT_SUCCESS);}
164.使用头插入法建立单链表
#include <stdio.h> #include <stdlib.h>#include <string.h>struct List_my { char name[20]; struct List_my *next;};struct List_my* create_list(void){ struct List_my *head,*next,*new_node; head = (struct List_my *)calloc(1,sizeof(struct List_my)); if(!head) exit(0); printf("please input a number:"); scanf("%s", head->name); next = head; next->next = NULL; for(int i=0;i<4;i++){ new_node = (struct List_my *)calloc(1,sizeof(struct List_my)); printf("please input a number:"); scanf("%s",new_node->name); new_node->next = next; next = new_node; } head = next; return(head);} int main(void){ struct List_my *head,*p,*n; p=head = create_list(); printf("%s ",p->name); p=p->next; while(p){ printf("%s ",p->name); p=p->next; } p = head->next; while(p){ n=p->next; free(p); p=n; } free(head); return(EXIT_SUCCESS);}
165.双链表逆序输出
创建一个指定节点数的双向链表,并将双链表中的节点数据逆序输出。
#include <stdio.h> #include <stdlib.h>#include <string.h>struct List_my { char name[20]; struct List_my *n,*p;};struct List_my* create_list(void){ struct List_my *head,*next,*new_node; head = (struct List_my *)calloc(1,sizeof(struct List_my)); if(!head) exit(0); printf("please input a number:"); scanf("%s", head->name); next = head; next->n = NULL; next->p = NULL; for(int i=0;i<4;i++){ new_node = (struct List_my *)calloc(1,sizeof(struct List_my)); printf("please input a number:"); scanf("%s",new_node->name); new_node->n = NULL; new_node->p = next; next->n = new_node; next = new_node; } return(head);} int main(void){ struct List_my *head,*p,*n; p=head = create_list(); p=p->n; while(p) { n=p; p =p->n; } p=n; while(p){ printf("%s ",p->name); p=p->p; } p = head->n; while(p){ n=p->n; free(p); p=n; } free(head); return(EXIT_SUCCESS);}
166.约瑟夫环
使用循环链表实现约瑟夫环,给定一组编号分别是4,7,5,9,3,2,6,1,8.报数初始值为4.
#include <stdio.h> #include <stdlib.h>#include <string.h>struct List_my { char num; struct List_my *n;};struct List_my* create_list(void){ struct List_my *head,*next,*new_node; int anum[9]={4,7,5,9,3,2,6,1,8}; head = (struct List_my *)calloc(1,sizeof(struct List_my)); if(!head) exit(0); next = head; next->num = *anum; next->n =head; for(int i=1;i<9;i++){ new_node = (struct List_my *)calloc(1,sizeof(struct List_my)); new_node->n = head; new_node->num = anum[i]; next->n = new_node; next = new_node; } return(head);} void Joseph(struct List_my *head, int num){ struct List_my *n,*cur; int i=0; cur = n = head; while(n != n->n){ if(i%4 == 3){ printf("\n%d ", n->num); cur->n = n->n; free(n); n = cur->n; i=0; } else{ i++; cur =n; n=n->n; } } }int main(void){ struct List_my *head,*p; p=head = create_list(); do{ printf("%d ",p->num); p=p->n; } while(p!=head); Joseph(head, 4); return(EXIT_SUCCESS);}
167.创建顺序表并插入元素
创建一个顺序表,在顺序表中插入元素,并输出到窗体上
#include <stdio.h>#include <stdlib.h>typedef struct { int num[20]; int length_l;}List_my,*pList_my;void insert(pList_my l, int num,int Id){ if(Id <0&&Id>l->length_l+1){ printf("wrong\n"); exit(0); } l->num[Id-1] = num; l->length_l++;}int main(void){ List_my l; insert(&l, 12,1); insert(&l, 13,2); insert(&l, 12,3); insert(&l, 13,4); for(int i=0;i<=l.length_l;i++) printf("%d ",l.num[i]); return(EXIT_SUCCESS);}
168.合并两个链表
将两个链表合并,合并后的链表为原来两个链表的连接,即将第二个链表直接连接到第一个链表的尾部,合成为一个链表
#include <stdio.h> #include <stdlib.h>#include <string.h>struct List_my { int num; struct List_my* next;};struct List_my* create_list(void){ struct List_my *head,*next,*new_node; head = (struct List_my *)calloc(1,sizeof(struct List_my)); if(!head) exit(0); printf("please input a number:"); scanf("%d",&(head->num)); next = head; for(int i=0;i<4;i++){ new_node = (struct List_my *)calloc(1,sizeof(struct List_my)); printf("please input a number:"); scanf("%d",&(new_node->num)); new_node->next=NULL; next->next = new_node; next = new_node; } return(head);}void com_list(struct List_my* l1,struct List_my *l2){ struct List_my *n=l1; if(l1 && l2){ while(n->next) n = n->next; n->next = l2; }}int main(void){ struct List_my *head1,*p,*head2; p=head1 = create_list(); printf("\n"); head2 = create_list(); com_list(head1,head2); while(p){ printf("%d ",p->num); head1 = p->next; free(p); p=head1; } return(EXIT_SUCCESS);}
169.单链表节点逆置
创建一个单链表,并将链表的节点逆置,将逆置后的链表输出在窗体上
#include <stdio.h> #include <stdlib.h>#include <string.h>struct List_my { int num; struct List_my* next;};struct List_my* create_list(void){ struct List_my *head,*next,*new_node; head = (struct List_my *)calloc(1,sizeof(struct List_my)); if(!head) exit(0); printf("please input a number:"); scanf("%d",&(head->num)); next = head; for(int i=0;i<4;i++){ new_node = (struct List_my *)calloc(1,sizeof(struct List_my)); printf("please input a number:"); scanf("%d",&(new_node->num)); new_node->next=NULL; next->next = new_node; next = new_node; } return(head);}struct List_my* inv_list(struct List_my* l1){ struct List_my *n=l1, *pre=NULL,*p=l1; while(n){ p=n->next; n->next = pre; pre = n; n= p; } return(pre);}int main(void){ struct List_my *head1,*p; p=head1 = create_list(); p= head1=inv_list(head1); while(p){ printf("%d ",p->num); head1 = p->next; free(p); p=head1; } return(EXIT_SUCCESS);}
170.应用栈实现进制转换
应用栈实现进制转换,可以将十进制数转换为其他进制数
#include <stdio.h> #include <stdlib.h>#define LENGTH 100typedef struct stcak{ int len; int *top, *base;}sStack,*psStack;psStack create_list(void){ psStack head; head = (psStack)calloc(1,sizeof(sStack)); if(!head) exit(0); head->len = LENGTH; head->base = (int*)calloc(1, LENGTH); head->top = head->base; return(head);} int is_empty(psStack s){ if(s->base == s->top )return(1); return 0; } int is_full(psStack s){ if(s->top - s->base >= LENGTH) return(1); return 0; }void push(psStack s, int num){ if(!is_full(s)) *s->top++ = num; else printf("wrong\n");}int pop(psStack s){ if(is_empty(s)) printf("void\n"); else return(*s->top--);}void conv(psStack s, int num,int b){ while(num){ push(s, num%b); num /=b; } while(s->top != s->base ) printf("%d", *--s->top); }int main(void){ psStack s; int num, b; s = create_list(); printf("please input a num:"); scanf("%d %d", &num,&b); conv(s,num,b); return(EXIT_SUCCESS);}
0 0
- c/c++练习–17
- C练习
- C练习
- C 练习
- c练习
- C练习
- C练习
- c 练习
- c/c++练习–03
- c/c++练习–04
- c/c++练习–05
- c/c++练习–06
- c/c++练习–07
- c/c++练习–08
- c/c++练习–09
- c/c++练习–10
- c/c++练习–11
- c/c++练习–12
- Java多线程总结之线程安全队列Queue
- WebView加载html代码,js脚本中调用android对象的方法...
- 使用Octave音频处理(二):基本数学信号处理
- swift3.0 延迟执行
- 【BZOJ 1257】[CQOI2007]余数之和sum
- c/c++练习–17
- zookeeper数据结构及Znode类型
- 性能优化之内存泄露(Memory Leak)分析
- chrome浏览器在测试H5页面中的应用
- Elastic Search 学习概要
- android_设计模式——单例
- OkHttp解析json,使用方法
- Java iterator iterable comparable comparator等接口设计的区别
- java IO流的基本常用操作