| 
 
| 本帖最后由 Micro 于 2015-2-10 10:11 编辑 C语言大复习--查缺补漏(合计2220行经典小程序)。
 
 方便大家复习。贡献给大家了!   链接已贡献给了大家,在最下文。
 
 Ps:    感谢柳柳桑美女老师设置为直播贴
 
 
 
 复制代码/*
* 耶稣的叛徒
*/
#include <stdio.h>
void main()
{
        int a[10],i,j,m=0,n=10;
        for(i=0;i<10;i++)
                a=i+1;
        for(i=0;n>1;i++)
        {
                if(i==10)i=0;                        //范围
                if(a!=0)m++;                        //数数
                if(m==3){a=0;m=0;n--;}//归零
        }
        for(i=0;i<10;i++)
                if(a!=0)printf("%d",a);
}
/*
* 删除一个字符串中重复的字符只留下最后一个
*/
#include <stdio.h>
#include <stdio.h>
void main()
{
        char a[]="12332123523123";
        int i,j;
        for(i=0;a;i++)
                for(j=0;j<i;j++)
                {
                        if(a==a[j])
                        {
                         strcpy(a+i,a+i+1);
                         i--;
                        }
                }
         puts(a);
}
 
 
 复制代码/*
 * 链表综合应用
 */
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node
{
        int num;
        struct node *next;
};
struct node *crfun(int n)
{
        struct node *head,*p1,*p2;
        p1=p2=head=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&head->num);
        n--;
        while(n)
        {
                p2=p1;
                p1=(struct node *)malloc(sizeof(struct node));
                scanf("%d",&p1->num);
                p2->next=p1;
                n--;
        }
        p1->next=NULL;
        return head;
}
struct node *hbfun(struct node *p1,struct node *p2)
{
         struct node *head,*p;
         if(p1->num<p2->num)head=p1;
         else head=p2;
         p=NULL;
         while(p1!=NULL&&p2!=NULL)
         {
                 if(p1->num<p2->num)
                 {
                         p->next=p1;
                         p=p1;
                         p1=p1->next;
                 }
                 else
                 {
                         p->next=p2;
                         p=p2;
                         p2=p2->next;
                 }
         }
         //以下两句作用是把多余的部分连接起来
         if(p1!=NULL)p->next=p1;
         if(p2!=NULL)p->next=p2;
         return head;
}
struct node *delfun(struct node *head,int m)
{
        struct node *p1,*p2;
        p1=p2=head;
        if(m==head->num)
        {
                head=head->next;
                return head;
        }
        while(p2!=NULL&&p2->num!=m)
        {
                p1=p2;
                p2=p2->next;
        }
        if(p2->num==m)
        {
                p1->next=p2->next;
        }
        return head;
}
struct node *insfun(struct node *head,int m)
{
        struct node *p1,*p2,*p;
        p=p2=head;
        p1=(struct node *)malloc(sizeof(struct node));
        p1->num=m;
        if(m<=head->num)
        {
                p1->next=head;
                head=p1;
                return head;
        }
        while(m>=p2->num&&p2!=NULL)
        {
                p=p2;
                p2=p2->next;
        }
                p->next=p1;
                p1->next=p2;
        return head;
}
struct node *sortfun(struct node *head)
{
        struct node *p;
        p=head->next;
        head->next=NULL;
        while(p!=NULL)
        {
                head=insfun(head,p->num);
                p=p->next;
        }
        return head;
}
void putfun(struct node *head)
{
        while(head!=NULL)
        {
                printf("%3d",head->num);
                head=head->next;
        }
        printf("\n");
}
void main()
{
         struct node *head,*p1,*p2;
         int m;
         p1=crfun(5);                        //创建链表
         p2=crfun(5);                        //创建链表
         head=hbfun(p1,p2);        //合并链表
         scanf("%d",&m);                //(二路合并法)
         head=delfun(p1,m);                //删除节点
         scanf("%d",&m);
         head=insfun(p1,m);                //插入节点
         head=sortfun(p1);                //排序链表
         putfun(head);                        //输出链表
         //在实际应用中链表使用完后要记得释放
}
 
 
 | 
 |