首页 > 编程语言 >2008秋-计算机软件基础- 实验二 参考源程序

2008秋-计算机软件基础- 实验二 参考源程序

时间:2023-11-08 10:38:08浏览次数:35  
标签:head NodeType head2 temp DataType next 计算机软件 2008 源程序

实验二 参考源程序
/* Author : Eman Lee,
计算机软件基础 教材 P79, ex4

设有一头为head的带头结点的单链表,其数据域为整形数据且递增有序。

试写一算法,将元素插入链表适当的位置,以保持链表的有序性。
*/

#include<stdio.h>

#include<stdlib.h>


typedef int DataType;
struct nodetype//Define node 定义节点
{

 DataType data;//Data field
 struct nodetype * next;//Pointer field which point to the next node
};


typedef struct nodetype NodeType;//Give struct nodetype a new name NodeType


NodeType * InitialLinkList()//Initialize a Linked List,and return the head pointer
{

 NodeType * head;

 head=(NodeType *)malloc(sizeof(NodeType));//
 head->next=NULL;

 return head;

}

void CreateLinkListInRear(NodeType * head, DataType numbers[], int LengthOfNumbers)

{//Insert new node in the rear of link list.
    int i;

    NodeType * temp,* rear;

    rear=head;

    for(i=0;i<LengthOfNumbers;i++)

    {

        temp=(NodeType *)malloc(sizeof(NodeType));

        temp->data=numbers[i];

        temp->next=NULL;

        rear->next=temp;

        rear=temp;

    }

}

void CreateLinkListInHead(NodeType * head, DataType numbers[], int LengthOfNumbers)

{//Insert new node in the front of link list.
    int i;

    NodeType * temp,*front;

    //front=head;
    for(i=0;i<LengthOfNumbers;i++)

    {

        temp=(NodeType *)malloc(sizeof(NodeType));

        temp->data=numbers[i];

        temp->next=head->next;

        head->next=temp;

        //rear=temp;
    }

}


NodeType * SearchInLinkList(NodeType * head, DataType x)

{//Search x in link list.
    NodeType * p=head->next;

    while(p!=NULL)

    {

    if(p->data==x)

        return p;

    else

        p=p->next;

    }

    return NULL;

}

void InsertNumberIntoLinkList(NodeType * head,DataType key,DataType x)

{

 //Insert x after key in the linked list.

    //Need two pointers if insert x before key.
    NodeType * location,*temp;

 if((location=SearchInLinkList(head,key))!=NULL)

    {

        temp=(NodeType *)malloc(sizeof(NodeType));

        temp->data=x;

        temp->next=location->next;

        location->next=temp;

        printf("\n链表中插入元素%d",x);

    }

 else

     printf("\nNot Found, Insert failed!\n");

}

void DeleteNumberFromLinkList(NodeType * head,DataType x)

{

 //Delete x in the linked list.

 //need two pointers

    //todo
}

void PrintIntegerLinkList(NodeType * head)

{//Show nodes on the screen.
    NodeType *temp;

    temp=head->next;

    printf("\n显示所有元素:");

    while(temp!=NULL)

    {

        printf("%d ",temp->data);

        temp=temp->next;

    }

    printf("\n");

}

int InsertIntoSortedList(NodeType * head, int x)

{

 NodeType * f,*r,*temp;

 f=head;

 r=head->next;

 while(r!=NULL)

  {

    if(r->data>=x)

    {

      temp=(NodeType *)malloc(sizeof(NodeType));

      temp->data=x;

      temp->next=r;

      f->next=temp;

      printf("\n链表中插入元素%d",x);

      return 1;

    }

    else

    {

     f=r;

     r=r->next;

    }

  }

  temp=(NodeType *)malloc(sizeof(NodeType));

  temp->data=x;

  temp->next=NULL;

  f->next=temp;

  printf("\n链表中插入元素%d",x);

  return 1;

  

}

void main()

{

 NodeType *head,*head2;

 //DataType x[5]={1,2,3,4,5};
 DataType y[5]={8,7,6,5,4};

 //head=InitialLinkList();
 head2=InitialLinkList();

 CreateLinkListInHead(head2,y,5);

 //CreateLinkListInRear(head,x,5);
 PrintIntegerLinkList(head2);

 //PrintIntegerLinkList(head);
 if(SearchInLinkList(head2,88)!=NULL)

     printf("\nFound 88\n");

 else

     printf("\nSearch 88,Not Found\n");

 //InsertNumberIntoLinkList(head2,7,33);

 //PrintIntegerLinkList(head2);


 InsertIntoSortedList(head2,0);

 PrintIntegerLinkList(head2);


 InsertIntoSortedList(head2,6);

 PrintIntegerLinkList(head2);


 InsertIntoSortedList(head2,9);

 PrintIntegerLinkList(head2);

}

