zoukankan      html  css  js  c++  java
  • 2008秋计算机软件基础单链表完整示例

    /*---------------------------------------------------------
     Title: Completed Simple Linked List
     Author: Eman Lee
     Date: Oct 22, 2008
     Fuction: Operation on Linked Stored Linear List.
                This is a completed simple sample.
     It is related to Section 2.2.4 in our textbook.(p56-63) 
    ----------------------------------------------------------
    */
    #include
    <stdio.h>
    #include
    <stdlib.h>
    struct nodetype//Define node 定义节点
    {
     
    int data;//Data field
     struct nodetype * next;//Pointer field which point to the next node
    };
    typedef 
    struct nodetype Node;
    Node 
    * InitialLinkList()//Initialize a Linked List,and return the head pointer
    {
     
    struct nodetype * head;
     head
    =(Node *)malloc(sizeof(Node));//
     head->next=NULL;
     
    return head;
    }

    void CreateLinkListInRear(Node * head,
        
    int a[], int L)
    {
    //Insert new node in the rear of link list.
        int i;
        Node 
    * t,* rear;
        rear
    =head;
        
    for(i=0;i<L;i++)
        {
            t
    =(Node *)malloc(sizeof(Node));
            t
    ->data=a[i];
            t
    ->next=NULL;
            rear
    ->next=t;
            rear
    =t;
        }
    }
    void CreateLinkListInHead(Node * head,
                              
    int a[], int L)
    {
    //Insert new node in the front of link list.
        int i;
        Node 
    * t;

        
    for(i=0;i<L;i++)
        {
            t
    =(Node *)malloc(sizeof(Node));
            t
    ->data=a[i];
            t
    ->next=head->next;
            head
    ->next=t;

        }
    }
    Node 
    * SearchInLinkList(Node * head, int x)
    {   
    //Search x in link list.
        Node * p=head->next;
        
    while(p!=NULL)
        {
        
    if(p->data==x)
            
    return p;
        
    else
            p
    =p->next;
        }
        
    return NULL;
    }

    void InsertNumberIntoLinkList(Node * head,
                                  
    int key,int x)
    {
      
    //Insert x after key in the linked list.
      
    //Need two pointers if insert x before key.
      Node * loc,*t;
      
    if((loc=SearchInLinkList(head,key))!=NULL)
        {
            t
    =(Node *)malloc(sizeof(Node));
            t
    ->data=x;
            t
    ->next=loc->next;
            loc
    ->next=t;
        }
       
    else
         printf(
    "\nNot Found, Insert failed!\n");
    }

    void InsertNumber(Node * head,
                                  
    int key,int x)
    {  
    //Insert x before key in the linked list.
      Node * f,*r,*t;
      f
    =head;
      r
    =head->next;
      
    while(r!=NULL && r->data!=key)//Find key
      {
        r
    =r->next;
        f
    =f->next;  
      }
      
    if(r!=NULL) //Found key
      {   
        t
    =(Node *)malloc(sizeof(Node));//Insert x
        t->data=x;
        f
    ->next=t;
        t
    ->next=r;
        printf(
    "Found key. Inserted Successfully.");
      }
      
    else //Not Found key
      {
        printf(
    "Not Found key.Not inserted.");
      }
    }

    void DeleteNumberFromLinkList(Node * head,int key)
    {
     
    //Delete x in the linked list.need two pointers
     Node * f,*r;
      f
    =head;
      r
    =head->next;
      
    while(r!=NULL && r->data!=key)//Find key
      {
        r
    =r->next;
        f
    =f->next;  
      }
      
    if(r!=NULL) //Found key
      { 
        f
    ->next=r->next;
        free(r);  
        printf(
    "Found key. Inserted Successfully.\n");
      }
      
    else //Not Found key
      {
        printf(
    "Not Found key.Failde.\n");
      }

    }

    void PrintIntegerLinkList(Node * head)
    {
    //Show nodes on the screen.
        Node *t;
        t
    =head->next;
        
    while(t!=NULL)
        {
            printf(
    "%d ",t->data);
            t
    =t->next;
        }
        printf(
    "\n");
    }

    void main()
    {
     Node 
    *head,*head2;
     
    //int x[5]={1,2,3,4,5};
     int y[5]={4,5,6,7,8};
     
    //head=InitialLinkList();
     head2=InitialLinkList();
     CreateLinkListInHead(head2,y,
    5);
     
    //CreateLinkListInRear(head,x,5);
     PrintIntegerLinkList(head2);
     
    //PrintIntegerLinkList(head);
     if(SearchInLinkList(head2,88)!=NULL)
         printf(
    "\nFound\n");
     
    else
         printf(
    "\nNot Found\n");
     
    //InsertNumberIntoLinkList(head2,7,33);
     InsertNumber(head2,4,44);
     DeleteNumberFromLinkList(head2,
    5);
     PrintIntegerLinkList(head2);
    }
  • 相关阅读:
    学习方法
    仿知乎Android端回答UI
    【LeetCode】:二叉树的Max,Min深度
    LeetCode:二叉树的前、中、后序遍历
    Caffe学习系列(四)之--训练自己的模型
    后端开发--之文件上传
    Python——轻量级web服务器flask的学习
    Django 部署(Apache下)
    Caffe学习系列(三)Docker安装及一些问题的记录
    Caffe学习系列(二)Caffe代码结构梳理,及相关知识点归纳
  • 原文地址:https://www.cnblogs.com/emanlee/p/1316671.html
Copyright © 2011-2022 走看看