关于双向链表的相关一系列操作(作为备忘)

来源:互联网 发布:shutting down mysql 编辑:程序博客网 时间:2024/06/07 11:26

#ifndef _1_H

#define _1_H



typedef struct dlist

{

int score;

struct dlist *prev;

struct dlist *next;

}DLIST;



static DLIST *creatDList(int n);


staticvoid showDList(DLIST *h);


staticvoid showDList2(DLIST *h);


staticvoid showSelectDList(DLIST *h,int score);


staticvoid initDList(DLIST *h,int a[]);


staticint getDListLen(DLIST *h);


staticvoid insertDListBack(DLIST *h,int data,int pos);


staticvoid delDList(DLIST *h,int pos);


staticvoid reverse(DLIST *h);


staticvoid destroy(DLIST *h);



#endif





#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "1.h"


 DLIST *creatDList(int n)

{

DLIST *p,*r,*h;

int i;


h = (DLIST*)malloc(sizeof(DLIST));

r = h;

if(h ==NULL)

{

returnNULL;

}


for(i =0;i < n;i++)

{

p = (DLIST*)malloc(sizeof(DLIST));

r->next = p;

p->prev = r;//±»µ•¡¥±Ì∂‡“ªæ‰

r = p;

}


r->next =NULL;


return h;


}


void showDList(DLIST *h)

{

DLIST *p = h->next;

if(!h->next)

{

printf("this list is empty!\n");

}


while(p)

{

printf("%d\n",p->score);

p = p->next;

}

}



void showDList2(DLIST *h)

{

DLIST *p = h;


if(!h->next)

{

printf("this list is empty!\n");

}

while(p->next)

{

p = p->next;

}


while(p!=h)

{

printf("%d\n",p->score);

p = p->prev;

}

}





void initDList(DLIST *h,int a[])

{

DLIST *p;

int i=0;


for(p=h->next;p;p=p->next)

{

p->score = a[i];

i++;

}

return;

}



int getDListLen(DLIST *h)

{

DLIST *p = h->next;

int n=0;


while(p)

{

p = p->next;

n++;

}

return n;

}


void showSelectDList(DLIST *h,int score)

{

int n,i;

DLIST *p;

n = getDListLen(h);

p = h->next;

for(i =0;i < n;i++)

{

if(p->score >= score)

{

printf("%d\n",p->score);

}

p = p->next;

}

return;

}


void insertDListBack(DLIST *h,int data,int pos)

{

int i;

DLIST *p = h,*q;

int len = getDListLen(h);

if (pos > len)

{

pos = len;

}

for(i=0;i<pos;i++)

{

p=p->next;

}


q = (DLIST*)malloc(sizeof(DLIST));


q->score = data;


q->next = p->next;


if (p->next)

{

p->next->prev = q; // add

}


p->next = q;


q->prev = p;  // add


}



void delDList(DLIST *h,int pos)

{

int i;

DLIST *p = h,*q;

int len = getDListLen(h);

if (pos > len)

{

pos = len;

}

for(i=0;i<pos;i++)

{

p=p->next;

}


p->prev->next = p->next;

if(p->next)

{

p->next->prev = p->prev;

}

free(p);


}


void reverse(DLIST *h)// ≤»επ–Ú

{

DLIST *p,*q;

p = h->next;

h->next =NULL;


while (p)

{

q = p;

p = p->next;

q->next = h->next;

h->next = q;


q->prev = h;

if (q->next)

{

q->next->prev = q;

}

}

}


void destroy(DLIST *h)

{

DLIST *p = h;

while(p->next)

{

delDList(h,1);

}


free(p);

}



int main()

{

DLIST *head,*p,*q,*tmp;

int a[]={70,80,50,100};

int i,n;


printf("\n-------------------------------\n");

printf("FIRST:\n");

head = creatDList(4);

initDList(head,a);

showDList(head);

printf("-------------------------------\n");


printf("SECOND:\n");

p = head->next;

n = getDListLen(head);


for(i=1;i<=n;i++)

{

if(50 == p->score)

{

insertDListBack(head,60,i);

break;

}

p = p->next;

}

showDList(head);

printf("-------------------------------\n");


printf("THIRD\n");

p = head->next;

n = getDListLen(head);

for(i=1;i<=n;i++)

{

if(50 == p->score)

{

delDList(head,i);

break;

}

p = p->next;

}

showDList(head);

printf("-------------------------------\n");


printf("FOURTH\n");

p = head->next;

while(p)

{

if(p->score >75)

{

printf("%d\n",p->score);

}

p = p->next;

}

printf("-------------------------------\n");


printf("FIFTH\n");

n =sizeof(DLIST)-8;

tmp = (DLIST *)malloc(n);

p = head->next;

while(p)

{

q = p->next;

while(q)

{

if(p->next < q->next)

{

memcpy(tmp,p,n);

memcpy(p,q,n);

memcpy(q,tmp,n);

}

q = q->next;

}

p = p->next;

}

free(tmp);

showDList(head);

printf("-------------------------------\n");


printf("SIXTH\n");

reverse(head);

showDList(head);

printf("-------------------------------\n");


destroy(head);

showDList(head);

return0;

}







0 0