单链表 基本操作

  1 #include<iostream.h>
  2 #include<stdlib.h>
  3 /*
  4 完成简单链表的定义(向前插入节点的方式)和初始化工作,然后输出链表数据。
  5 struct Node{};//节点。
  6 如输入1 2
  7 3 4
  8 5 6
  9 输出5 6
 10 3 4
 11 1 2
 12 */
 13 struct Node
 14 {
 15     float x;
 16     float y;
 17     struct Node *Next;
 18 };
 19 main(void)
 20 {
 21   struct Node *create(struct Node *head);    //声明Node链表建立函数create
 22   struct Node *insert(struct Node *head);    //声明Node链表建立函数insert,插入节点
 23   struct Node *delet(struct Node *head);    //声明Node链表建立函数delet,删除节点
 24   void print(struct Node *head);     //声明输出函数
 25   struct Node *head;     //定义节点头指针
 26   head=NULL;     //初始化为空
 27   head=create(head);     //动态建立链表
 28   print(head);     //打印链表
 29   head=insert(head);     //插入一个节点
 30   print(head);     //打印新链表
 31   head=delet(head);     //删除一个节点
 32   print(head);     //打印新链表
 33 }
 34 /**********************************************************/
 35 struct Node *create(struct Node *head)/*参数类型为结构体Node类型的头指针*/
 36 {
 37   struct Node *p1,*p2;
 38   //定义两个Node类型的指针
 39   p1=(struct Node*)malloc(sizeof(struct Node));
 40   p2=(struct Node*)malloc(sizeof(struct Node));/*动态申请两块Node大小的内存指针*/
 41   cin>>p1->x>>p1->y;
 42   p1->Next=NULL;
 43   head=p1;     //处理只有链表只有一个节点的情况
 44   if(p1->x==0)
 45   {
 46     return head;
 47   }
 48   while(1)
 49   {
 50     cin>>p2->x>>p2->y;     //读入新节点数据
 51     if(p2->x!=0)
 52     {
 53       p2->Next=p1;     //在原有链表之前插入节点
 54       head=p2;     //头指针前移
 55       p1=head;
 56     p2=(struct Node*)malloc(sizeof(struct Node));/*重新申请一块内存放新节点*/
 57 
 58     }
 59     else
 60     {
 61       break;     //如果输入新节点的横坐标x=0,则终止。新节点不计入到链表中
 62     }
 63   }
 64   return head;
 65 }
 66 /**************************************************************/
 67 void print(struct Node *head)    //输出函数
 68 {
 69   struct Node *temp;
 70   temp=head;
 71   while(temp!=NULL)
 72   {
 73     cout<<temp->x<<" "<<temp->y<<endl;
 74     temp=temp->Next;
 75   }
 76 }
 77 /**************************************************************/
 78 struct Node *insert(struct Node *head)/*插入节点*/
 79 {
 80   struct Node *p1,*p2,*temp;
 81   int n;
 82   p1=(struct Node*)malloc(sizeof(struct Node));    //动态申请内存
 83   temp=(struct Node*)malloc(sizeof(struct Node));    //动态申请内存
 84   cout<<"请选择在第几个节点后插入新节点:n=";
 85   cin>>n;
 86   cout<<"请输入新节点数据:";
 87   cin>>p1->x>>p1->y;
 88   //将链表头指针赋给p2
 89   p2=head;
 90   //找到要插入的节点
 91   for(int i=1;i<n;i++)
 92   {
 93     p2=p2->Next;
 94   }
 95   temp->Next=p2->Next;    //将要插入节点处的后一个节点的地址存放在temp里
 96   p2->Next=p1;     //让p2的Next里存放新节点p1的地址,即指向p1
 97   p1->Next=temp->Next;    //将临时节点temp里存放的地址赋给新节点P1的Next,使p1指向下一个节点
 98   return head;
 99 }
100 /**************************************************************/
101 struct Node *delet(struct Node *head)/*删除节点*/
102 {
103   struct Node *p2,*temp;
104   temp=(struct Node*)malloc(sizeof(struct Node));    //动态申请内存
105   int n;
106   cout<<"请选择删除第几个节点:n=";
107   cin>>n;
108   //将链表头指针赋给p2
109   p2=head;
110   //找到要删除的节点前一个节点
111   for(int i=1;i<n;i++)
112   {
113     p2=p2->Next;    
114   }
115   temp=p2->Next;
116   p2->Next=temp->Next;
117   delete temp;//delete删除了指针指向的内存里变量
118   return head;
119 }

 

posted @ 2014-12-06 17:00  mt.luo  阅读(143)  评论(0)    收藏  举报