线性表-----单链表
#include <stdio.h>
#include <malloc.h>
struct LNode{
int data;
struct LNode *next;
};
void InitLinkList(LNode *head) {
if(head->next==NULL)
return;
head->next=NULL;
}
void createListR(LNode *head,int a[],int n)
{
LNode *p;
p=head;
while(p->next!=NULL)
{
p=p->next;
}
for(int i=0;i<n;i++)
{
LNode *pnew=(LNode *)malloc(sizeof(LNode));
pnew->data=a[i];
p->next=pnew;
p=p->next;
}
p->next=NULL;
}
void createListL(LNode *head,int a[],int n)
{
LNode *p,*q;
q=head->next;
p=head;
for(int i=0;i<n;i++)
{
LNode *newp =(LNode *)malloc(sizeof(LNode));
p->next=newp;
newp->data=a[i];
p=p->next;
}
p->next=q;
}
void sortLNode(LNode *head)
{
LNode *i,*j;
int temp;
i=head->next;
j=i->next;
while(i!=NULL)
{
while(j!=NULL)
{
if(j->data<i->data)
{
temp=i->data;
i->data=j->data;
j->data=temp;
}
j=j->next;
}
j=i->next;
i=i->next;
}
}
void mergeL(LNode *a,LNode *b,LNode *&c)
{
LNode *p=a->next,*q=b->next;
LNode *cp=c;
while(p!=NULL&&q!=NULL)
{
if(p->data>q->data)
{
LNode *newc =(LNode *)malloc(sizeof(LNode));
newc->data=q->data;
cp->next=newc;
cp=cp->next;
q=q->next;
}
if(p->data<=q->data)
{
LNode *newc =(LNode *)malloc(sizeof(LNode));
newc->data=p->data;
cp->next=newc;
cp=cp->next;
p=p->next;
}
}
while(p!=NULL)
{
LNode *newc =(LNode *)malloc(sizeof(LNode));
newc->data=p->data;
cp->next=newc;
cp=cp->next;
p=p->next;
}
while(q!=NULL)
{
LNode *newc =(LNode *)malloc(sizeof(LNode));
newc->data=q->data;
cp->next=newc;
cp=cp->next;
q=q->next;
}
cp->next=NULL;
}
void mergeListtoIncreasing(LNode *A, LNode *B, LNode *&C){
LNode *p = A->next;
LNode *q = B->next;
LNode *r;
C = A;
C->next = NULL;
free(B);
r = C;
while(p != NULL && q != NULL){
if(p->data <= q->data){
r->next = p;
p = p->next;
r = r->next;
}else{
r->next = q;
q = q->next;
r = r->next;
}
}
r->next = NULL;
if(p != NULL)
r->next = p;
if(q != NULL)
r->next = q;
r->next = NULL;
}
void count(LNode *head)
{
int i=0;
LNode *p;
p=head;
while(p!=NULL)
{
i++;
p=p->next;
}
printf("有%d个节点(包括头结点)\n",i);
}
void deleteLNode(LNode *head,int n)
{
int i=1;
LNode *p=head->next,*prev=head,*q;
while(p!=NULL)
{
if(i==n)
{
q=prev->next;
prev->next=p->next;
free(q);
return ;
}
i++;
prev=p;
p=p->next;
}
printf("第%d个节点没有\n",n);
}
void insertLNode(LNode *head, int m,int n)
{
int i=1;
LNode *p=head->next,*prev=head;
while (p!=NULL)
{
if(i==m)
{
LNode *newp=(LNode *)malloc(sizeof(LNode));
newp->data=n;
newp->next=p;
prev->next=newp;
return;
}
i++;
prev=p;
p=p->next;
}
printf("第%d个节点没有\n",m);
}
void findAndDelete(LNode *head, int x) {
LNode *p,*prev,*q;
p=head->next;
prev=head;
while(p!=NULL)
{
if(x==p->data)
{
q=prev->next;
prev->next=p->next;
free(q);
return ;
}
prev=p;
p=p->next;
}
printf("查找元素不存在\n");
}
void deleteMin(LNode *head) {
LNode *p=head->next,*prev=head;
LNode *prevmin=head,*min=p;
while(p!=NULL)
{
if(min->data>p->data)
{
min=p;
prevmin=prev;
}
prev=p;
p=p->next;
}
prevmin->next=min->next;
free(min);
}
void deleterepeat(LNode *head)
{
LNode *p,*q;
p=head->next;
while(p->next!=NULL)
{
if(p->data==p->next->data)
{
q=p->next;
p->next=p->next->next;
free(q);
continue;
}
p=p->next;
}
}
void reverseList(LNode *head) {
LNode *p=head->next,*q;
head->next=NULL;
while(p!=NULL)
{
q=p->next;
p->next=head->next;
head->next=p;
p=q;
}
}
void find (LNode *head,int k)
{
LNode *p=head,*q=head;
int i=0;
while(p->next!=NULL)
{
i++;
p=p->next;
if(i>=k)
{
q=q->next;
}
}
if(q==head)
printf("表中没有倒数第%d个节点",k);
else
printf("表中倒数第%d个节点为%d",k,q->data);
}
void display(LNode *head)
{
if(head->next==NULL)
return;
printf("结构体:%d\n",head);
LNode *p=head->next;
int i=0;
while(p!=NULL)
{
i++;
printf("第%d个节点数据是%d\n",i,p->data);
p=p->next;
}
}
void reverprintf(LNode *head)
{
LNode *p=head->next;
if(p!=NULL)
{
reverprintf(p);
printf("节点数据是%d\n",p->data);
}
}
int main()
{
LNode *a = (LNode *)malloc(sizeof(LNode));
LNode *b = (LNode *)malloc(sizeof(LNode));
InitLinkList(a);
InitLinkList(b);
int a1[10],b1[10];
for(int i=0;i<10;i++)
{
a1[i]=i;
b1[i]=20-i;
}
createListR(a,a1,10);
display(a);
createListL(b,b1,10);
display(b);
sortLNode(b);
display(b);
LNode *d=(LNode *)malloc(sizeof(LNode));
mergeL(a,b,d);
display(d);
count(d);
deleteLNode(d,1);
display(d);
insertLNode(d, 3,0);
display(d);
findAndDelete(d,19);
display(d);
deleteMin(d);
display(d);
deleterepeat(d);
display(d);
reverseList(d);
display(d);
find(d,1);
reverprintf(d);
return 0;
}