单链表和栈

一个简单的栈的实现:

栈的插入和删除就是链表中第一个元素的删除和插入操作。

#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
    int data;
    node *next;
} Lnode;

Lnode *Creatlist();
void Printlist(Lnode *head);
int IsEmpty(Lnode *L);
Lnode *Find(int x,Lnode *L);
void Insert(int x,Lnode *P);
int Islast(Lnode *p,Lnode *L);
void Delect(int x,Lnode *L);
Lnode *Findpre(int x,Lnode *L);


int main()
{
    Lnode *tt,*hh;
    int t,m=4;
    tt=Creatlist();
    Printlist(tt);
    t=IsEmpty(tt);
    printf("%d/n",t);
    hh=Find(m,tt);
    printf("输出第%d个数 %d/n",m,hh->data);


    printf("输出插入数之后的数字:/n");
    Insert(3,Find(0,tt));
   
    Printlist(tt);


   printf("输出查找到的前一个数:");
    printf("%d/n",Findpre(4,tt)->data);

    printf("输出删除之后的线性表/n");
    Delect(0,tt);
    Printlist(tt);

  
    return 0;
}

Lnode *Creatlist()
{
    Lnode *head,*p,*q;
    int t=7;
    int i;
    head=(Lnode*)malloc(sizeof(Lnode));
    p=head;
    for(i=0;i<t;i++)
    {
        q=(Lnode*)malloc(sizeof(Lnode));
        q->data=(i*i);
        p->next=q;
        p=q;
    }
    p->next=NULL;
    return head;

}

void Printlist(Lnode *head)
{
    Lnode *p;
    p=head->next;
    while(p!=NULL)
    {
   
        printf("%d/n",p->data);
            p=p->next;
    }
}

int IsEmpty(Lnode *L)
{
    return L->next==NULL;
}

Lnode *Find(int x,Lnode *L)
{
    Lnode *p;
    int i;
    p=L->next;
    if(x<0||x>7)
        printf("the result is wrong/n");
    else
    {
   for(i=0;i<x;i++)
        p=p->next;
    }
    return p;
}

void Insert(int x,Lnode *P)
{
    Lnode *tmp;
    tmp=(Lnode*)malloc(sizeof(Lnode));
    if(tmp==NULL)
        printf("out of space/n");
    tmp->data=x;
    tmp->next=P->next;
    P->next=tmp;
}

int Islast(Lnode *p)
{
    return p->next==NULL;
}

Lnode *Findpre(int x,Lnode *L)
{
    Lnode *p;
    int i;
    p=L;
    if(x<0||x>7)
        printf("the result is wrong/n");
    //else
    //{
   //for(i=0;i<x;i++)
    //    p=p->next;
    //}
    //return p;
   for(i=0;i<=x-1;i++)
   {
       p=p->next;
   }
    return p;

}

void Delect(int x,Lnode *L)
{
    Lnode *p,*tmp;
    p=Findpre(x,L);
    if(!Islast(L))
    {
        tmp=p->next;
p->next=tmp->next;
free(tmp);
    }
}

 

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

单链表:

 

 

#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
    int data;
    node *next;
} Lnode;

Lnode *Creatlist();
void Printlist(Lnode *head);
int IsEmpty(Lnode *L);
Lnode *Find(int x,Lnode *L);
void Insert(int x,Lnode *P);
int Islast(Lnode *p,Lnode *L);
void Delect(int x,Lnode *L);
Lnode *Findpre(int x,Lnode *L);


int main()
{
    Lnode *tt,*hh;
    int t,m=4;
    tt=Creatlist();
    Printlist(tt);
    t=IsEmpty(tt);
    printf("%d/n",t);
    hh=Find(m,tt);
    printf("输出第%d个数 %d/n",m,hh->data);


    printf("输出插入数之后的数字:/n");
    Insert(3,Find(0,tt));
   
    Printlist(tt);


   printf("输出查找到的前一个数:");
    printf("%d/n",Findpre(4,tt)->data);

    printf("输出删除之后的线性表/n");
    Delect(0,tt);
    Printlist(tt);

  
    return 0;
}

Lnode *Creatlist()
{
    Lnode *head,*p,*q;
    int t=7;
    int i;
    head=(Lnode*)malloc(sizeof(Lnode));
    p=head;
    for(i=0;i<t;i++)
    {
        q=(Lnode*)malloc(sizeof(Lnode));
        q->data=(i*i);
        p->next=q;
        p=q;
    }
    p->next=NULL;
    return head;

}

void Printlist(Lnode *head)
{
    Lnode *p;
    p=head->next;
    while(p!=NULL)
    {
   
        printf("%d/n",p->data);
            p=p->next;
    }
}

int IsEmpty(Lnode *L)
{
    return L->next==NULL;
}

Lnode *Find(int x,Lnode *L)
{
    Lnode *p;
    int i;
    p=L->next;
    if(x<0||x>7)
        printf("the result is wrong/n");
    else
    {
   for(i=0;i<x;i++)
        p=p->next;
    }
    return p;
}

void Insert(int x,Lnode *P)
{
    Lnode *tmp;
    tmp=(Lnode*)malloc(sizeof(Lnode));
    if(tmp==NULL)
        printf("out of space/n");
    tmp->data=x;
    tmp->next=P->next;
    P->next=tmp;
}

int Islast(Lnode *p)
{
    return p->next==NULL;
}

Lnode *Findpre(int x,Lnode *L)
{
    Lnode *p;
    int i;
    p=L;
    if(x<0||x>7)
        printf("the result is wrong/n");
    //else
    //{
   //for(i=0;i<x;i++)
    //    p=p->next;
    //}
    //return p;
   for(i=0;i<=x-1;i++)
   {
       p=p->next;
   }
    return p;

}

void Delect(int x,Lnode *L)
{
    Lnode *p,*tmp;
    p=Findpre(x,L);
    if(!Islast(L))
    {
        tmp=p->next;
p->next=tmp->next;
free(tmp);
    }
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值