线性表

本文介绍如何实现单链表的创建、打印及逆置等基本操作,并详细展示了双向链表的创建过程,包括节点的插入和删除等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                               线性表

 单链表逆置

//已知单链表H,写一个算法将其逆置

//H->head->32->63->18->50->26->NULL

#include <stdio.h>

#include <stdlib.h>

typedef struct node

{

    char data;                                                   //data 为结点数据信息

    struct node *next;                                            //next为指向后继结点的指针

}LNode;                                                           //单链表数据类型

LNode *CreateLinkList()                                             //生成单链表

{

    LNode *head,*p,*q;

    char x;

    head=(LNode *)malloc(sizeof(LNode));                           //生成头结点

    head->next=NULL;

    p=head;

    q=p;                                                          //q始终指向链尾结点

    printf("Input any charstring:\n");

    scanf("%c",&x);

    while(x!='\n')

    {

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

        p->data=x;

        p->next=NULL;

        q->next=p;                                                 //在链尾插入

        q=p;

        scanf("%c",&x);

    }

    return head;                                                   //返回指向单链表的头指针head

}

void Convert(LNode *H)                                             //单链表的逆置

{

    LNode *p,*q;

    p=H->next;                                            //p指向剩余结点链表的第一个数据结点

    H->next=NULL;                                                   //新链表H初始为空

    while(p!=NULL)

    {

        q=p;                                               //从剩余结点链表中取出第一个结点

        p=p->next;                               //p继续指向剩余结点链表新的第一个数据结点

        q->next=H->next;                              //将取出的结点*q插入到新链表H的链首

        H->next=q;

    }

}

void main()

{

    LNode *A,*p;

    A=CreateLinkList();                                              //生成单链表A

    Convert(A);                                                       //单链表逆置

    p=A->next;                                                       //输出逆置后的单链表

    while(p!=NULL)

    {

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

        p=p->next;

    }

    printf("\n");

}

                                     双链表合并

//对两个元素递增有序的单链表A和B,编写算法将A、B合并成一个按元素递减有序的单链表C,要求算法使用A\B中原有的结点,不允许增加新结点

#include <stdio.h>

#include <stdlib.h>

typedef struct node

{

    int data;                                                       //data为结点的数据信息

    struct node *next;                                           //next为指向后继结点的指针

}LNode;

LNode *CreateLinkList()                                                        //生成单链表

{

    LNode *head,*p,*q;

    int i,n;

    head=(LNode *)malloc(sizeof(LNode));                                       //生成头结点

    head->next=NULL;

    p=head;

    q=p;                                                           //指针q始终指向链尾结点

    printf("Input length oflist:\n");

    scanf("%d",&n);                                                         //读入节点数据

    printf("Input data of list:\n");

    for(i=1;i<=n;i++)                                                  //生成链表的数据结点

    {

        p=(LNode *)malloc(sizeof(LNode));                                 //申请一个节点空间

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

        p->next=NULL;

        q->next=p;                                                              //在链尾插入

        q=p;

    }

    return head;                                                    //返回单链表的头指针head

}

void Merge(LNode *A,LNode*B,LNode **C)                  //将升序链表A\B合并成降序链表C

{

    LNode *p,*q,*s;

    p=A->next;                                 //p始终指向链表A的第一个未比较的数据结点

    q=B->next;                                //q时钟指向链表B的第一个未比较的数据节点

    *C=A;                                                  //生成的链表的*C的头结点

    (*C)->next=NULL;

    free(B);                                                //回收链表B的头结点空间

    while(p!=NULL&&q!=NULL)              //将A、B两链表中当前比较节点中值小者赋给*S

    {

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

        {

            s=p;

            p=p->next;

        }

        else

        {

            s=q;

            q=q->next;

        }

        s->next=(*C)->next;                    //用头插法将结点*S插到链表*C的头结点之后

        (*C)->next=s;

    }

    if(p==NULL)                         //如果指向链表A的指针*P为空,则使*P指向链表B

    p=q;

    while(p!=NULL)                   //将*P所指链表中剩余结点依次摘下插入的链表C的链首

    {

        s=p;

        p=p->next;

        s->next=(*C)->next;

        (*C)->next=s;

    }

}

void print(LNode *p)                                                   //输出单链表

