单项链表的 建立、插入(有序,构造有序链表),删除节点,头插法,尾插法,排序(交换数据)及输出函数,释放节点;
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head=NULL,*t,*q,*p;
void built()
{
while(1)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
if(p->data<=0) break;
p->next=NULL;
if(head==NULL) head=p;
else q->next=p;
q=p;
}
return ;
}
void hadd()
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
if(head==NULL) head=p;
else
{
p->next=head;
head=p;
}
return ;
}
void tadd()
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
if(head==NULL) head=p;
else
{
t=head;
while(t->next!=NULL) t=t->next;
t->next=p;
}
return ;
}
void print()
{
t=head;
while(t!=NULL)
{
printf("%d ",t->data);
t=t->next;
}
return ;
}
void input()
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
if(head==NULL) head=p;
else
{
t=head;
while(t!=NULL)
{
if(t->next==NULL||t->next->data>p->data)
{
p->next=t->next;
t->next=p;
break;
}
t=t->next;
}
}
return ;
}
void delet()
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
t=head;
if(t->data==p->data&&t==head)
{ head=head->next; return ;}
while(t->next!=NULL)
{
if(t->next->data==p->data)
{
t->next=t->next->next;
return ;
}
t=t->next;
}
printf("error\n");
return ;
}
void sort()
{
t=head;
while(t!=NULL)
{
p=t;
while(p->data>p->next->data)
{
int temp=p->data;
p->data=p->next->data;
p->next->data=temp;
}
t=t->next;
}
return ;
}
void Free()
{
t=head;
while(head!=NULL)
{
free(t);
head=head->next;
t=head;
}
return ;
}
int main()
{
built();
delet();
print();
Free();
return 0;
}
双向链表的构建,有序插入,头插法,尾插法,删除,输出操作;
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
struct node *prev;
};
struct node *head,*p,*q,*t,*s;
void built()
{
q=NULL;
while(1)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
if(p->data<=0) break;
p->next=NULL; p->prev=NULL;
if(head==NULL) head=p;
else
{
q->next=p;
p->prev=q;
}
q=p;
}
return ;
}
void hadd()
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data); p->next=NULL;p->prev=NULL;
if(head==NULL)
{
head=p;
return ;
}
p->next=head;
head->prev=p;
head=p;
return ;
}
void tadd()
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data); p->next=NULL;p->prev=NULL;
t=head;
if(head==NULL)
{
head=p;
return ;
}
while(t->next!=NULL) t=t->next;
t->next=p;
p->prev=t;
return ;
}
void input()
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data); p->next=NULL;p->prev=NULL;
t=head;
if(head==NULL) head=p;
else
{
if(head->data>p->data)
{
p->next=head;
head->prev=p;
head=p;
return ;
}
while(t!=NULL)
{
if(t->next==NULL||t->next->data>p->data)
{
t->next->prev=p;p->next=t->next;
t->next=p;
p->prev=t;
t=p;
break;
}
t=t->next;
}
}
return ;
}
void delet()
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data); p->next=NULL;p->prev=NULL;
t=head;
if(head->data==p->data)
{
head->next->prev=NULL;
head=head->next;
return ;
}
while(t->next!=NULL)
{
if(t->next->data==p->data)
{
if(t->next->next==NULL)
{
t->next->prev=NULL;
t->next=NULL;
return ;
}
t->next->next->prev=t;
t->next=t->next->next;
return ;
}
t=t->next;
}
printf("error\n");
return ;
}
void print()
{
int flag=1;
t=head;
while(t!=NULL)
{
printf("%d ",t->data);
if(t->next==NULL) p=t;
t=t->next;
}
while(p!=NULL)
{
printf("%d ",p->data);
p=p->prev;
}
return ;
}
int main()
{
built();
tadd();
print();
return 0;
}