标签:head,NodeType,head2,temp,DataType,next,计算机软件,2008,源程序
From: https://blog.51cto.com/emanlee/8245747

相关文章

  • 2008秋-计算机软件基础-冒泡排序
    /*Title:冒泡排序Author:emanlee算法功能:冒泡排序算法实现将一个长度为n的线性表r上的所有元素按关键字升序排列。*/#include<stdio.h>voidbubblesort(intr[],intn){/*elementsarestoredinr[1]tor[n]*/inti,j,flag;inttemp;flag=1;i......
  • 2008秋-计算机软件基础-第四章习题参考答案 P131 ,习题4,5
    Author:EmanLee 计算机软件基础,教材P131,第4题参考答案(1)查找e的过程abcdefghLow=1Mid=4High=8abcdefghLow=5Mid=6High=8abcdefghLow=5Mid=5High=5查找成功(2)查找f的过程abcdefghLow=1Mid=4High=8abcdefghLow=5Mid=6High=8查找成功(3)查找h的过程abcdefghLow=1Mid=4High=8abcdefghLo......
  • 2008秋-计算机软件基础-第四章习题参考答案 P131 ,习题6之一
    Author:EmanLee P131,第6题之一参考答案(1)      直接插入排序初始状态第一趟第二趟第三趟第四趟第五趟第六趟第七趟第八趟第九趟(2)      冒泡排序初始状态第一趟 第二趟第三趟第四趟第五趟 第六趟 第七趟第八趟第九趟53       12    12 ......
  • 2008秋-计算机软件基础-快速排序
    快速排序c语言源程序之一//快速排序//使用递归调用来实现快速排序//Author:emanlee/*QuickSort*/#include<stdio.h>voidquick_sort(intdata[],intlow,inthigh){inti,j,pivot;if(low<high){pivot=data[low];......
  • 2008秋-计算机软件基础-第四章习题参考答案 P131 ,习题6之二
    P131,第6题之二参考答案(1)      快速排序(第一趟)初始状态Pivot=5353871261706827652135J向左扫描ij第一次交换后358712617068276521ijI向右扫描,第二次交换后351261706827652187ijJ向左扫描,第三次交换后352112617068276587ijI向右扫描,第四次交换352112706827656187ijJ向左扫描35......
  • 2008秋-计算机软件基础-直接插入排序
    //==============================================//直接插入排序(StraightSelectionSort)//Author:emanlee//直接插入排序c语言源程序//==============================================#defineN5#include<stdio.h>//显示元素voidDisplay(inta[],intelem......
  • 2008秋-计算机软件基础-简单选择排序
    //===========================================//简单选择排序//Author:EmanLee//===========================================#include<stdio.h>#defineN10voidDisplay(int*a,intn){inti;for(i=0;i<n;i++){printf(&qu......
  • 2008毕业设计-毕业设计需要了解的信息
     学号:姓名:系别:专业:班级:联系电话:常用邮箱:备用联系方式家庭电话:宿舍电话:备用邮箱: 选择的题目:在就业单位做毕业设计,还是在学校做毕业设计: (在其后填写是或否):是否有PC:是否已过四级:是否有需要补考课程:是否已签订工作: 工作地点: 是否学习过(在其后填写是或否):CC++JavaC#2003C#2005VB......
  • 2008秋-计算机软件基础-实验内容
    ------------------------------------------------------------------题目:实验C语言综合复习目的:掌握函数,结构体,指针的使用,预习线性表的顺序存储(顺序表)实验内容:1看PPT, 函数,结构体,指针章节。要求: -----------------------------------------------------------......
  • 2008秋季-计算机软件基础-线性表的顺序存储(顺序表)
    引例:在一维数组中插入和删除元素//在一维数组中插入和删除元素//2008-8-31#include<stdio.h>voidmain(){//在一维数组位置Location处插入EintList[10]={0,1,2,3,4,5};intListLength=6;//表长intE=6;//被插入的元素inti;//循环变量intLocati......