{

    p=p->next;

    while(p!=NULL)

    {

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

        p=p->next;

    }

    printf("\n");

}

void main()

{

    LNode *A,*B,*C;

    printf("Input data of list:\n");

    A=CreateLinkList();                                             //生成单链表A

    printf("Output list A:\n");

    print(A);                                                                  //输出单链表A

    printf("Input data of B:\n");

    B=CreateLinkList();

    printf("Output list of B:\n");

    print(B);

    printf("Make list C:\n");

    Merge(A,B,&C);                                   //将升序链表A、B合并成降序链表C

    printf("Output list of C:\n");

    print(C);

}

                                    静态链表

#include <stdio.h>

#include <stdlib.h>

#define MAXSIZE 30

typedef struct

{

    char data;                                                   //data为结点数据信息

    int cursor;                                                  //cursor 标示直接后继结点

}SNode;

void InsertList(SNode L[],inti,char x)                          //在静态链表中插入元素

{

    int j,j1,j2,k;

    j=L[0].cursor;                                              //j指向第一个数据结点

    if(i==1)                                                    //作为第一个数据结点插入

    {

        if(j==0)                                                 //静态链表为空

        {

            L[1].data=x;                                           //将x放入结点L[1]中

            L[0].cursor=1;                              //头指针cursor指向这个新插入的结点

            L[1].cursor=0;                                           //置链尾标示

        }

        else

        {

            k=j+1;

            while(k!=j)                                //在数组中循环查找存放x的位置

            if(L[k].cursor==-1)                                      //找到空位置

            break;

            else

            k=(k+1)%MAXSIZE;                                          //否则查找下一个位置

            if(k!=j)                                   //在数组中查找一个空位子来存放x

            {

                L[k].data=x;

                L[k].cursor=L[0].cursor;                            //将其插入到静态链表表头

                L[0].cursor=k;

            }

            else

            printf("Listoverflow!\n");                                 //链表已满无法插入

        }

    }

    else                                               //不是作为第一个结点插入时

    {

        k=0;

        while(k<i-2&&j!=0)               //查找第i-1个结点,j不等于0则表示未到链尾

        {

            k++;

            j=L[j].cursor;

        }

        if(j==0)              //查完整个静态链表未找到第i-1个结点,即链表长度小于i-1个结点

        printf("Insert error\n");

        else

        {

            j1=j;                                                        //找到第i-1个结点

            j2=L[j].cursor;           //用j2保存原L[j].cursor值,此值为第i个结点的位置值

            k=j+1;

            while(k!=j)                                 //在数组中循环查找存放x的位置

            if(L[k].cursor==-1)                                           //找到空位置

            break;

            else

            k=(k+1)%MAXSIZE;                            //否则查找下一个位置

            if(k!=j)                                   //在数组中找到一个空位置来存放x

            {

                L[k].data=x;

                L[j1].cursor=k;                             //作为第i个结点链入到静态链表

                L[k].cursor=j2;                          //新结点之后再连接上原第i个结点

            }

            else

            printf("Listoverflow!\n");                                   //链表已满,无法插入

        }

    }

}

void print(SNode *L)                                                      //输出静态链表

{

    int i;

    i=L[0].cursor;                                        //从静态链表的表头元素开始输出

    while(i!=0)

     {

         printf("%c",L[i].data);

         i=L[i].cursor;

     }

     printf("\n");

}

void main()

{

    SNode L[MAXSIZE];

    int i;

    char x;

    for(i=1;i<MAXSIZE;i++)                                                 //静态链表初始化

    L[i].cursor=-1;

    L[0].cursor=0;                                                //静态链表初始为空标志

    i=1;

    printf("Input element oflist\n");                                        //建立静态链表

    scanf("%c",&x);

    while(x!='\n');

    {

        InsertList(L,i,x);

        i++;

        scanf("%c",&x);

    }

    printf("Input site and element ofinsert\n");

    scanf("%d.%c",&i,&x);                                        //输入要插入的元素的位置

    InsertList(L,i,x);                                             //在静态链表中插入元素

    printf("Output list:\n");

print(L);                                                                //输出静态链表

}

                         链表的基本运算

#include <stdio.h>

#include <stdlib.h>

 

typedef struct node

{

    char data;                                      //data为结点数据信息

    struct node *next;                              //next为指向后继结点的指针

}LNode;                                              //单链表结点类型

