本篇文章给大家谈谈c语言单向链表逆序,以及c语言单向链表实现快速排序对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、用c语言实现链表的合并和逆序2、单循环链表的逆置(C语言)3、C语言链表倒序4、c语言,链表的反转怎么写代码?
用c语言实现链表的合并和逆序
#include stdio.h
#include stdlib.h
typedef struct Node
{
int num;
struct Node* nextptr;
}Node;
void printList(Node head)
{
Node *cptr= head.nextptr;
while(cptr!=NULL)
{
//printf(“%x: %d|%x-\n”,cptr, cptr-num, cptr-nextptr);
printf(“%d “,cptr-num);
cptr= cptr-nextptr;
}
printf(“\n”);
}
void freeList(Node head)
{
Node *nptr, *cptr= head.nextptr;
while(cptr != NULL)
{
nptr= cptr-nextptr;
free(cptr);
cptr= nptr;
}
}
Node* newNode(const int ipt_num)
{
static Node *newptr;
newptr=(Node*)malloc(sizeof(Node));
newptr-num= ipt_num;
newptr-nextptr= NULL;
return newptr;
}
Node merger(Node la, Node lb)
{
Node lc, *cptr= lc;
Node *aptr= la.nextptr;
Node *bptr= lb.nextptr;
while(aptr!=NULL || bptr!=NULL)
{
if(aptr!=NULL bptr!=NULL)
{
if(aptr-num bptr-num)
{
cptr-nextptr= newNode(aptr-num);
aptr= aptr-nextptr;
}
else if(aptr-num bptr-num)
{
cptr-nextptr= newNode(bptr-num);
bptr= bptr-nextptr;
}
else{
cptr-nextptr= newNode(aptr-num);
aptr= aptr-nextptr;
bptr= bptr-nextptr;
}
}
else{
if(aptr==NULL)
{
cptr-nextptr= newNode(bptr-num);
bptr= bptr-nextptr;
}
else{//bptr==NULL
cptr-nextptr= newNode(aptr-num);
aptr= aptr-nextptr;
}
}
cptr= cptr-nextptr;
cptr-nextptr= NULL;
}
return lc;
}
void inverted(Node la, Node lb)
{
Node newnode, *cutptr;
newnode.nextptr=NULL;
while(la.nextptr!=NULL || lb.nextptr!=NULL)
{
if(la.nextptr!=NULL lb.nextptr!=NULL)
{
if(la.nextptr-num lb.nextptr-num)
{
cutptr= la.nextptr;
la.nextptr= la.nextptr-nextptr;
}
else{
cutptr= lb.nextptr;
lb.nextptr= lb.nextptr-nextptr;
}
}
else{
if(la.nextptr!=NULL)
{
cutptr= la.nextptr;
la.nextptr= la.nextptr-nextptr;
}
else{
cutptr= lb.nextptr;
lb.nextptr= lb.nextptr-nextptr;
}
}
cutptr-nextptr= newnode.nextptr;
newnode.nextptr= cutptr;
}
la= newnode;
}
int main(void)
{
Node la, lb, lc;
Node *curptr;
curptr= la;
curptr-nextptr= newNode(3);
curptr= curptr-nextptr;
curptr-nextptr= newNode(5);
curptr= curptr-nextptr;
curptr-nextptr= newNode(8);
curptr= curptr-nextptr;
curptr-nextptr= newNode(11);
curptr= lb;
curptr-nextptr= newNode(2);
curptr= curptr-nextptr;
curptr-nextptr= newNode(6);
curptr= curptr-nextptr;
curptr-nextptr= newNode(8);
curptr= curptr-nextptr;
curptr-nextptr= newNode(9);
curptr= curptr-nextptr;
curptr-nextptr= newNode(11);
curptr= curptr-nextptr;
curptr-nextptr= newNode(15);
curptr= curptr-nextptr;
curptr-nextptr= newNode(20);
printList(la);
printList(lb);
lc= merger(la,lb);
printList(lc);
inverted(la,lb);
printList(la);
freeList(la);
freeList(lb);
freeList(lc);
printf(“end”);
return 0;
}
单循环链表的逆置(C语言)
//单循环链表的逆置
#include stdio.h
#include malloc.h
#include math.h
#include process.h
typedef struct lnode
{
int data;
struct lnode *next;
}lnode ,*linklist;
//建立一个空表,初始化,并对其进行输入值
void creatlinklist(linklist l,int n)
{
l=(linklist)malloc(sizeof(lnode));
if(!l)
exit(OVERFLOW);
l-next=l;//循环链表
linklist p,q;
int i;
q=l;
printf(“please input %d data:\n”,n);
for(i=0;i=n-1;i++)
{
p=(linklist)malloc(sizeof(lnode));
scanf(“%d”,p-data);
p-next=q-next;
q-next=p;
q=p;
}
}
//逆置函数
void invert(linklist l)
{
linklist p,q,r;
p=l-next;
q=p-next;
r=q-next;
while(p!=l)
{
q-next=p;
p=q;
q=r;
r=r-next;
}
}
int main()
{
linklist l,p;
int length,i;
printf(“please input the length of the list:\n”);
scanf(“%d”,length);
creatlinklist(l,length);
p=l-next;
while(p!=l)
{
printf(“%d “,p-data);
p=p-next;
}
printf(“\n”);
invert(l);
p=l-next;
while(p!=l)
{
printf(“%d “,p-data);
p=p-next;
}
printf(“\n”);
return 0;
}
C语言链表倒序
你得在p2-next=p3;后面加一句:p2=p3;否则你的p2一直是指向head。
c语言,链表的反转怎么写代码?
单链表反转很简单,只说下思路:
1,从头到尾循环遍历链表
2,取下头结点,作为尾结点,尾结点此时也为头结点
3,采用前插法,将步骤二中取下的结点一个一个连接到头结点前面,成为新的头结点。
4,链表全部遍历完后,新的链表产生了,是原来链表的反转。
c语言单向链表逆序的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于c语言单向链表实现快速排序、c语言单向链表逆序的信息别忘了在本站进行查找喔。