双链表

 

#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
 using namespace std;
 
//双链表的结构定义
 
typedef struct DuList
 {
     char data;
     struct DuList *next;
     struct DuList *pre;
 }dnode;
 
dnode *head;//双链表的标识
 
//创建
 
dnode *creat(dnode *head)
 {
     dnode *p,*s;
     bool judge = 1;
     char x;
     head = (dnode*)malloc(sizeof(dnode));
     p = head;
     while(judge)
     {
         scanf("%c",&x);
         if(x != '\n')
         {
             s = (dnode*)malloc(sizeof(dnode));
             s->data = x;
             p->next = s;
             s->pre = p;
             p = s;
             judge = 1;
         }
         else
             judge = 0;
     }
     head = head->next;
     head->pre = NULL;
     p->next = NULL;
     return(head);
 }
 
//打印
 
dnode *DuList_display(dnode *head)
 {
     dnode *p;
     int i = 0;
     p = head;
     while(p != NULL)
     {
         printf("%c",p->data);
         p = p->next;
     }
     return(head);
 }
 
 
 
//求长
 
int DuList_len(dnode *head)
 {
     dnode *p;
     int i = 0;
     p = head;
     while(p != NULL)
     {
         p = p->next;
         i++;
     }
     return i;
 }
 
//插入
 
dnode *DuList_insert(dnode *head)
 {
     dnode *p,*temp;
     char x;//用接收插入的字符
     int k = 1;
     int i; //用于接收插入的位置
     int len;//用于接收双链表的长度
     len = DuList_len(head);
     p = head;
     printf("please input the data 'x' and the position 'i':\n");
     scanf("%c,%d",&x,&i);
     if(i<0||i>len)
     {
         printf("the position you input is error!\n");
         return false;
     }
     if(0 == i)//插入在第一个元素之前
     {
         temp = (dnode*)malloc(sizeof(dnode));
         temp->data = x;
         temp->next = head;
         head->pre = temp;
         head = temp;
         DuList_display(head);
         return(head);
     }
     if(len == i)//插入在最后一个元素之后
     {
         temp =(dnode*)malloc(sizeof(dnode));
         temp->data = x;
         while(k != len)
         {
             p = p->next;
             k++;
         }
         p->next = temp;
         temp->pre =p;
         temp->next = NULL;
         DuList_display(head);
         return(head);
     }
     else //插入在某个元素之后
     {
         int k = 1;
         while(k != i)
         {
              p = p->next;
              k++;
         }
         temp = (dnode*)malloc(sizeof(dnode));
         temp->data = x;
         temp->next = p->next;
         p->next->pre = temp;
         p->next = temp;
         temp->pre = p;
     }
     DuList_display(head);
     return(head);
 }
 
//删除
 
dnode *DuList_del(dnode *head)
 {
     dnode *p,*s;
     p = head;
     int i,len;
     int k = 1;
     int j = 1;
     len = DuList_len(head);
     printf("\n");
     printf("now the len of List is :len=%d\n",len);
     printf("please input the position you want to delete:i=");
     scanf("%d",&i);
     if(i<0||i>len)
     {
         printf("the position you input is error!");
         return false;
     }
     if(1 == i)//删除第一个元素
     {
         head = head->next;
         head->pre =NULL;
         free(p);
         DuList_display(head);
         return(head);
     }
     if(len == i)//删除最后一个元素
     {
         while(k !=len)
         {
             p = p->next;
             k++;
         }
         p->pre->next = NULL;
         p->pre = NULL;
         free(p);
     }
     else //删除除了第一个和最后一个元素之外的元素
     {
         while(k != i)
         {
             s = p;
             p = p->next;
             k++;
         }
         s->next = p->next;
         p->next->pre = s;
         p->next = NULL;
         p->pre = NULL;
         free(p);
     }
     DuList_display(head);
     return(head);
 }
       
 int main()
 {
     int len;
     head = DuList_creat(head);
     head = DuList_display(head);
     len = DuList_len(head);
     printf("\n");
     printf("len=%d",len);
     printf("\n");
     head = DuList_insert(head);
     head = DuList_del(head);
     return 0;
 }

 


 #include <malloc.h>
 #include "conio.h"
 #include "stdio.h"
 struct student         
 {
  int num;         
  int score;         
  struct student* next; 
  struct student *prev;     
 };
 typedef struct student stu;      
 stu* create(void);        
 void disp(stu* head);       
 stu* insert(stu* head);       
 stu* del(stu* head, int num);      
 void freeAll(stu* head);      
 