LNode *CreateLinkList()                             //在表尾生成单链表

{

    LNode *head,*p,*q;

    char x;

    head=(LNode *)malloc(sizeof(LNode));              //生成头结点

    head->next=NULL;

    p=head;

    q=p;                                              //指针q始终指向尾结点

    printf("Input any charstring:\n");

    scanf("%c",&x);                                    //读入结点数据

    while(x!='\n')                                        //生成链表的其他结点

    {

        p=(LNode *)malloc(sizeof(LNode));                  //生成待插入结点的存储空间

        p->data=x;                                          //将读入的数据赋给待插入结点*p

        p->next=NULL;                            //待插入结点*p作为连尾结点时其后继结点为空

        q->next=p;                                            //在链尾插入*p结点

        q=p;                                                  //q继续指向新的链尾节点*p

        scanf("%c",&x);

    }

    return head;                                              //返回链表表头指针

}

int Length_LinkList(LNode*head)                                //求单链表的长度

{

    LNode *p=head;                                             //p指向单链表头结点

    int i=0;                                                    //i为结点计数器

    while(p->next!=NULL)

    {

        p=p->next;

        i++;

    }

    return i;

}

LNode *Get_LinkList(LNode*head,int i)

{                                                   //在单链表head中按序号查找第i个结点

    LNode *p=head;

    int j=0;

    while(p!=NULL&&j<i)                                     //由第一个数据结点开始查找

    {

        p=p->next;

        j++;

    }

    return p;                 //找到则返回指向i结点的指针值,找不到则p已为空返回空值

}

LNode *Locate_LinkList(LNode*head,char x)

{                                                    //在单链表中查找结点值为x的元素

    LNode *p=head->next;

    while(p!=NULL&&p->data!=x)                           //由第一个数据结点开始查找

    p=p->next;

    return p;

}

void Insert_LinkList(LNode*head,int i,char x)

{                                              //在单链表head的第i个位置上插入值为x的元素

    LNode *p,*s;

    p=Get_LinkList(head,i-1);                                         //查找第i-1个结点

    if(p==NULL)

    printf("Error!\n");                                 //第i-1个位置不存在而无法插入

    else

        {

 

            s=(LNode*)malloc(sizeof(LNode));                           //申请节点空间

            s->data=x;

            s->next=p->next;

            p->next=s;

        }

}

void Del_LinkList(LNode*head,int i)                  //删除单链表head上的第i个数据结点

{

    LNode *p,*s;

    p=Get_LinkList(head,i-1);                                       // 查找第i-1个结点

    if(p==NULL)

   printf("第i-1个结点不存在!\n");     //待删除结点的前一个节点不存在,也就没有待删结点

    else

       if(p->next==NULL)

       printf("第i个结点不存在!\n");                                  //待删结点不存在

       else

           {

               s=p->next;                                              //s指向第i个结点

               p->next=s->next;                               //从链表中删除第i各节点

               free(s);                                   //系统回收第i个结点的存储空间

           }

}

void print(LNode *h)                                                     //输出单链表

{

    LNode *p;

    p=h->next;

    while(p!=NULL)

    {

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

        p=p->next;

    }

    printf("\n");

}

void main()

{

    LNode *h,*p;

    int i;

    char x;

    h=CreateLinkList();                                             //生成一个单链表

    print(h);                                                        //输出单链表

    i=Length_LinkList(h);                                               //求单链表长度

    printf("Length=%d\n",i);                                             //输出单链表长度之

    printf("Input order and search toelement:\n");

    scanf("%d",&i);                                            //输入要查找元素的序号

    p=Get_LinkList(h,i);                                     //按序号在顺序表中查找

    if(p!=NULL)

    printf("Element is%c\n",p->data);                         //找到则输出该元素值

    else

      printf("search fail!\n");                                         //未找到

      printf("Input value of element andsearch to element:\n");

      getchar();

      scanf("%c",&x);                                               //输入要查找的元素的值

      p=Locate_LinkList(h,x);                                        //按值在顺序表中查找

      if(p!=NULL)

      printf("Element is%c\n",p->data);

      else

          printf("Search fail!\n");

          printf("Insert a element ,Inputsite and value of element:\n");

         scanf("%d,%c",&i,&x);                      //输入要查找元素的位置值i和元素值x

      Insert_LinkList(h,i,x);                                   //在单链表中插入该元素

      print(h);                                                                //输出单链表

      printf("Delete a element ,Inout siteof element:\n");

      scanf("%d",&i);                                            //输入要删除元素的位置值i

      Del_LinkList(h,i);                               //在单链表中删除该位置的元素值

      print(h);                                                        //输出单链表

}

                            双向链表的基本运算

