C语言算法实例代码.docx

上传人:b****7 文档编号:26670871 上传时间:2023-06-21 格式:DOCX 页数:43 大小:24.70KB
下载 相关 举报
C语言算法实例代码.docx_第1页
第1页 / 共43页
C语言算法实例代码.docx_第2页
第2页 / 共43页
C语言算法实例代码.docx_第3页
第3页 / 共43页
C语言算法实例代码.docx_第4页
第4页 / 共43页
C语言算法实例代码.docx_第5页
第5页 / 共43页
点击查看更多>>
下载资源
资源描述

C语言算法实例代码.docx

《C语言算法实例代码.docx》由会员分享,可在线阅读,更多相关《C语言算法实例代码.docx(43页珍藏版)》请在冰豆网上搜索。

C语言算法实例代码.docx

C语言算法实例代码

Formoreinformationpleasevisit:

在初学c语言算法是,很多人都会很苦恼,因为没有源代码可以供自己去实现,下面我为大家提供一些源代码,都是我自己实现过的,可以直接粘贴后用的。

 

/**以下源程序是用来构建链表,删除链表,

对链表排序,链接链表的,适用于刚学习数据

结构的编程爱好者**/

//头文件“Linklist.h”目的是构造结构体

#include

#include

typedefintelemtype;

typedefstructnode

{

 elemtypedata;

 structnode*next;

}Linklist;

//头文件“outputlist”目的是输出链表

voidoutputlist(Linklist*head)

{

 Linklist*p;

 p=head->next;

 printf("head");

 while(p){

  printf("->%d",p->data);

  p=p->next;

 }

 printf("\n");

}

// 头文件“creatlist.h”creatlist()的作用是建立一个带头结点的链表 

Linklist*creatlist(intn)

