데이터 구조의 링크 의 실현 및 각종 응용
6515 단어 데이터 구조
#include
#include
#include //the max and the min.
typedef struct LNode{
int data;
struct LNode *next;
}LNode;
//
LNode* initLinkList(LNode *L)
{
L =(LNode*)malloc(sizeof(LNode));
L -> next = NULL;
return L;
}
int listLength(LNode *L){
int length = 0;
LNode *p = L->next;
while(p!=NULL){
length++;
p=p->next;
}
//printf("\r
:%d\r
",length);
return length;
}
//-----Important ----
void DestoryList(LNode *&L){
LNode *q;
// L
while(L->next!=NULL){
//q L
q=L->next;
free(L);
//
L=q;
}
}
//
void insertLinkListByLNode(LNode *&L,int x){
LNode *s,*r,*q;
//
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
// r
r=L;
q=r->next;
r->next = s;
s->next = q;
}
//
void insertLinkListByLNode(LNode *&L,int a[],int n){
LNode *s,*p,*q;
int i;
for(i=0;idata = a[i];
// r
s->next = L->next;
L->next = s;
}
}
// - -
void insertByRear(LNode *&L,int x){
LNode *s,*r;
r=L;
//
//
if(L->next == NULL){
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
r ->next = s;
s->next = NULL;
return;
}
// // r L?
while(r->next!=NULL){
r = r->next;
}
//
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
r->next = s;
s->next = NULL;//----->
}
// - -
void insertByArrayByRear(LNode *&L,int a[],int n){
int i;
LNode *s,*r;
r = L;//r
for(i=0;i<10;i++){
s = (LNode*)malloc(sizeof(LNode));
s->data = a[i];
r ->next = s;
r = r->next;
}
r->next = NULL;
}
//
void insertByorder(LNode *&L,int x){
LNode *s,*p,*pre_p;//pre_p
pre_p = L;
p=L->next;
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
//
if(L->next == NULL){
L->next = s;
s->next = NULL;
return;
}
//
while(x>p->data){
// if
if(p->next==NULL) {
p->next = s;
return;
}
pre_p = pre_p->next;
p = p->next;
}
//p
s ->next = p;
pre_p ->next = s;
}
//
void DeleteByNum(LNode *&L,int x){
if(L->next==NULL){
printf(" ! ");
return;
}
LNode *d;
LNode *p = L->next;
LNode *pre_p = L;
while(p!=NULL){
if(p->data==x){
d = p;
p = p->next;
pre_p->next = p;
free(d);
}
pre_p = pre_p->next;
p=p->next;
}
}
//
void DeleteByPos(LNode *&L,int pos){
printf("len = %d",listLength(L));
//
if(pos<0||pos>listLength(L)){
printf(" , !\r
");
return;
}
//
LNode *pre_p,*p,*q;
pre_p = L;
p = pre_p->next;
//
int i=0;printf("i=%d\r
",i);
while(inext;
p=p->next;
i++;
}
pre_p->next = p->next;
q = p;
// e = p->data;
p=p->next;
free(q);
}
//
void Lsort(LNode *&L)//
{
int i,j,t;
LNode *p,*q;//tail
if(L->next==NULL) return;
//
for(i=0,p=L->next;inext){
for(j=i+1,q = p->next;jnext)
{
if(p->data > q->data)// p p->next data
{
t=p->data;
p->data=q->data;
q->data=t;
}
}
}
}
// ,
void Merge_TwoOrdered_List(LNode *A,LNode *B,LNode *&C){
LNode *p = A->next;
LNode *q = B->next;
LNode *s = C;
while(p&&q){
if(p->data<=q->data){
s->next = p;
s = s->next;
p=p->next;
}
else{
s->next = q;
s = s->next;
q = q->next;
}
}
if(p==NULL){
s->next = q;
}
if(q==NULL){
s->next = p;
}
}
//
void reverse(LNode *L){
if(L->next==NULL||L->next->next=NULL) return;
LNode *p;
LNode *newNode = NULL;
while(p->next!=NULL){
LNode *temp = p->next;
p->next
}
}
void print(LNode *L){
LNode *p = L->next;
if(p==NULL){
printf(" !\r
");
return;
}
while(p!=NULL)
{
printf("%d ",p->data);
p = p->next;
}
}
void structTest()
{
LNode *L,*A,*B,*C;
L = initLinkList(L);
A = initLinkList(A);
B = initLinkList(B);
C = initLinkList(C);
//
/* insertLinkListByF(L,5);
insertLinkListByF(L,5);
insertLinkListByF(L,1);
insertLinkListByF(L,3);
insertLinkListByF(L,9);
print(L);*/
insertByRear(L,0);
insertByRear(L,1);
insertByRear(L,8);
insertByRear(L,3);
insertByRear(L,4);
insertByRear(L,5);
print(L);
printf("\r
");
Lsort(L);
print(L);
/*insertByRear(A,1);
insertByRear(A,4);
insertByRear(A,8);
insertByRear(A,9);
insertByRear(A,100);
insertByRear(B,2);
insertByRear(B,4);
insertByRear(B,5);
insertByRear(B,100);
Merge_TwoOrdered_List(A,B,C);
*///
/*int i,a[100];
for(i=0;i<10;i++){
scanf("%d",&a[i]);
}
insertByArrayByRear(L,a,10);
*/
/* int i,a[100];
for(i=0;i<10;i++){
scanf("%d",&a[i]);
}
insertLinkListByLNode(L,a,10);
*/
/* insertByorder(L,10);
insertByorder(L,18);
insertByorder(L,7);
insertByorder(L,17);
insertByorder(L,5);
insertByorder(L,6);
*/
// DestoryList(L);
//insertLinkListByR(L,2);
//print(L);
//DeleteByNum(L,15);
// printf("\r
delete\r
");
// DeleteByPos(L,8);
// print(C);
} //Of structTest.
void main()
{
structTest();
} //of main.