#include <stdio.h>

#include <stdlib.h>

typedef struct dlnode

{

    char data;                                                //data为结点的数据信息

    struct dlnode *prior,*next;        //prior和next分别为指向直接前驱和后继结点的指针

}DLNode;

DLNode*CreateDlinkList()                                //建立带头结点的双向链表

{

    DLNode *head,*s;

    char x;

    head=(DLNode *)malloc(sizeof(DLNode));         //先生成仅含头结点的空双向循环链表

    head->prior=head;

    head->next=head;

    printf("Input any charstring:\n");

    scanf("%c",&x);                                                //读入结点数据

    while(x!='\n')                                        //采用头插法生成双向循环链表

    {

        s=(DLNode*)malloc(sizeof(DLNode));                 //生成待插入节点的存储空间

        s->data=x;                                       //将读入的数据赋给待插入结点*s

        s->prior=head;                        //新插入的节点*s其前驱结点为头结点*head

        s->next=head->next;                  //插入后*s的后继结点为头结点*head的后继结点

        head->next->prior=s;                        //头结点的原后继结点其前驱结点为*s

        head->next=s;                                         //头结点此时新的后继结点为*s

        scanf("%c",&x);                                    //继续读入下一个节点数据

    }

    return head;                                                       //返回头指针

}

DLNode *Get_DLinkList(DLNode*head,int i)            //在单链表head中按序号查找第i个结点

{

    DLNode *p=head;

    int j=0;

   while(p->next!=head&&j<i)                                //由第一个数据结点开始查找

    {

        p=p->next;

        j++;

    }

    if(p->next!=head)

    return p;                                           //找到则返回指向i结点的指针值

    else

        return NULL;                                              //找不到则返回空值

}

Void Insert_DLinkList(DLNode*head,int i,char x) //在单链表head的第i个位置插入值为x的元素

{

    DLNode *p,*s;

    p=Get_DLinkList(head,i-1);                                    //查找第i-1个结点

    if(p==NULL)

    printf("Error!\n");                            //第i-1个结点位置不存在而无法插入

    else

       {

           s=(DLNode *)malloc(sizeof(DLNode));

           s->data=x;                                                 //申请节点空间

           s->prior=p;                             //新插入的节点*s其前驱结点为*p

           s->next=p->next;                   //插入后*s的后继结点为*p原来的后继结点

           p->next->prior=s;                     //*p原后继结点此时的前驱结点为*s

           p->next=s;                                  //插入后*p的后继结点为*s

       }

}

void Del_DLinkList(DLNode *head,inti)             //删除单链表head上的第i个数据结点

{

    DLNode *p;

    p=Get_DLinkList(head,i);                                    //查找第i各节点

    if(p==NULL)

    printf("第i个数据节点不存在!\n");                       //待删节点不存在

    else

        {

             p->prior->next=p->next;       //待删结点*p的前驱结点其后继指针指向*p的后继结点

            p->next->prior=p->prior;      //待删结点*p的后继结点其前驱指针指向*p的前驱结点

            free(p);

        }

}

void print(DLNode *h)                                   //后向输出双向循环链表

{

    DLNode *p;

    p=h->next;

    while(p!=h)

    {

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

        p=p->next;

    }

    printf("\n");

}

void print2(DLNode *h)                                       //前向输出双向循环链表

{

    DLNode *p;

    p=h->prior;

    while(p!=h)

    {

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

        p=p->prior;

    }

    printf("\n");

}

void main()

