本文共 2214 字,大约阅读时间需要 7 分钟。
双向循环链表创建
typedef struct Node{ int x; struct node *prior,*next;};Node *create_DList(){ Node *p,*head,*l; int n,i,x; head = new Node; head->prior = head; head->next = head; p = head; scanf("%d",&n); for(i = 0; i < n; i++) { scanf("%d",&x); l = new Node; l->x = x; p->next = l; l->prior = p; l->next = head; head->prior = l; p = l; } return(head);}循环链表的创建
#include删除值为n的节点#include typedef struct node{ int x; struct node *prior,*next;}Node;//函数声明Node *create_DList();void out_DList(Node *l);int main(){ Node *l; l = create_DList(); printf("创建成功!"); out_DList(l);}//读取双向链表void out_DList(Node *p){ p = p->next; while(p) { printf("%5d",p->x); p = p->next; }}//创建双向链表Node *create_DList(){ Node *p,*head,*l; int n,i,x; head = (Node *)malloc(sizeof(Node)); head->x = 3; head->prior = NULL; head->next = NULL; p = head; scanf("%d",&n); for(i = 0; i < n; i++) { scanf("%d",&x); l = (Node *)malloc(sizeof(Node)); l->x = x; p->next = l; l->prior = p; p = l; //p->next = NULL; } return(head);}
node *deleted(node *p ,int n){ int flag=0; node *p1; p1=p->next ; while(p1) { if(p1->data == n) { if(p1->next !=NULL) { p1->next ->prior =p1->prior ; p1->prior ->next =p1->next ; } else//针对最后一个元素为删除结点 { p1->prior ->next =NULL; } break; } p1=p1->next; } return p;}插入节点(按大小顺序插入)
node * insert(node *p){ int kk; node *s,*ss,*mm; mm=(node *)malloc (sizeof(node));//申请一个空间用于存储新添加的结点 s=p; ss=s->next; scanf("%d",&kk); mm->data =kk; while(ss) { if(ss->data >= kk) { mm->next=ss; s->next=mm; mm->prior=s; ss->prior=mm; break; } else { s=s->next ; ss=ss->next ; } } if(s->next==NULL) { s->next=mm; mm->prior=s; mm->next=NULL; } return p;}
转载地址:http://obsgi.baihongyu.com/