沈阳垃圾清运公司:怎样创建一个线性链表(C语言)?

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/30 02:15:11
怎样创建一个线性链表,解释的清楚一点。带上源代码
typedef是宏定义的意思吧?那么typedey struct node是什么意思呢,还有struct node *next?

可以用头插法或尾插法
(下面用尾插法)
思想为:让你输入一串字符串, 为每个字符创建一个节点,添加到链表的后面.直到输入的字符为@为止.

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

typedef char datatype;
typedef struct node
{
datatype data;
struct node *next;
}linklist;
linklist *p,*q,*head;

main()
{
char c;
head = (linklist *)malloc(sizeof(linklist));
head->next = NULL;
p = head;
c = getchar();
while(c != '@')
{
q = (linklist *)malloc(sizeof(linklist));
q->data = c;
q->next = NULL;
p->next = q;
p = p->next;
c = getchar();
}
}

可以在 main() 最后加上
for(p=head->next; p!=NULL; p=p->next)
{
printf("%5c", p->data);
}
来测试结果,本人已经TC 2.0下面测试通过.

那么typedey struct node是什么意思呢
答: 是给结构“struct node”起的名字“node”,以后就可以直接用“node”定义变量,就像用“int”……

还有struct node *next?
答: 是链表中指向节点的指针

可以用头插法或尾插法
(下面用尾插法)
思想为:让你输入一串字符串, 为每个字符创建一个节点,添加到链表的后面.直到输入的字符为@为止.

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

typedef char datatype;
typedef struct node
{
datatype data;
struct node *next;
}linklist;
linklist *p,*q,*head;

main()
{
char c;
head = (linklist *)malloc(sizeof(linklist));
head->next = NULL;
p = head;
c = getchar();
while(c != '@')
{
q = (linklist *)malloc(sizeof(linklist));
q->data = c;
q->next = NULL;
p->next = q;
p = p->next;
c = getchar();
}
}

可以在 main() 最后加上
for(p=head->next; p!=NULL; p=p->next)
{
printf("%5c", p->data);
}
来测试结果,本人已经TC 2.0下面测试通过.

下面是我设计的一个链表的创建和遍历:/*结构体链表的创建和遍历*/
//本程序采用c++程序,设计到C++的地方,我在注释中,给你表示出来。
#include <iostream.h>//头文件声明,相当于#include <stdio.h>

struct Student//一个学生结构体的声明;
{
long number;
float score;
Student *next;//这是一个自身调用。注意是一个结构体的指针;
};

Student *head;//这是一个全局变量;下面有对它的使用;

Student *Create()//函数,用来创建链表;
{
Student *pS;//这个结构指针,是用来创建节点的;
Student *pEnd;//这个结构指针,是用来传递*pS给它的;
pS=new Student;//这是C++中的用法,相当于C
中的malloc函数;具体使用,请参考一下课本;
cin>>pS->number>>pS->score;//给pS输入值;CIN相当于C中的SCANF;
head=NULL;//这里是将头指针设为空值;
pEnd=pS;

while(pS->number!=0)//当输入的number值是0时,结束循环;
{
if(head==NULL)
head=pS;
else
pEnd->next=pS;

pEnd=pS;
pS=new Student;
cin>>pS->number>>pS->score;
}

pEnd->next=NULL;
delete pS;
return (head);
}

void ShowList(Student *head)
{
cout<<"now the items of list are:\n";
while (head!=NULL)
{
cout<<head->number<<","<<head->score<<endl;
head=head->next;
}
}

void main()
{
ShowList(Create());
}

# include <stdio.h>
# include <stdlib.h>
# define StopCondition  t!=0        //根据需求修改 

typedef int DataType;        //节点的数据域 的数据类型,视情况而定

typedef struct node
{
    DataType  data;
    struct node *next;
} Node;

typedef Node* List;




/*********根据需求,有可能变更的函数接口*********/ 
void  GetData(DataType *px);
void PrintData(DataType x);


/********标准链表接口******/

void PrintList(List li);
List CreatList();
void DestroyList(List li);



//测试main函数 
int main(void)
{
    
    List myList;
    myList = CreatList();    //创建链表 ,输入0就结束输入 
      
    PrintList(myList);      //打印 
    
    DestroyList(myList);    //销毁 
    
    return 0;






void  GetData(DataType *px)
{
    
    while(1!=scanf("%d",px))  ////
    {
        fputs("输入有误,请重新输入\n",stderr);
        while(getchar()!='\n')
        ;
    }
    
}

void PrintData(DataType x)
{
    
    printf("%d\n",x); //////
}


List CreatList()
{  //创建一个带头节点的链表
    Node * phead;
    Node *p1,*p2;
    DataType t;
    
    //创建头节点
    phead = (Node*)malloc(sizeof(Node));
    if(phead==NULL)
    {
        fputs("memory allocate error\n",stderr);
        exit(-1);
    }
    phead->next=NULL;
    
    //开始创建数据节点
    GetData(&t); //获取节点的数据
    while(StopCondition)
    {
        //开辟内存
        p1 = (Node*)malloc(sizeof(Node));
        if(p1==NULL)
        {
            fputs("memory allocate error\n",stderr);
            exit(-1);
        }
        //填补节点的数据(数据域和指针域)
        p1->data = t;
        p1->next=NULL;
        
        //链接
        if(phead->next ==NULL)
         phead->next = p1; 
         
        else
         p2->next = p1;
         
        
        //重置 
        p2=p1;
        
        //再次获取数据,为下次创节点准备
        GetData(&t);
    }
    
    return phead;    //返回链表的头头节点的地址
    
    
}



void PrintList(List li)
{    //打印链表的所有节点,
    Node*p = li->next;
    
    while(p!=NULL)
    {
        PrintData(p->data);
        
        p=p->next;
        
    }
    
    
    
    
}


void DestroyList(List li)
{  
    //销毁链表
    Node*p=li;
    Node*t;
    while(p!=NULL)
    {
        t=p;
        free(t);
        p=p->next;
    }
    
    
    
}