数据结构与算法分析100例之001(我自己敲的,我是菜鸟~!)

//本程序有很多需要改进的地方,我自己都清楚,但第一篇就写成这样吧!

//以后改进,写程序应该是从简单到复杂的过程,本人是菜鸟,请您抱着看小学生作文的态度看这些帖子。

//原来很多东西贴的很齐,只是转到qq空间变得不齐整了,本人不知道怎样转换不改变格式,或许程序转成记事本格式又会自己对齐,

//但也不保证!

 

//本人自己写的,水平很低,纯属为了激发自己学习算法的兴趣@@@@@@@@@@@@@@@@@@@@@@@@|
//将写一百个小例子,分为五部分:1、基本数据结构 2、简单算法 3、高级数据结构@@@@@|
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%4、算法进阶 5、综合实例 @@@@|
//肯定刚开始好写,以后越来越难写,肯定有很多写的不好的地方 @@@@@@@@@@@@@@@@@@@@@|
//借用Linus的一句话:Just for fun(乐者为王)! @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|

/*linklist01.cpp*****************************************************************|
|*the first example of  linklists************************************************|
|*C++实现,Dev C++4.9.9.2编译通过(其实我用了C风格的C++,为了偷懒)**************|
|*欢迎提宝贵意见,本人是菜鸟! **************************************************|
|*数据结构与算法分析一百例之基本数据结构(编号001)******************************/


//本程序包括单链表创建,计算链表长度,打印链表,删除结点,插入结点%%%%%%%%%%%%%//
// 有兴趣的可以实现单链表逆制,链表排序,链表求中间结点,合并有序链表 %%%%%%%%%//
//参考书目:严蔚敏《数据结构(C语言版)》%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%//
//笑一笑!今天很好,明天会更好! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%//
 
 
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <conio.h>

using namespace std;

typedef struct NODE{//链表结点
        int data;
        struct NODE *next;
}node;

node *creat(){//创建链表,假定以数字0为结束创建
     node *head,*p,*s;
     int x,cycle=1;
     head=(node*)malloc(sizeof(node));
     p=head;
     while(cycle){
       cout<<"***************";
       cout<<"***************";
       cout<<endl;
       cout<<"please input the data:"<<endl;
       cin>>x;
       if(x!=0){
          s=(node*)malloc(sizeof(node));
          s->data=x;
          cout<<"The input data is:"<<endl;
          cout<<s->data<<endl;
          cout<<"***************";
          cout<<"***************";
          p->next=s;
          p=s;
          }
          else cycle=0;
          }        
          p->next=NULL;
          cout<<"***************";
          cout<<"***************";
          cout<<endl;
          return head;
}

int length(node *head){//计算链表长度值并返回
    int n=0;
    node *p;
    p=head;
    while(p->next!=NULL){
      n++;
      p=p->next;
    }
    cout<<endl;
    cout<<"The length of the linklist is :"<<n<<endl;
    cout<<"***************";
    cout<<"***************";
    cout<<endl;
}

void print(node *head){//打印链表中的每一个结点值
     node *p;
     p=head;
     while(p->next!=NULL){
       p=p->next;
       cout<<p->data<<endl;
     }
     cout<<"***************";
     cout<<"***************";
     cout<<endl;
}

node *deletenode(node *head,int num){//删除值为num的结点,此处未考虑没找到num的情况
    node *p,*q;
    p=head;
    while(p->next!=NULL){
         if(p->next->data==num){
            q=p->next;
            p->next=q->next;
            free(q);
          }
          else{
               p=p->next;
               }
    }
    return head;
}

node *insertnode(node *head,int num,int d){//在第num个结点后添加结点,结点值为d
     node *s,*p=head;
     s=(node*)malloc(sizeof(node));
     s->data=d;
     if(num<0||num>length(head)){
        cout<<"The wrong number!";
        return head;
     }
     for(int i=0;i<num;i++){
             p=p->next;
     }
     s->next=p->next;
     p->next=s;
     cout<<"The insert d is :"<<p->next->data<<endl;
     return head;
}
                               

int main(){
    node *L;
    L=creat();
    length(L);
    print(L);
    deletenode(L,3);
    length(L);
    print(L);
    insertnode(L,3,6);
    length(L);
    print(L);
    system("pause");//便于演示
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值