{

    DLNode *h,*p;

    int i;

    char x;

    h=CreateDlinkList();                                   //建立带头结点的双向循环链表

    printf("Output list for next\n");

    print(h);

    printf("Output list forprior\n");

    print2(h);

    printf("Input order and search toelement:\n");

    scanf("%d",&i);                                       //输入要查找的元素的序号

    p=Get_DLinkList(h,i);                                    //按序号在顺序表中查找

    if(p!=NULL)

    printf("Element is%c\n",p->data);                     //找到则输出该元素的值

    else

     printf("Search fail\n");                                       //未找到

     printf("Insert a elenment,Input siteand value of element:\n");

     scanf("%d,%c",&i,&x);                            //输入要插入元素的位置值i和元素值x

     Insert_DLinkList(h,i,x);                              //在双向链表中插入该元素

     print(h);

     printf("Delete a element ,Input siteof element:\n");

     scanf("%d",&i);                                   //输入要删除元素的位置值i

     Del_DLinkList(h,i);                             //在单链表中删除该位置上的元素

     print(h);

}

                                          顺序表合并

//有顺序表A和B,其中元素均按从小到大的顺序排列。编写一个算法将他们合并成一个顺序表C,并且也按照从小到大的顺序排列

#include <stdio.h>

#include <stdlib.h>

#define MAXSIZE 20

typedef struct

{

    int data[MAXSIZE];                                             //存储顺序表中的元素

    int len;                                                               // 顺序表的表长

}SeqList;                                                                   //顺序表的类型

SeqList *Init_SeqList()                                         //顺序表的初始化

{

    SeqList *L;

    L=(SeqList *)malloc(sizeof(SeqList));

    L->len=0;

    return L;                                                 //返回指向顺序表的指针

}

void CreateList(SeqList**L)                                                //生成顺序表

{

    int i,n;

    printf("Input length oflist:");

    scanf("%d",&n);

    printf("Input element ofList:\n");

    for(i=1;i<=n;i++)

    scanf("%d",&(*L)->data[i]);

    (*L)->len=n;

}

void Merge(SeqList *A,SeqList*B,SeqList **C)     //将两个升序的顺序表合并成一个顺序表

{

    int i=1,j=1,k=1;

    if(A->len+B->len>=MAXSIZE)

    printf("Error!\n");

    else

    {

        *C=(SeqList *)malloc(sizeof(SeqList));

        while(i<=A->len&&j<=B->len)

        if(A->data[i]<B->data[j])

        (*C)->data[k++]=A->data[i++];

        else

        (*C)->data[k++]=B->data[j++];

        while(i<=A->len)                                                   //当表A未复制完

        (*C)->data[k++]=A->data[i++];

        while(j<=B->len)                                               //当表B未复制完

        (*C)->data[k++]=B->data[j++];

        (*C)->len=k-1;                                                       //存储表长

    }

}

void print(SeqList *L)                                                       //输出顺序表

{

    int i;

    for(i=1;i<=L->len;i++)

    printf("%4d",L->data[i]);

    printf("\n");

}

void main()

{

    SeqList *A,*B,*C;

    A=Init_SeqList();                                              //顺序表A的初始化

    printf("Creat List A:\n");

    CreateList(&A);                                                         //生成顺序表A

    printf("Output list A:\n");

    print(A);

    B=Init_SeqList();

    printf("Creat list B:\n");

    CreateList(&B);

    printf("Output list B:\n");

    print(B);

    C=Init_SeqList();

    printf("Merge list A and B TOC:\n");

    Merge(A,B,&C);                             //将两个顺序表A和B合并为一个生序表C

    printf("Output list C:\n");

    print(C);

}

                                顺序表的基本实现

#include <stdio.h>

#include <stdlib.h>

#define MAXSIZE 20

typedef struct

{

    int data[MAXSIZE];                     //存储顺序表中的元素

    int len;                               //顺序表的长

}SeqList;                                  //顺序表的类型

SeqList *Init_SeqList()                    //顺序表初始化

{

    SeqList *L;

    L=(SeqList*)malloc(sizeof(SeqList));

    L->len=0;

    return L;

}

void CreatList(SeqList**L)                 //建立顺序表

{

    int i,n;

    printf("Input length of List:");

    scanf("%d",&n);

    printf("Input element ofList:\n");

    for(i=1;i<=n;i++)

   scanf("%d",&(*L)->data[i]);

    (*L)->len=n;

}

void Insert_SeqList(SeqList*L,int i,int x)       //在顺序表中插入元素

