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

本文介绍了一个双向链表的数据结构实现,包括创建、显示、插入、删除等基本操作,并通过一个示例程序展示了这些功能的应用。代码中还包含了逆序、销毁链表等额外功能。

#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;

}

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zwarwolf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值