{

 Linklist*p,*q,*head;

 inti=0;

 p=(Linklist*)malloc(sizeof(Linklist));

 head=q=p;

 q->next=NULL;

 printf("Input%dvalues:

\n",n);

 while(i++

 {

  p=(Linklist*)malloc(sizeof(Linklist)); scanf("%d",&p->data);

  p->next=NULL;

  q->next=p;

  q=p;

 }

 returnhead;

}

//头文件“Inorder.h”,目的地将顺序表中的顺序按升序排列

voidinorder(Linklist*head)

{

 Linklist*p,*q;

 intmin,k;

 q=p=head->next;

 while(p)

 {

  min=p->data;

  q=p->next;

  while(q)

   {

    if(min>q->data)

     {

      k=min;

      min=q->data;

      q->data=k;

     }

     q=q->next;

   }

  p->data=min;

  p=p->next;

 } 

}

//头文件“uni.h”目的是链接两个链表

Linklist*uni(Linklist*heada,Linklist*headb)

{

 Linklist*p,*q;

 p=heada->next;

 q=headb->next;

 while(p->next)

 {

  p=p->next;

 }

 p->next=q;

 returnheada;

}

//下面是源文件

#include"Linklist.h"

#include"creatlist.h"

#include"outputlist.h"

#include"InOrder.h"

#include"uin.h"

// 函数deletesamenode(Linklist*head)的功能是删除具有相同数据的节点

voiddeletesamenode(Linklist*head)

{

 Linklist*p,*q,*r;

 p=q=r=head->next;

 while(p)

 {

  r=p;

  q=p->next;

      while(q) 

   {  

    if(p->data==q->data)

    {

     r->next=q->next;

     q=q->next; 

    }

    else

    {

     r=q;

     q=q->next;

    }

   }

   p=p->next;

 }

 

}

voidmain()

{

 Linklist*heada,*headb,*p,r;

 intn1,n2;

 p=&r;

 heada=&r;

 printf("Starttosetuplista\n");

 do{

  printf("Inputthelongthoflista:

\n");

  scanf("%d",&n1);

 }while(n1<1);

 heada=creatlist(n1);

 headb=&r;

 printf("Starttosetuplistb\n");

 do{

  printf("Inputthelongthoflistb:

\n");

  scanf("%d",&n2);

 }while(n2<1);

 headb=creatlist(n2);

 p=uni(heada,headb);

 printf("Thelistbeforeinorder:

\n");

 outputlist(p);

 inorder(p);

 printf("Thelistafterinorder:

\n");

 outputlist(p);

 deletesamenode(p);

 printf("Thelistafterhasdeletesamenodesis:

\n");

 outputlist(p);

 

}

 

 链表不像数组一样容易进行排序,因为链表中进行排序时,交换两个节点要涉及到这两个节点的前后节点的操作。

   下面是我写的一段函数,该函数用来在链表中进行排。

写的不好,但编译后能够实现该函数的功能。

linklist*inorder(linklist*head)

{

 linklist*p,*q,*r,*m,*n,*k,*i,*j;

   r=head;

 i=j=p=q=m=n=k=head->next;

 while(p)

 {

  n=m=q=p->next;//把m作为p的后驱

  i=p;//用i保留p原来的位置

  while(q)

  {

      k=q->next;//把k作为q的后驱

   j=q;//用j保留q原来的位置

   if(p->num>q->num)

   {

    if(q==p->next)//如果相邻两项交换

    {

       r->next=q;

     p->next=q->next;

     q->next=p;

    }

    else{//非相邻两项交换

     r->next=q;

     n->next=p;

     q->next=m;

     p->next=k;

    }

       j=p;

    i=q;

    

   }

    n=j;//把n作为q的前驱

    q=j->next;

  }

对于单链表的逆置代码如下:

#include

#include

//定义结构体

typedefstructnode

 intdata;

 structnode*next;

}linklist;

//链表的初始化,即构建链表

linklist*creatlist(intn)

{

 inti;

 linklist*p,*q,*head;

 head=(linklist*)malloc(sizeof(linklist));

 p=q=head;

 head->next=head;//既然是循环链表就应该在链表中无元素时h->head=head;

 printf("请输入%d个值:

\n",n);

 for(i=0;i

 {

  p=(linklist*)malloc(sizeof(linklist));

  q->next=p;

  p->next=NULL;

  q=p;

  scanf("%d",&p->data);

 }

 p->next=head;

 return(head);

}

//打印链表。

即链表的输出

voidoutputlist(linklist*head)

{

 linklist*p;

 p=head->next;

 printf("链表如下:

\n");

 printf("head");

 while(p!

=head)

 {

  printf("->%d",p->data);

  p=p->next;

 }

}

//逆置链表

linklist*invert(linklist*head)

{

 linklist*p,*q,*r;

 p=head->next;

 q=head;

 //q是p的前驱r是q的前驱

   while(p!

=head)

 {

  r=q;

  q=p;

  p=p->next;

  q->next=r;

 }  

  head->next=q;

  returnhead;

}

//主函数

voidmain()

{

 linklist*head;

 intn;

 printf("开始建立链表:

\n");

 printf("请输入链表的长度:

\n");

 scanf("%d",&n);

 head=creatlist(n);

 outputlist(head);

 printf("链表逆置后:

\n");

 head=invert(head);

 outputlist(head);

}

双向链表的综合运算-----基于C语言的算法

代码如下:

/************************************************************************/

/*要求:

       构建一个双向链表,实现插入,查找,和删除,输出的操作

 知识要点:

      双向链表的节点有两个指针域,其一指向直接后继,其二则指向直接前驱

双向链表节点的定义如下:

      typedef structdupnode

       {  

  ElemType data;

  structdupnode*next,*prior;

    }dulinklist;

      和单链表类似,双向链表通常也带头结点。

通过某节点的指针p即可直接得到它的

后继结点的指针p->next,也可以直接得到它的前驱指针p->prior.设p指向双向链表中的某

一结点,则p->prior->next表示的是*p结点之前驱指针的后继结点的指针,即与p相等;

类似,p->next->prior也与p相等。

      所以又一下等式:

          p->next->prior=p=p->prior->next

    双向链表的构造与单链表相同,至于它的插入与删除运算比单链表复杂,

插入运算需要4不操作,删除运算需要2不操作,注意语句的次序,不要任意交换位置,

以免不能正确插入或删除节点。

                                          */

/************************************************************************/

#include

#include

typedef intElemType;

typedefstructdupnode{

 ElemTypedata;

 structdupnode*next,*prior;

}duplinklist;

//构建单链表

duplinklist*create_duplinklist(intn)

{

 duplinklist*head,*p,*q;

 inti;

 head=(duplinklist*)malloc(sizeof(duplinklist));

 p=(duplinklist*)malloc(sizeof(duplinklist));

 printf("请输入%d个元素的值:

\n",n);

 scanf("%d",&p->data);

 head->next=p;

 p->prior=head;

 p->next=NULL;

 for(i=1;i

 {

  q=p;

  p=(duplinklist*)malloc(sizeof(duplinklist));

  scanf("%d",&p->data);

  q->next=p;

  p->prior=q;

  p->next=NULL;

 }

 

 returnhead;

}

//单链表的插入运算

duplinklist*insert_duplinklist(duplinklist*h,intlocate,ElemTypee )

{

     inti;

  duplinklist*head,*q,*p,*r;

  head=h;

  q=head->next;

  r=q->next;

  if(locate==1)  //如果插入的位置为第一个节点,进行如下操作

  {

   p=(duplinklist*)malloc(sizeof(duplinklist));

   p->data=e;

   head->next=p;

   p->prior=head;

   p->next=q;

   q->prior=p;

  }

  else

  {

   

      for(i=2;i!

=locate;i++)

   {

    q=q->next;

    r=q->next;

   }

   

     p=(duplinklist*)malloc(sizeof(duplinklist));

  p->data=e;

  q->next=p;

  p->prior=q;

  p->next=r;

  r->prior=p;

  }

      

  returnhead;

}//我在想有必要将插入位置分类吗?

思考题?

//删除运算

duplinklist*delete_duplinklist(duplinklist*h,intlocate)

{

 duplinklist*head,*q,*r;

 inti;

 head=h;

 q=head->next;

 r=q->next;

 if(locate==1)    //如果要删除的节点是第一个

 {

  printf("要删除的节点的位置是%d,元素值是:

%d\n",locate,q->data);

  head->next=r;

  r->prior=head;

 }

 else

 {

  for(i=1;i!

=locate;i++)

  {

   q=q->next;

   r=q->next;

  }

  printf("要删除的节点的位置是%d,元素值是:

%d\n",locate,q->data);

  q->prior->next=r;

  r->prior=q->prior;

  

 }

 free(q);

 returnhead;

}

//查找算法

voidfind_duplinklist(duplinklist*h,intlocate)

{

 duplinklist*head,*p;

 inti;

 head=h;

 p=head;

 for(i=0;i!

=locate;i++)

 {

  p=p->next;

 }

 printf("第%d处的元素的值是:

%d\n",locate,p->data);

}

//输出算法

voidoutput_duplinklist(duplinklist*h)

{

 duplinklist*head,*p;

 head=h;

 p=head->next;

 printf("head");

 while(p)

 {

  printf("->%d",p->data);

  p=p->next;

 }

     printf("->NULL\n");

}

//菜单选择函数

 voidmenu_select()

 {

 

 

 printf("======================================================\n");

 printf("                 欢迎使用                          \n");

 printf("   1--构建双向链表(第一次使用时必须先构建链表!

)\n");

 printf("   2--插入                                         \n");

 printf("   3--删除                                          \n");

 printf("   4--查找                                          \n");

 printf("   5--浏览全表                                      \n");

 printf("   6--退出                                          \n");

    printf("     请选择1-6                                      \n");

    printf("======================================================\n");

  

 }

 //主程序

 voidmain()

 {

 intselect,flag=0;

 intlocate,n;

 ElemTypee;

 duplinklist*head;

 while

(1){

  

  menu_select();

  scanf("%d",&select);

  if(select==1)

  {

   flag++;

  }

   system("cls");

   if(flag>=1)

   {

    

  switch(select)

  {

  case1:

printf("请输入链表的长度:

\n");

   scanf("%d",&n);

   head=create_duplinklist(n);

   break;

  case2:

 printf("请输入待插入的节点的位置:

\n");

   scanf("%d",&locate);

   printf("请输入待插入的元素值:

\n");

   scanf("%d",&e);

   head=insert_duplinklist(head,locate,e);

   break;

  case3:

    printf("请输入删除的节点的位置:

\n");

   scanf("%d",&locate);

   head=delete_duplinklist(head,locate);

   break;

  case4:

   printf("请输入待查找的节点位置:

\n");

   scanf("%d",&locate);

   find_duplinklist(head,locate);

   break;

  case5:

   printf("表中各节点值如下:

\n");

   output_duplinklist(head);

   break;

  case6:

exit(0);

   break;

  default:

printf("请重新选择:

\n");

   

  }

   }

  else

  {

   printf("第一次使用时必须构建链表:

\n");

  }

  

 }

 }

栈的顺序存储与操作代码如下:

 

#include

#include

#defineSTACK_INIT_SIZE  100

#defineSTACKINCREMENT 10

#defineSElemTypeint 

#defineOVERFLOW 2

#defineOK  1

#defineERROR0

typedefintStatus;

//定义栈的顺序存储结构

typedef  struct 

{

 SElemType *base,*top;

 intstacksize;

}SqStack;

//初始化顺序栈

StatusInitSqStack(SqStack&S)

{

 S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));

 if(!

S.base) 

 {

  printf("存储分配失败!

\n");

  exit(OVERFLOW);

 }

 S.top=S.base;

 S.stacksize=STACK_INIT_SIZE;

    returnOK;

}//InitSqstack

//入栈

StatusPush(SqStack&S,SElemType&e)

{

 if(S.top-S.base>=S.stacksize) //如果栈满,追加空间

 {

  S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));

  if(!

S.base)

   exit(OVERFLOW);//存储分配失败

  S.top=S.base+S.stacksize;

  S.stacksize+=STAC

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书 > 调解书

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1