首页 > 其他分享 >2008秋-计算机软件基础-单链表完整示例

2008秋-计算机软件基础-单链表完整示例

时间:2023-11-08 12:06:55浏览次数:30  
标签:Node head 单链 示例 int next key 2008 NULL

/*---------------------------------------------------------

 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);

}

标签:Node,head,单链,示例,int,next,key,2008,NULL
From: https://blog.51cto.com/emanlee/8247621

相关文章

  • 2008秋-计算机软件基础-多关键字排序
    /*多关键字排序:先按总分由高到低排序,若总分相同则按数学成绩由高到低排序,若总分和数学成绩都相同,则按英语成绩由高到低排序。*/#include<stdio.h>structstudent{intxuehao;charxingming[10];intshuxue;intyingyu;intyuwen;intzongf......
  • SQL Server 2005透视表运算符PIVOT应用示例
    SQLServer2005行列转换 有用SQL写过交叉报表的,往往都比较头痛,还好现在SQL2005中提供了新的PIVOT操作符,可以很简单地写出交叉数据查询。正好前两天在研究ORACLE最新的11G版本提供的新特性,发现ORACLE11G也同样推出这个新PIVOT,而且语法格式也几乎是一样的,呵,看来这些主流的数据库都......
  • 2008秋季-计算机软件基础-有序表合并 教材 P79, ex3
    /*Author:EmanLee*//*计算机软件基础教材P79,ex3*/#include<stdio.h>#include<stdlib.h>intinsert(inta[],intarrayLength,int*listLength,intx){inti,j;if(*listLength+1==arrayLength)return0;/*fail*/for(i=0;i<*list......
  • 2008秋季-计算机软件基础- 线性表顺序存储 - 菜单
    /*2008-10-27*//*tod:删除,修改,参考:教材P63-67*/#include<stdio.h>#defineN1typedefstructstudent{charxuehao[10];charxingming[10];intchengji;}S;voidxianshicaidan(){printf("\n1-Initialization.\n");......
  • 2008秋季-计算机软件基础-0901课堂用例
    #include<stdio.h>voidupdate(intxiabiao,intb[],intxinshu);voidcharu(intweizhi,intb[],intcharushu,intshuzuchang);voidmain(){/*顺序存储的线性表-顺序表*/inta[5]={1,2,4,5};inti;intweizhi=2;/*for(i=......
  • 2008秋季-计算机软件基础-0903课堂用例(1)
    #include<stdio.h>voidupdate(intxiabiao,intb[],intxinshu);voidcharu(intweizhi,intb[],intcharushu,intshuzuchang);voidshanchu(intweizhi,intb[],int*changdu);voidshuchu(intaa[],intbiaochang);voidchazhao(int......
  • C语言程序设计 求阶乘递归函数调用示例
    ......
  • C语言程序设计 回答同学提出的几个疑问 2008
    1,例4.11中的K=1时什麽意思?K用来表示是否是空格字符(注:不是空字符,而是空格字符)0表示空格字符1表示非空格字符  2,例3.17看不懂呀?这一题不难。如果看不懂需要加把劲了。=12时就是按第三个式子算得呀。是有逻辑错误的。1<=x<10就有明显的错误,因为这是数学中的写法,不是C语言中的写法。......
  • 2008秋-计算机软件基础-实验三 参考源程序
    实验三参考源程序//软件基础教材79页习题6答案//EmanLee#include<stdio.h>#include<stdlib.h>#definem5//队列容量//定义队列的结构structqueue{intseq[m];//队列元素intquelen;//队列中元素个数intrear;//队列尾指针};//初始化队列......
  • 2008秋-计算机软件基础- 实验二 参考源程序
    实验二参考源程序/*Author:EmanLee,计算机软件基础教材P79,ex4设有一头为head的带头结点的单链表,其数据域为整形数据且递增有序。试写一算法,将元素插入链表适当的位置,以保持链表的有序性。*/#include<stdio.h>#include<stdlib.h>typedefintDataType;struc......