33,010
社区成员
发帖
与我相关
我的任务
分享
// LinkList.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "Linklsit.h"
#include "malloc.h"
#define FLAG -1
struct Node{
ElementType Element;
Position next;
};
int IsEmpty(List l)
{
return (l->next==NULL);
}
void Delete(List L)
{
Position p,Tmp;
p=L->next;
L->next=NULL;
while (p!=NULL)//下面为释放每个结点
{
Tmp=p->next;
free(p);//但p没有看到在哪创建了,运行出错 p=Tmp;
}
}
List MakeEmpty(List l)
{
#if 0
if (!IsEmpty(l))
{
l->next=NULL;//这样只是将链表头指向为空,但没有释放其他剩余结点
}
else
{
printf("The List is empty!");
}
return l;
#else
if (l)
{
Delete(l);
}
l=(List)malloc(sizeof(struct Node));
if (!l)
{
printf("Out of Memory!");
}
l->next=NULL;
return l;
#endif
}
List CreateList(void)
{
int i;
List l;//用于返回创建的链表,不能被赋值否则改变了链表
PtrToNode s;//用于申请一个结点空间
PtrToNode r;//用于将刚创建的每个结点连接起来
l=r=s=NULL;//初始为NUll,一定要初始化,否则下面访问会出错。注意r=s;
l=(List)malloc(sizeof(struct Node*));
if(!l)
printf("No Space!");
l->next=NULL;
scanf("%d",&i);
while(i!=FLAG)
{
s=(PtrToNode)malloc(sizeof(struct Node*));//需释放,在哪释放呢?
if (!s) {
printf("malloc is fail!");
}
s->Element=i;
if(NULL==l->next)
{
l->next=s;
}
else
{
r->next=s;
}
r=s;//无论l->next是否为NULL,都要执行这语句
scanf("%d",&i);
}
if (r)
{
r->next=NULL;//链表l表尾结点指向NULL
}
return l;
}
Position Header(List l)
{
return l;
}
void PrintList(List l)
{
#if 1
List TempL;
TempL=l->next;//采用临时变量,为了不改变链表,函数本身只是打印不涉及改变链表
if(IsEmpty(l))
printf("List is empty");
while(!IsEmpty(l)&&TempL)
{
printf("%d ",TempL->Element);
TempL=TempL->next;
}
printf("\n");
#else
List s;
s=l;
while(s->next!=NULL)
{
s=s->next;//此方法先改变next域在打印都用到临时变量
printf("%3d",s->Element);
}
printf("\n");
#endif
}
int main(int argc, char* argv[])
{
List l,l_copy;
l=CreateList();
PrintList(l);
l_copy=Header(l);
// l_copy=l;//直接赋值相当于Header
// MakeEmpty(l);
PrintList(l_copy);
return 0;
}
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
#ifndef __LINKLIST_H__
#define __LINKLIST_H__
typedef int ElementType;
//#define ElementType int
struct Node;
typedef struct Node* PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
List CreateList(void); //这个应该没有形参吧
int Empty(List l);
List MakeEmpty(List l);
int IsFull(List l);
int Insert(List l,ElementType x,Position p);
void Delete(List l);
Position Find(List l,ElementType x);
Position FindPrevious(List l,ElementType x);
Position Header(List l);
int IsLast(Position p,List L);
void Reverse(List H);
int Length(List L);
#endif
#define FLAG -1
struct Node{
ElementType Element;
Position next;
};
int IsEmpty(List l)
{
return (l->next==NULL);
}
void Delete(List L) //这个函数应该没错吧
{
Position p,Tmp;
p=L->next;
L->next=NULL;
while (p!=NULL)//下面为释放每个结点
{
Tmp=p->next;
free(p); //但p没有看到在哪创建了,运行出错[/color]
p=Tmp;
}
}
List MakeEmpty(List l)
{
#if 0
if (!IsEmpty(l))
{
l->next=NULL;//这样只是将链表头指向为空,但没有释放其他剩余结点
}
else
{
printf("The List is empty!");
}
return l;
#else
if (l)
{
Delete(l);
}
l=(List)malloc(sizeof(struct Node));
if (!l)
{
printf("Out of Memory!");
}
l->next=NULL;
return l;
#endif
}
List CreateList(void)
{
int i;
List l;//用于返回创建的链表,不能被赋值否则改变了链表
PtrToNode s;//用于申请一个结点空间
PtrToNode r;//用于将刚创建的每个结点连接起来
l=r=s=NULL;//初始为NUll,一定要初始化,否则下面访问会出错。注意r=s;
l=(List)malloc(sizeof(struct Node));//应该分配的是struct Node大小,而不是指针大小吧
if(!l)
printf("No Space!");
l->next=NULL;
scanf("%d",&i);
while(i!=FLAG)
{
s=(PtrToNode)malloc(sizeof(struct Node));//需释放,在哪释放呢?
if (!s) {
printf("malloc is fail!");
}
s->Element=i;
if(NULL==l->next)
{
l->next=s;
}
else
{
r->next=s;
}
r=s;//无论l->next是否为NULL,都要执行这语句
scanf("%d",&i);
}
if (r)
{
r->next=NULL;//链表l表尾结点指向NULL
}
return l;
}
Position Header(List l)
{
return l;
}
void PrintList(List l)
{
#if 1
List TempL;
TempL=l->next;//采用临时变量,为了不改变链表,函数本身只是打印不涉及改变链表
if(IsEmpty(l))
printf("List is empty");
while(!IsEmpty(l)&&TempL)
{
printf("%d ",TempL->Element);
TempL=TempL->next;
}
printf("\n");
#else
List s;
s=l;
while(s->next!=NULL)
{
s=s->next;//此方法先改变next域在打印都用到临时变量
printf("%3d",s->Element);
}
printf("\n");
#endif
}
int main(int argc, char* argv[])
{
List l,l_copy;
l = CreateList();
PrintList(l);
l_copy=Header(l);
// l_copy=l;//直接赋值相当于Header
// MakeEmpty(l);
PrintList(l_copy);
Delete(l);
return 0;
}
#ifndef __LINKLIST_H__
#define __LINKLIST_H__
typedef int ElementType;
//#define ElementType int
struct Node;
typedef struct Node* PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
List CreateList(List l);
int Empty(List l);
List MakeEmpty(List l);
int IsFull(List l);
int Insert(List l,ElementType x,Position p);
void Delete(List l);
Position Find(List l,ElementType x);
Position FindPrevious(List l,ElementType x);
Position Header(List l);
int IsLast(Position p,List L);
void Reverse(List H);
int Length(List L);
#endif