链表基本操作

//双链表的基本操作
#include<stdio.h>
#include"DLinklist.h"
#include<stdlib.h>

void createDL1(DLinklist h)
 {
     getchar();
     DLinklist p;
     h->next=NULL;
     char d;
     printf("input :");
     while((d=getchar())!='#')
     {
         getchar();
         p=(DLinklist)malloc(sizeof(DLnode));
         p->alp=d;
         p->next=h->next;p->prior=h;
         h->next=p;
         if(h->next->next!=NULL) p->next->prior=p;
          printf("input :");
     }
 }
 void printDL(DLinklist h)
 {
     if(h->next==NULL) printf("empty");
     else {
        DLinklist p=h->next;
        while(p!=NULL)
        {
            printf("%c ",p->alp);
            p=p->next;
        }
     }
     printf("\n");
 }
void createDL2(DLinklist h)
{
     getchar();//不让选择时候的回车作为元素进入链表
     DLinklist p,q;
     h->next=NULL;//每次创建都重置
     q=h;
     char d;
     printf("input :");
     while((d=getchar())!='#')
     {
         getchar();
         p=(DLinklist)malloc(sizeof(DLnode));
         p->alp=d;p->next=NULL;
         q->next=p;p->prior=q;
         q=p;
          printf("input :");
     }
}
void insertDL(DLinklist h)
{
    getchar();
    char d;
    DLinklist p,q;
    p=(DLinklist)malloc(sizeof(DLnode));
    //p->next=NULL;p->prior=NULL;
    printf("input alp to inser:");
    d=getchar();
    p->alp=d;
    q=h->next;
    if(!q) {h->next=p;p->prior=h->next;p->next=NULL;}
    else{
        while(q->alp<p->alp&&q->next!=NULL) q=q->next;
        if(q->alp>=p->alp){q->prior->next=p;p->prior=q->prior;p->next=q;q->prior=p;}
        else {q->next=p;p->next=NULL;p->prior=q;}
        }
}
void delDL(DLinklist h)
{
    char c;
    getchar();
    printf("input the alp to delete:");
    c=getchar();
    DLinklist p;
    p=h->next;
    if(p==NULL) printf("empty linklist!\n");
    else {
        while(p->alp!=c&&p->next!=NULL) p=p->next;
        if(p->alp!=c) printf("%c is not in linklist",c);
        else { if(p->next==NULL) {p->prior->next=NULL;free(p);}
                else {p->prior->next=p->next;
                      p->next->prior=p->prior;free(p);}
             }
    }

}


单链表的基本操作#include<stdio.h>#include"linklist.h"#include<stdlib.h>void create1(Linklist head)//带头结点的头插法建立单链表{ getchar();//选择create后会有一个回车,必须用getchar接收 Linklist p; head->next=NULL; char c; printf("input:"); scanf("%c",&c); while(c!='#') { getchar(); p=(Linklist)malloc(sizeof(Lnode)); p->alp=c; p->next=head->next; head->next=p; printf("input:"); scanf("%c",&c); } printf("create success!\n");}void create2(Linklist p){ char c; getchar(); Linklist t,r; t=p; printf("input :"); while((c=getchar())!='#') { getchar(); r=(Linklist)malloc(sizeof(Lnode)); r->alp=c;r->next=NULL; t->next=r; t=r; printf("input :"); }}void print(Linklist p){ if(p->next==NULL) printf("empty linklist!"); else{ Linklist q; q=p->next; do{ printf("%c ",q->alp); q=q->next; }while(q!=NULL); } printf("\n");}void inser(Linklist p){ getchar(); char x; printf("input the alp:"); scanf("%c",&x); Linklist q,k,t; q=(Linklist)malloc(sizeof(Lnode)); (*q).alp=x;q->next=NULL; t=p; k=p->next; while(k!=NULL&&k->alp<x) {t=k;k=k->next;} t->next=q;q->next=k; printf("inser %c success\n",x);}void del(Linklist p){ char x; int n=0; getchar(); if(p->next==NULL) printf("no element in list\n") ; else { printf("input alp to delete:"); scanf("%c",&x); Linklist k,t; k=p;t=p->next; while(t!=NULL) { if(t->alp==x) {k->next=t->next;t=t->next;n++;} else {k=t;t=t->next;} } } if(n==0) printf("%c is not in\n",x);}void yuesofu(Linklist p)//约瑟夫环问题的链表处理{ Linklist q,r,t; q=p; while(q->next!=NULL) q=q->next; q->next=p->next; r=q=p->next; int k; k=1; printf("the sequence leve :"); while(r->next!=r) { r=q;q=q->next;k++; if(3==k){printf("%c ",q->alp);t=q;r->next=q->next; q=r->next;free(t);k=1;} } printf("the left is %c \n",r->alp);}void merger()//单链表合并{ Linklist h1,h2; h1=(Linklist)malloc(sizeof(Lnode)); h2=(Linklist)malloc(sizeof(Lnode));//不能h1=h2=()malloc(),会只开辟一个空间 h1->next=h2->next=NULL; printf("linklist 1:\n"); create2(h1); printf("linklist 2:\n"); create2(h2); Linklist p,q,r,t; p=h1->next;q=h2->next; r=h1; while(p&&q) { if(p->alp<q->alp) {r->next=p;r=r->next;p=p->next;} //else {r->next=q;r=r->next;q=q->next;}//相同元素重复出现 else if(p->alp>q->alp) {r->next=q;r=r->next;q=q->next;} else {t=q;r->next=p;r=r->next;p=p->next;q=q->next;free(t);}//相同元素只出现一次 } while(p) {r->next=p;r=r->next;p=p->next;} while(q) {r->next=q;r=r->next;q=q->next;} r->next=NULL;free(h2); printf("After merge now :\n"); print(h1);}void delsame(void)//删除A中与B中相同的节点{ Linklist h1,h2; h1=(Linklist)malloc(sizeof(Lnode)); h2=(Linklist)malloc(sizeof(Lnode)); h1->next=h2->next=NULL; printf("linklist 1:\n"); create2(h1); printf("linklist 2:\n"); create2(h2); Linklist p,q,r; r=h1;p=h1->next; while(p!=NULL) { //for(q=h2->next;q!=NULL&&q->alp!=p->alp;q=q->next); q=h2->next; while(q!=NULL&&q->alp!=p->alp) q=q->next; if(q==NULL) r=p; else r->next=p->next; p=p->next; } print(h1);}void reserve(Linklist h)//单链表逆置{ Linklist p,q,r; q=h->next; if(q==NULL) print(h); else { r=q->next; while(r!=NULL) { p=q;q=r;r=r->next; q->next=p; } h->next=q; print(h); }}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值