一个简单的栈的实现:
栈的插入和删除就是链表中第一个元素的删除和插入操作。
#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);
}
}