{

    int j;

    if(L->len==MAXSIZE-1)                           //表满

    printf("This List is full!\n");

    else

    if(i<1||i>L->len+1)                             //插入位置非法

    printf("The position isnvalid!\n");

    else

    {

        for(j=L->len;j>=i;j--)              //将an~ai顺序后移一个元素位置

        L->data[j+1]=L->data[j];

        L->data[i]=x;                        //插入x到第i个位置

        L->len++;                              //表长增1

    }

}

void Delete_SeqList(SeqList*L,int i)            //在顺序表中删除元素

{

    int j;

    if(L->len==0)                                  //表为空

    printf("The List is empt!\n");

    else

    if(i<12||i>L->len)                                //删除位置非法

    printf("The position isnvalid!\n");

    else

       {

           for(j=i+1;j<=L->len;j++)              //将ai+1~an顺序前移一个位置实现对ai的删除

           L->data[j-1]=L->data[j];

           L->len--;                                    // 表长减一

       }

}

int Location_SeqList(SeqList*L,int x)                     //在顺序表中查找元素

{

    int i=1;                                              // 从第一个元素开始查找

    while(i<L->len&&L->data[i]!=x)                  //顺序表未查完且当前元素不是要查找的元素

    i++;

    if(L->data[i]==x)

    return i;                                                //找到则返回其位置值

    else

       return 0;                                             //未找到则返回0

}

  void print(SeqList *L)                                        //顺虚标的输出

  {

      int i;

      for(i=1;i<=L->len;i++)

      printf("%4d",L->data[i]);

      printf("\n");

  }

  void main()

  {

      SeqList *s;

      int i,x;

      s=Init_SeqList();                                             //顺序表的初始化

      printf("Creat List:\n");

      CreatList(&s);                                                  //建立顺序表

      printf("Output list:\n");

      print(s);                                                 //输出所建立的顺序表

      printf("Input element and site ofinsert:\n");

     scanf("%d%d",&x,&i);                               //输入要插入的元素的值和位置值

      Insert_SeqList(s,i,x);                               //将元素插入到顺序表中

      printf("Output list:\n");

      print(s);                                             //输出插入元素后的顺序表

      printf("Input element site ofdelete:\n");

      scanf("%d",&i);                                      //输入要删除的元素的位置值

      Delete_SeqList(s,i);                                 //删除顺序表中第i个位置的值

      printf("Output list:\n");

      print(s);                                               //输出删除元素后的顺序表

      printf("Input element value oflocation:\n");

      scanf("%d",&x);                                           //输入要查找德元素的值

      i=Location_SeqList(s,x);                         //定位要查找元素在顺序表中的位置哦

      printf("element %d site is%d\n",x,i);                      //输出该元素的元素值

  }

                                   线性表的逆置

//已知线性表A的长度为n,试写出将该线性表逆置的算法

#include <stdio.h>

#include <stdlib.h>

#define MAXSIZE 20

typedef struct

{

    int data[MAXSIZE];                                           //存储顺序表中的元素

    int len;                                                      //顺序表的表长

}SeqList;                                                        //顺序表类型

SeqList *Init_SeqList()                                             //顺序表初始化

{

    SeqList *L;

    L=(SeqList *)malloc(sizeof(SeqList));

    L->len=0;

    return L;                                                     //返回指向顺序表的指针

}

void CreatList(SeqList**L)                                        //生成顺序表

{

    int i,n;

    printf("Input length of List:");

    scanf("%d",&n);

    printf("Input element ofList:\n");

    for(i=1;i<=n;i++)

   scanf("%d",&(*L)->data[i]);

    (*L)->len=n;

}

void Coverts(SeqList *A)                                             //将顺序表逆置

{

    int i,n;

    int x;

    n=A->len;                                                       //n为线性表*A的长度

    for(i=1;i<=n/2;i++)                                               //实现逆置

    {

        x=A->data[i];

        A->data[i]=A->data[n-i+1];

        A->data[n-i+1]=x;

    }

}

void print(SeqList *L)                                               //输出顺序表

{

    int i;

    for(i=1;i<=L->len;i++)

    printf("%4d",L->data[i]);

    printf("\n");

}void main()

{

    SeqList *A;

    A=Init_SeqList();                                                 //顺序表初始化

    printf("Creat List A:\n");

    CreatList(&A);                                                   //生成顺序表

    printf("Output list A:\n");

    print(A);                                                           //输出顺序表

    printf("Covert list A:\n");

    Coverts(A);                                                      //将顺序表中的元素逆置

    printf("Output list A:\n");

    print(A);                                                         // 输出逆置后的顺序表

}

                               约瑟夫问题

