AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > 数据结构

链表基本操作的程序实现

51自学网 http://www.51zixue.net

原帖及讨论:http://bbs.bccn.net/thread-130712-1-1.html

#include<stdio.h>
#include<malloc.h>

typedef struct List_Node{
    int info;
    struct List_Node *next;
  }node;//结点结构体

/******************************/
/* 尾插法建立带头结点的单链表 */
/******************************/
node* Creat_Node()
{
    node *head,*pre,*p;
    int x;
    head=(node*)malloc(sizeof(node));;
    head->next=NULL;
    pre=head;
    printf("输入各结点的值,以0结束:");
    while(EOF!=(scanf("%d",&x))&&x!=0)
    {
        p=(node*)malloc(sizeof(node));
        p->info=x;
        p->next=pre->next;
        pre->next=p;
        pre=pre->next;
    }
    return head;
}

/******************************/
/* 头插法建立带头结点的单链表 */
/******************************/
node* Build_Node()
{
    node *head,*p;
    int x;
    head=(node*)malloc(sizeof(node));;
    head->next=NULL;
    printf("输入各结点的值,以0结束:");
    while(EOF!=(scanf("%d",&x))&&x!=0)
    {
        p=(node*)malloc(sizeof(node));
        p->info=x;
        p->next=head->next;
        head->next=p;
    }
    return head;
}


/******************************/
/*         打印单链表         */
/******************************/

void Print_Node(node *head)
{
    node *p=head->next;
    printf("输出该链表:");
    while(p)
    {
        printf("%-5d--->",p->info);
        p=p->next;
    }
    if(p==NULL)
    {
        printf("^/n/n/n");
    }
}

 

#include"Head_Node.h"

int Count_Node(node *head)
{
    node *p=head->next;
    int num=0;
    while(p!=NULL)
    {
        num++;
        p=p->next;
    }
    return num;
}

int main()
{
    node *head;
    head=Creat_Node();
    Print_Node(head);
    printf("结点个数为:%d/n",Count_Node(head));
    return 0;
}

<

 

 

 
上一篇:无向图转换&遍历&MST  下一篇:有向图转换&遍历&拓扑&最短路径