2008秋季-计算机软件基础-0908课堂用例(2)

#include<stdio.h>
#include
<stdlib.h>
struct nodetype
{
    
int data;
    
/* data数据项用于存放结点的数据值 */
    
struct nodetype *next; 
    
/* next数据项存放下一个结点的指针 */
};
struct nodetype * InitialLinkList ()
{
 
struct nodetype * head;
 head
=(struct nodetype *)malloc(sizeof(struct nodetype ));//
 head->next=NULL;
 
return head;
}

void CreateLinkListInRear(struct nodetype * head, int a[], int n)
{   
int i;  struct nodetype * temp,* rear;
    rear
=head;
    
for(i=0;i<n;i++)
    {
        temp
=(struct nodetype *)malloc(sizeof(struct nodetype));
        temp
->data=a[i];
        temp
->next=NULL;
        rear
->next=temp;
    rear
=temp;
    }
}

void CreateLinkListInHead(struct nodetype * head,
                          
int a[], int n)
{   
int i;struct nodetype * temp,*front;
    
for(i=0;i<n;i++)
    {
        temp
=(struct nodetype *)malloc(
            
sizeof(struct nodetype));
        temp
->data=a[i];
        temp
->next=head->next;
        head
->next=temp;
    }
}

void printlinklist(struct nodetype * head)
{
  
struct nodetype * p;
  p
=head->next;
  
while(p!=NULL)
  {
    printf(
" %d ",p->data);
    p
=p->next;
  }
}

void main()
{
  
struct nodetype * head;
  
int a[3]={3,2,1};
  head
=InitialLinkList();
  
//CreateLinkListInRear(head,a,3);
  CreateLinkListInHead(head,a,3);
  printlinklist(head);
}

 

参看: http://www.cnblogs.com/emanlee/archive/2007/09/10/888942.html

posted @ 2008-09-08 16:52  emanlee  阅读(223)  评论(0编辑  收藏  举报