首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

C语言经典算法之单链表处理全集

C语言经典算法之单链表处理全集

/*谁有兴趣一起来丰富这个程序的的功能??*/
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
#define ELEMTP int
#define v (*p)
struct node
{
         ELEMTPdata;
         structnode *next;
};
struct node *p,*q,*s,*head;
int j=0,i,k;
main()
{
         intx,y,cord;
         voidoutlin(struct node *h);
         voidcreate();
         voidinsert(struct node *h,int x,int y);
         voiddeletes(struct node *h,int x);
         structnode *MaxCompare(struct node *h);
         structnode *MinCompare(struct node *h);
         intdelIterance(struct node *h);
         voidbatchInsert(struct node *h,int x);
         voidbatchDelete(struct node *h,int x,int y);
         voidCz(struct node*  h);
         voidXg(struct node * h);
         printf("建立链表,输入-999完成链表:\n");
         create();
         i=j;
         outlin(head);
         do{
                   printf("\n        主菜单        \n");
                   printf("     1   插入一个元素   \n");
                   printf("     2   删除一个元素   \n");
                   printf("     3   升序排序       \n");
                   printf("     4   降序排序       \n");
                   printf("     5   查找元素       \n");
                   printf("     6   修改元素       \n");
                   printf("     7   删除重复元素   \n");
                   printf("     8   批量加入元素   \n");
                   printf("     9   批量删除元素   \n");
                   printf("     0   结束程序运行   \n");
                   printf("-----------------------------------------\n");
                   printf("请输入您的选择(1, 2, 3, 4, 5, 6, 7, 8, 9, 0) ");
                   scanf("%d",&cord);
                   switch(cord)
                   {
                            case1:
                            {
                                     printf("请输入插入的位置 i:");
                                     scanf("%d",&x);
                                     printf("请输入插入的数据 y:");
                                     scanf("%d",&y);
                                     insert(head,x,y);
                                     i=j;
                                     outlin(head);
                            }break;
                            case2:
                            {
                                     printf("x=?");
                                     scanf("%d",&x);
                                     deletes(head,x);
                                     i=j;
                                     outlin(head);
                            }break;
                            case3:
                            {
                                     printf("链表由大到小是");
                                     s=MaxCompare(head);
                                     j=i;
                                     outlin(s);
                                     //outlin(head);
                            }break;
                            case4:
                            {
                                     printf("链表由大到小是");
                                     s=MinCompare(head);
                                     j=i;
                                     outlin(s);
                            }break;
                            case5:
                            {
                                     Cz(head);
                                     outlin(head);
                            }break;
                            case6:
                            {
                                     Xg(head);
                                     outlin(head);
                            }break;
                            case7:
                            {
                                     k=delIterance(head);
                                     i=i-k;
                                     j=i;
                                     outlin(head);
                            }break;
                            case8:
                            {
                                     printf("请输入插入的位置 i:");
                                     scanf("%d",&x);
                                     batchInsert(head,x);
                                     i=j;
                                     outlin(head);
                            }break;
                            case9:
                            {
                                     printf("请输入删除的起始位置 i:");
                                     scanf("%d",&x);
                                     printf("请输入删除的结束位置 y:");
                                     scanf("%d",&y);
                                     batchDelete(head,x,y);
                                     i=j;
                                     outlin(head);
                            }break;
                            case0:
                            {
                                     exit(0);
                            }break;
                   }
         }while(cord<=9&&cord>=0);
}
void outlin(struct node *h)
{
         p=h->next;
         while(p!=NULL)
         {
                   printf("data=%4d ",p->data);
                   p=p->next;
         }
         printf("\n输出结束\n\n");
}
void deletes(struct node *h,int x)//删除节点
{
         p=h;
         while(p->next!=NULL&&p->next->data!=x)             p=p->next;
         if(p->next==NULL)
                   printf("x不存在 !");
         else
         {
                   q=p->next;
                   p->next=q->next;
                   free(q);
                   --j;
         }
}
void insert(struct node *h,int x,int y)
{
         s=(structnode*)malloc(sizeof(struct node));
         s->data=y;
         q=h;p=h->next;
         while(p!=NULL&&p->data!=x)
         {
                   q=p;
                   p=p->next;
         }
         q->next=s;
         s->next=p;
         ++j;
}
void create()//建立链表
{
         intx;
         head=(structnode*)malloc(sizeof(struct node));
         head->next=NULL;
         p=head;
         printf("x=?");
         scanf("%d",&x);
         while(x!=-999)
         {
                   s=(structnode*)malloc(sizeof(struct node));
                   s->data=x;
                   s->next=NULL;
                   p->next=s;
                   p=s;
                   printf("x=?");++j;
                   scanf("%d",&x);
         }
}
/////////////以下函数由七绝玩家编写/////////////
struct node *MaxCompare(struct node *h)//由大到小排序
{      
         structnode *t;
         intx;
         t=h;s=NULL;
         while(j!=0)
         {
         x=t->next->data;
         q=t->next;
         while(q!=NULL)
         {
                   if(q->data<=x)
                            x=q->data;
                   else
                            x=x;
                   q=q->next;        
         }
         p=t;
         while(p->next!=NULL&&p->next->data!=x)
                   p=p->next;
         q=p->next;
         p->next=q->next;
         t=p;t=h;
         p=q;p->next=s;
         s=p;
         j--;
         }
         t->next=s;
         head=t;
         return(t);
}
struct node *MinCompare(struct node *h)//由小到大排序
{
         structnode *t;
         intx;
         t=h;s=NULL;
         while(j!=0)
         {
         x=t->next->data;
         q=t->next;
         while(q!=NULL)
         {
                   if(q->data>=x)
                            x=q->data;
                   else
                            x=x;
                   q=q->next;        
         }
         p=t;
         while(p->next!=NULL&&p->next->data!=x)
                   p=p->next;
         q=p->next;
         p->next=q->next;
         t=p;t=h;
         p=q;p->next=s;
         s=p;
         j--;
         }
         t->next=s;
         head=t;
         return(t);
}
int delIterance(struct node *h)//删除重复元素
{
         intx,y=0;
         --j;
         s=h->next;
         while(j>0)
         {        
                   x=s->data;
                   p=s;
                   while(p->next!=NULL)
                   {
                            if(p->next==NULL)
                            {
                                     x=x;
                            }
                            elseif(p->next->data==x)
                            {
                                     q=p->next;
                                     p->next=q->next;
                                     free(q);
                                     --j;
                                     ++y;
                            }
                            else
                            {
                                     p=p->next;
                            }
                   }
                   s=s->next;
                   --j;
         }
         returny;
}
void batchInsert(struct node *h,int x)//批量加入
{
         inty=0;
         q=h;p=h->next;
         while(p!=NULL&&p->data!=x)
         {
                   q=p;
                   p=p->next;
         }
         printf("y=?");
         scanf("%d",&y);
         while(y!=-999)
         {
         s=(structnode*)malloc(sizeof(struct node));
         s->data=y;
         q->next=s;
         s->next=p;
         q=s;
         printf("y=?");
         scanf("%d",&y);
         ++j;
         }
}
void batchDelete(struct node *h,int x,inty)//批量删除
{
         intk=0,w=0;
         structnode *t;
         p=h;q=h;
         while(p->next!=NULL&&p->next->data!=x)
         {
                   ++k;
                   p=p->next;
         }
         while(q->next!=NULL&&q->next->data!=y)
         {
                   ++w;
                   q=q->next;
         }
         if(p->next==NULL||q->next==NULL)
                   printf("输入的位置不正确,请重新开始!");
         elseif(k<w)
         {
                   s=q->next;
                   while(p->next!=s)
                   {
                            t=p->next;
                            p->next=t->next;
                            free(t);
                            --j;
                   }


         }
         elseif(w=k)
         {
                   printf("没有删除元素");
         }
         else
         {
                   s=p->next;
                   while(q->next!=s)
                   {
                            t=q->next;
                            q->next=t->next;
                            free(t);
                            --j;
                   }
         }
}
//////////////七绝玩家编写结束//////////////
//////////////以下函数由lihk编写////////////
void Cz(struct node*  h)           //查找//
{
         structnode * num;int i;
         num=head;
         printf("输入您要查找的号码:");
         scanf("%d",&i);
         while(i!=num->data&& num->next!=NULL)
         {   
                   num=num->next;
         }
         if(i==num->data)
                 printf("号码:%d",num->data);
         else
                 printf("该号码不在链表里.");
}
void Xg(struct node * h) //修改//
{
         structnode * num; int i;
         num=head;
         printf("查找您要修改的号码:");
         scanf("%d",&i);
         while(i!=num->data&&num->next!=NULL){
                   num=num->next;}
         if(i==num->data)
         {
               printf(":%d\n",num->data);
               printf("输入您要修改的新信息:");
                   printf("号码:");
               scanf("%d",&num->data);
               printf("\n修改成功!");
     }
         else         
               printf("该号码不在链表里!");  
}
//////////////lihk编写结束/////////////////
返回列表