环界txt下载:单向链表、双向链表,栈,队列的C语言的实现方法
来源:百度文库 编辑:中财网 时间:2024/04/28 12:54:04
以下是单向链表、双向链表,栈,队列的C语言的实现方法,只包含简单的插入删除操作1,单向链表的插入,删除,逆序操作#include
#includetypedef struct Node
{
int key;
struct Node* next;
}* node;node newNode(int k)
{
node n=(node)malloc(sizeof(node));
n->key=k;
n->next=NULL;
return n;
}void printlist(node n)
{
if(!n)
{
printf("n is NULL list\n");
}
while(n)
{
printf("%d",n->key);
printf(" ");
n=n->next;
}
printf("\n");
}
node newList()
{
int k;
node head=(node)malloc(sizeof(node));;
scanf("%d",&k);
if(k==0)
{
head=NULL;
return head;
}
else
{
node n=newNode(k);
head=n;
while(k)
{
scanf("%d",&k);
if(k!=0)
{
node n1=newNode(k);
n->next=n1;
n=n->next;
}
}
n->next=NULL;
return head;
}
}node insertNode(node n,int p,int k)
{
node n1=newNode(k);
node head=(node)malloc(sizeof(node));
head=n;
if(head==NULL)
{
n1->next=head;
return n1;
}
else
{
if(p==1)
{
n1->next=head;
head=n1;
return head;
}
else
{
int i=2;
while(i!=p&&(n->next))
{
n=n->next;
i++;
}
if(n->next==NULL)
{
printf("the p can't be found\n");
return head;
}
else
{
n1->next=n->next;
n->next=n1;
return head;
}
}
}
}node deleteNode(node n,int k)
{
node n1=(node)malloc(sizeof(node));
node head=(node)malloc(sizeof(node));
head=n;
if(head==NULL)
{
printf("list is NULL\n");
return head;
}
else
{
if(head->key==k)
{
head=head->next;
return head;
}
while(n->key!=k&&n->next)
{
n1=n;
n=n->next;
}
if(n==NULL)
{
printf("can't find the same value as k in this list\n");
return head;
}
else
{
n1->next=n->next;
n=NULL;
return head;
}
}
}node reverse(node n)
{ node n1[10];
node head=(node)malloc(sizeof(node));
node n2=(node)malloc(sizeof(node));
head=n; if(head==NULL)
{
return head;
}
else
{
int i=0;
while(head!=NULL)
{
n2=head;
head=head->next;
n2->next=NULL;
n1[i]=n2;
i++;
}
head=n1[i-1];
for(int j=i-1;j>0;j--)
{
n1[j]->next=n1[j-1];
}
return head;
}
}int main()
{
node n=newList();
printlist(n); //插入操作
int k,p;
scanf("%d,%d",&p,&k);
node nn=insertNode(n,p,k);
printlist(nn); //删除操作
int q;
scanf("%d",&q);
node nd=deleteNode(nn,q);
printlist(nd); //链表的倒置操作
node m=reverse(n);
printlist(m);
return 0;
}2,双向链表的插入删除操作#include
#includetypedef struct Node
{
int key;
struct Node* pre;
struct Node* next;
}* node;node newNode(int i)
{
node n=(node)malloc(sizeof(node));
n->key=i;
n->pre=NULL;
n->next=NULL;
return n;
}node newduplinklist()
{
int i;
scanf("%d",&i);
node n;
if(i==0)
{
n=NULL;
return n;
}
n=newNode(i);
node head=n;
int k=1;
while(k!=0)
{
scanf("%d",&k);
if(k!=0)
{
node n1=newNode(k);
n->next=n1;
n1->pre=n;
n=n1;
}
}
n->next=head;
head->pre=n;
return head;
}
int sizeduplinklist(node n)
{ if(n==NULL) return 0;
node head=n;
int i=1;
while(head->next!=n)
{
head=head->next;
i++;
}
return i;
}void print(node n)
{
if(n==NULL)
printf("此时链表为空!");
else
{
printf("输出链表:\n");
for(int i=0;i {
printf("%-2d",n->key);
n=n->next;
}
printf("\n");
}
}
node insertNode(node n)
{
int p,k;
printf("插入位置p:\n");
scanf("%d,%d",&p,&k);
printf("\n");
if(p>sizeduplinklist(n))
{
printf("此位置超出链表的长度!\n");
return n;
}
else if(p<1)
{
printf("此位置不存在!\n");
return n; }
else
{
node n1,head,m;
m=newNode(k);
head=n;
if(p==1)
{
m->pre=n->pre;
n->pre->next=m;
m->next=n;
n->pre=m;
return m;
}
else
{
int i=1;
while(i!=p)
{
n1=n;
n=n->next;
i++;
}
n1->next=m;
m->pre=n1;
m->next=n;
n->pre=m;
return head;
}
}
}node delNode(node n)
{
int p;
printf("删除位置p:\n");
scanf("%d",&p);
printf("\n");
if(p>sizeduplinklist(n))
{
printf("此位置超出链表的长度!\n");
return n;
}
else if(p<1)
{
printf("此位置不存在!\n");
return n; }
else
{
node n1,head;
if(p==1)
{ head=n->next;
n->pre->next=n->next;
n->next->pre=n->pre;
return head;
}
else
{
head=n;
int i=1;
while(i!=p)
{
n1=n;
n=n->next;
i++;
}
n1->next=n->next;
n->next->pre=n1;
return head;
}
}
}
int main()
{
node n=newduplinklist();
printf("%d\n",sizeduplinklist(n));
print(n);
node m=insertNode(n);
print(m);
node m1=delNode(m);
print(m1);
return 0;
}3,栈操作,用数组实现的包含出栈,入栈的操作#include
#includetypedef struct Sta
{
int a[20];
int num;
}* sta;sta NullStack()
{
sta s=(sta)malloc(sizeof(sta));
s->num=0;
return s;
}sta pushstack(sta s,int i)
{
s->a[s->num]=i;
s->num=s->num+1;
return s;
}sta popstack(sta s)
{
if(s->num==0)
{
printf("stack is NULL\n");
return s;
}
s->num=s->num-1;
return s;
}void printstack(sta s)
{
if(s->num==0)
{
printf("stack is NULL\n");
}
else
{
for(int i=s->num-1; i>=0;i--)
{
printf("%d\n",s->a[i]);
}
}
}
int main()
{
sta s=NullStack();
s=pushstack(s,1);
s=pushstack(s,2);
printstack(s);
s=popstack(s);
s=popstack(s);
printstack(s);return 0;
}4,队列操作,类似于栈,#include
#includetypedef struct Que
{
int a[20];
int num;
}* que;que NullQueue()
{
que q=(que)malloc(sizeof(que));
q->num=0;
return q;
}que enq(que q,int i)
{
q->a[q->num]=i;
q->num=q->num+1;
return q;
}que deq(que q)
{
if(q->num==0)
{
printf("queue is NULL\n");
return q;
}
for(int i=1;inum;i++)
{
q->a[i-1]=q->a[i];
}
q->num=q->num-1;
return q;
}void printqueue(que q)
{
if(q->num==0)
{
printf("queue is NULL\n");
}
else
{
for(int i=q->num-1; i>=0;i--)
{
printf("%3d",q->a[i]);
}
}
}
int main()
{
que q=NullQueue();
q=enq(q,1);
q=enq(q,2);
q=enq(q,3);
q=enq(q,4);
printqueue(q);
printf("\n");
q=deq(q);
//printf("%d",s->a[0]);
//q=deq(q);
printqueue(q);return 0;
} 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/liucong2004/archive/2009/05/20/4204776.aspx
#include
{
int key;
struct Node* next;
}* node;node newNode(int k)
{
node n=(node)malloc(sizeof(node));
n->key=k;
n->next=NULL;
return n;
}void printlist(node n)
{
if(!n)
{
printf("n is NULL list\n");
}
while(n)
{
printf("%d",n->key);
printf(" ");
n=n->next;
}
printf("\n");
}
node newList()
{
int k;
node head=(node)malloc(sizeof(node));;
scanf("%d",&k);
if(k==0)
{
head=NULL;
return head;
}
else
{
node n=newNode(k);
head=n;
while(k)
{
scanf("%d",&k);
if(k!=0)
{
node n1=newNode(k);
n->next=n1;
n=n->next;
}
}
n->next=NULL;
return head;
}
}node insertNode(node n,int p,int k)
{
node n1=newNode(k);
node head=(node)malloc(sizeof(node));
head=n;
if(head==NULL)
{
n1->next=head;
return n1;
}
else
{
if(p==1)
{
n1->next=head;
head=n1;
return head;
}
else
{
int i=2;
while(i!=p&&(n->next))
{
n=n->next;
i++;
}
if(n->next==NULL)
{
printf("the p can't be found\n");
return head;
}
else
{
n1->next=n->next;
n->next=n1;
return head;
}
}
}
}node deleteNode(node n,int k)
{
node n1=(node)malloc(sizeof(node));
node head=(node)malloc(sizeof(node));
head=n;
if(head==NULL)
{
printf("list is NULL\n");
return head;
}
else
{
if(head->key==k)
{
head=head->next;
return head;
}
while(n->key!=k&&n->next)
{
n1=n;
n=n->next;
}
if(n==NULL)
{
printf("can't find the same value as k in this list\n");
return head;
}
else
{
n1->next=n->next;
n=NULL;
return head;
}
}
}node reverse(node n)
{ node n1[10];
node head=(node)malloc(sizeof(node));
node n2=(node)malloc(sizeof(node));
head=n; if(head==NULL)
{
return head;
}
else
{
int i=0;
while(head!=NULL)
{
n2=head;
head=head->next;
n2->next=NULL;
n1[i]=n2;
i++;
}
head=n1[i-1];
for(int j=i-1;j>0;j--)
{
n1[j]->next=n1[j-1];
}
return head;
}
}int main()
{
node n=newList();
printlist(n); //插入操作
int k,p;
scanf("%d,%d",&p,&k);
node nn=insertNode(n,p,k);
printlist(nn); //删除操作
int q;
scanf("%d",&q);
node nd=deleteNode(nn,q);
printlist(nd); //链表的倒置操作
node m=reverse(n);
printlist(m);
return 0;
}2,双向链表的插入删除操作#include
#include
{
int key;
struct Node* pre;
struct Node* next;
}* node;node newNode(int i)
{
node n=(node)malloc(sizeof(node));
n->key=i;
n->pre=NULL;
n->next=NULL;
return n;
}node newduplinklist()
{
int i;
scanf("%d",&i);
node n;
if(i==0)
{
n=NULL;
return n;
}
n=newNode(i);
node head=n;
int k=1;
while(k!=0)
{
scanf("%d",&k);
if(k!=0)
{
node n1=newNode(k);
n->next=n1;
n1->pre=n;
n=n1;
}
}
n->next=head;
head->pre=n;
return head;
}
int sizeduplinklist(node n)
{ if(n==NULL) return 0;
node head=n;
int i=1;
while(head->next!=n)
{
head=head->next;
i++;
}
return i;
}void print(node n)
{
if(n==NULL)
printf("此时链表为空!");
else
{
printf("输出链表:\n");
for(int i=0;i
printf("%-2d",n->key);
n=n->next;
}
printf("\n");
}
}
node insertNode(node n)
{
int p,k;
printf("插入位置p:\n");
scanf("%d,%d",&p,&k);
printf("\n");
if(p>sizeduplinklist(n))
{
printf("此位置超出链表的长度!\n");
return n;
}
else if(p<1)
{
printf("此位置不存在!\n");
return n; }
else
{
node n1,head,m;
m=newNode(k);
head=n;
if(p==1)
{
m->pre=n->pre;
n->pre->next=m;
m->next=n;
n->pre=m;
return m;
}
else
{
int i=1;
while(i!=p)
{
n1=n;
n=n->next;
i++;
}
n1->next=m;
m->pre=n1;
m->next=n;
n->pre=m;
return head;
}
}
}node delNode(node n)
{
int p;
printf("删除位置p:\n");
scanf("%d",&p);
printf("\n");
if(p>sizeduplinklist(n))
{
printf("此位置超出链表的长度!\n");
return n;
}
else if(p<1)
{
printf("此位置不存在!\n");
return n; }
else
{
node n1,head;
if(p==1)
{ head=n->next;
n->pre->next=n->next;
n->next->pre=n->pre;
return head;
}
else
{
head=n;
int i=1;
while(i!=p)
{
n1=n;
n=n->next;
i++;
}
n1->next=n->next;
n->next->pre=n1;
return head;
}
}
}
int main()
{
node n=newduplinklist();
printf("%d\n",sizeduplinklist(n));
print(n);
node m=insertNode(n);
print(m);
node m1=delNode(m);
print(m1);
return 0;
}3,栈操作,用数组实现的包含出栈,入栈的操作#include
#include
{
int a[20];
int num;
}* sta;sta NullStack()
{
sta s=(sta)malloc(sizeof(sta));
s->num=0;
return s;
}sta pushstack(sta s,int i)
{
s->a[s->num]=i;
s->num=s->num+1;
return s;
}sta popstack(sta s)
{
if(s->num==0)
{
printf("stack is NULL\n");
return s;
}
s->num=s->num-1;
return s;
}void printstack(sta s)
{
if(s->num==0)
{
printf("stack is NULL\n");
}
else
{
for(int i=s->num-1; i>=0;i--)
{
printf("%d\n",s->a[i]);
}
}
}
int main()
{
sta s=NullStack();
s=pushstack(s,1);
s=pushstack(s,2);
printstack(s);
s=popstack(s);
s=popstack(s);
printstack(s);return 0;
}4,队列操作,类似于栈,#include
#include
{
int a[20];
int num;
}* que;que NullQueue()
{
que q=(que)malloc(sizeof(que));
q->num=0;
return q;
}que enq(que q,int i)
{
q->a[q->num]=i;
q->num=q->num+1;
return q;
}que deq(que q)
{
if(q->num==0)
{
printf("queue is NULL\n");
return q;
}
for(int i=1;i
{
q->a[i-1]=q->a[i];
}
q->num=q->num-1;
return q;
}void printqueue(que q)
{
if(q->num==0)
{
printf("queue is NULL\n");
}
else
{
for(int i=q->num-1; i>=0;i--)
{
printf("%3d",q->a[i]);
}
}
}
int main()
{
que q=NullQueue();
q=enq(q,1);
q=enq(q,2);
q=enq(q,3);
q=enq(q,4);
printqueue(q);
printf("\n");
q=deq(q);
//printf("%d",s->a[0]);
//q=deq(q);
printqueue(q);return 0;
} 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/liucong2004/archive/2009/05/20/4204776.aspx
将单向循环链表改成双向链表
C语言高手:什么是“单向链表”????解释一下!!
谁知道..用C语言描述双向链表的插入的算法
帮忙做道C语言的题:双向链表的排序
c语言编程,双向链表,要求建立,插入删除,排序,放学生的学号,成绩
传播是单向还是双向的
十亿火急,给个C语言双向链表排序流程图!!!!!!!!!!!!
链栈和链队列的实现
哪儿有双向优先队列的中文资料?
地铁的线路是单向还是双向的?
请教:MOC3021内的可控硅是双向还是单向的?
'文化的双向(单向)沟通'怎么翻译?
怎样知道手机卡是单向还是双向收费的
移动的139是单向还是双向收费?
那怎么样才可以知道是单向还是双向的?
求常用的单向链表的遍历算法
带有尾指针的单向循环链表R中,
C++ 双向链表
链表 双向链表
数据结构中双向循环链表的ADT表示
C语言中 的链表
c语言链表的用途是什么
求助!!用后接法建立一个带头结点的单向链表,并就地逆址该带头结点的单向链表。不甚感激!!
有哪位高手知道B137是双向的还是单向的可控硅,谢谢!