stu* create(void)
 {
  stu* head=NULL;       
  stu* tail=NULL;       
  head=(stu*)malloc(sizeof(stu));    
  if(head==NULL)       
  {
   printf("头结点内存申请失败");
   return NULL;
  }
  head->next=NULL;            
  tail=head;      
  stu* pNewElement=NULL; 
  int n,s;      
  while(1)
  {
   printf("请输入该同学的学号和成绩\n");  
   scanf("%d%d",&n,&s);     
   if(n>0 && s>0)       
   {
    pNewElement=(stu*)malloc(sizeof(stu));
    if(pNewElement==NULL)    
    {
     printf("结点内存申请失败");
     return NULL;
    }
    pNewElement->num=n;    
    pNewElement->score=s;
    pNewElement->next=NULL;
    tail->next=pNewElement;
    pNewElement->prev=tail;    
    tail=pNewElement; 
    
   }
   else      
    break;
  }
  pNewElement=head;
  head=head->next;
  head->prev=NULL;       
  free(pNewElement);       
  return head;
 }
 void disp(stu* head)
 {
  stu* p=head;        
  while(1)
  {
   if(p==NULL)       
          return;
   printf("(学号:%d,成绩:%d)\n",p->num,p->score);
   p=p->next;       
  }
 }
 
stu* insert(stu* head)
 {
  int n,s;         
  stu* Einsert=NULL,*E1=NULL,*E2=NULL;
  E1=head;
  while(1)
  {
   printf("请输入待插入同学的学号和成绩\n"); 
   scanf("%d%d",&n,&s);     
   if(n>0 && s>0)       
   {
    Einsert=(stu*)malloc(sizeof(stu));  
    if(Einsert==NULL)     
    {
     printf("结点内存申请失败");
     return NULL;
    }
    Einsert->num=n;     
    Einsert->score=s;
    while(n>E1->num&&E1->next!=NULL)
    {
     E2=E1;
     E1=E1->next;
    }
    if(n<=E1->num)
    {
     if(head==E1)     
     {
      Einsert->next=E1;
      E1->prev=Einsert;
      head=Einsert;
     }
     else       
     {
      E2->next=Einsert;
      Einsert->next=E1;
      Einsert->prev=E1->prev;
      E1->prev=Einsert;
     }
    }
    else        
    {
     E1->next=Einsert;
     Einsert->prev=E1;
     Einsert->next=NULL;
    }
   }
   else      
    break;
  }
  return head;
 }
 
stu* del(stu* head, int num)      
 {
  stu *p1=NULL,*p2=NULL;     
  p1=head;
  while(num!=p1->num&&p1->next!=NULL)   
  {
   p2=p1;
   p1=p1->next;
  }
  if(num==p1->num)       
  {
   if(p1==head)       
   {
    head=p1->next;
    head->prev=NULL;
    free(p1);
   }
   else       
   {
    p2->next=p1->next;
    free(p1);
   }
  }
  else          
   printf("找不到待删除的结点\n");
  return head;        
 }
 
 
void freeAll(stu* head)
 {
  stu* p=NULL,*q=NULL;
  p=head;
  while(p->next!=NULL)
  {
   q=p->next;
   p->next=q->next;      
   free(q);        
  }
  free(head);        
 }
 

 main()         
 {
  stu* head=NULL;       
  head=create();        
  printf("创建完毕后的链表为:\n");
  disp(head);        
  head=insert(head);       
  printf("插入结点后的链表为:\n");
  disp(head);        
  printf("请输入要删除的结点学号:\n");
  int xh=0;
  scanf("%d",&xh);
  head=del(head,xh);       
  printf("删除学号为%d的结点后:\n",xh);
  disp(head);                
  freeAll(head);        
  getch();         
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值