数据结构—链表应用能力测评
- 格式:docx
- 大小:81.43 KB
- 文档页数:3
任务:
编写一个能向表尾插入结点,并输出链表中所有数据元素的小程序
#ifndef _LINKLIST
#define _LINKLIST
#include
using namespace std ;
struct node
{
int data ;
struct node *next ;
};
typedef struct node *PLIST;
typedef struct node NODE;
/*创建链表,并初始化链表元素*/
PLIST createList_link()
{
PLIST head ,tail ,temp;
int elem = -1;
head = new NODE; //初始化头结点
if( head == NULL)
{
cout<<"分配空间失败,链表创建失败"< return NULL; } head->next = NULL; tail = head ; while(1) { cin >> elem ; if(elem == 0 ) break ; temp = new NODE ; if(temp == NULL) { cout<<"分配空间失败,链表创建失败"< break ; } temp->data = elem ; temp->next = NULL ; tail->next = temp ; tail = temp; } return head ; } void printList_link(PLIST head ) { /*在此处完成任务,输出head为表头的单链表数据元素*/ //begin PLIST p =new NODE; p=head->next; while(p){ printf("%d ",p->data); p=p->next; } //end } void insertDataTail(PLIST head , int insData ) { /*在此处完成任务,在head为表头的单链表表尾插入数据元素insData*/ //begin PLIST p; p=head->next; while(p->next!=NULL){ p=p->next; } PLIST q = new NODE; //初始化结点 p->next=q; q->data=insData; q->next=NULL; //end } #endif