//约瑟夫问题:设有N个人围成一圈并顺序编号为1~N。由编号为K的人进行1到M的报数,数到M的人出圈。接着再从他的下一个人重新开始1到M的报数

//,直到所有的人都出圈为止。输出出圈人的出圈次序

#include <stdio.h>

#include <stdlib.h>

typedef struct node

{

    char data;                                             //data 为节点的数据信息

    struct node *next;                                   //next为指向后继结点的指针

}LNode;

void Josephus(int n,int m,intk)

{

    LNode *p,*q;

    int i;

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

    q=p;

    for(i=1;i<n;i++)                                    //从编号k开始建立一个链表

    {

        q->data=k;

        k=k%n+1;

        q->next=(LNode*)malloc(sizeof(LNode));

        q=q->next;

    }

    q->data=k;

    q->next=p;                          //链接成循环单链表,此时p指向编号为k的结点

    while(p->next!=p)                                //当循环单链表中结点数不为1时

    {

        for(i=1;i<m;i++)

        {

            q=p;

            p=p->next;

        }                               //p指向报数为m的结点,q指向报数为m-1的结点

        q->next=p->next;                                      //删除报数为m的结点

       printf("%4d",p->data);                                 //输出出圈人的编号

        free(p);                                               //释放被删结点的空间

        p=q->next;                                             //p指向新的开始报数结点

    }

    printf("%4d",p->data);                                       //输出最后出圈人的编号

}

void main()

{

    int n,m,k;

    printf("Please inputn,m,k:\n");             //输入总人数n、报数个数m和起始报数人序号k

   scanf("%d,%d,%d",&n,&m,&k);

    Josephus(n,m,k);

    printf("\n");

}

                      在表头插入生成单链表

#include <stdio.h>

#include <stdlib.h>

 

typedef struct node

{

    char data;                                        //data为节点的数据信息

    struct node *next;                                 //next为指向后继结点的指针

}LNode;                                                 //单链表结点类型

void CreateLnkList(LNode**head)                          //在表头生成单链表

{                                //将主调函数中指向待生成单链表的指针地址传给**head

    char x;

    LNode *p;

    *head=(LNode *)malloc(sizeof(LNode));                  //在主调函数空间生成链表头结点

    (*head)->next=NULL;                                     //*head为链表头结点

    printf("Input any charstring:\n");

    scanf("%c",&x);                           //节点的数据域为char型,读入节点数据

    while(x!='\n')                                           //生成链表的其他节点

    {

        p=(LNode *)malloc(sizeof(LNode));                    //申请一个结点空间

        p->data=x;

        p->next=(*head)->next;               //将头结点的next值赋给新结点*p的next

        (*head)->next=p;              //头结点的newxt指针指向新结点*p实现在头结点插入

        scanf("%c",&x);                                       //继续生成下一个新结点

    }

}

 

void main()

{

    LNode *h,*p;

    CreateLnkList(&h);                                        //在表头生成单链表

    p=h->next;                                                //输出单链表

    while(p!=NULL);

    {

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

        p=p->next;

    }

    printf("\n");

}

                              在表尾插入

#include <stdio.h>

#include <stdlib.h>

 

typedef struct node

{

    char data;                                  //data为结点的数据信息

    struct node *next;                           //next为指向后继结点的指针

}LNode;                                           //单链表结点类型

LNode *CreateLinkList()                            //在表尾生成单链表

{

    LNode *head,*p,*q;

    char x;

    head=(LNode *)malloc(sizeof(LNode));            //生成头结点

    head->next=NULL;

    p=head;

    q=p;                                            //指针q始终指向链尾结点

    printf("Input any charstring:\n");

    scanf("%c",&x);

    while(x!='\n')                                    //生成链表其他结点

    {

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

        p->data=x;

        p->next=NULL;

        q->next=p;                                     //在链尾插入

        q=p;

        scanf("%c",&x);

    }

    return head;                                     //返回单链表头指针

}

void main()

{

    LNode *hian,*p;

    h=CreateLinkList();                             //在表尾生成单链表

    p=h->next;                                       //输出单链表

    while(p!=NULL)

    {

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

        p=p->next;

    }

    printf("\n");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值