读书人

求简单C程序,速给分!解决思路

发布时间: 2012-02-25 10:01:47 作者: rapoo

求简单C程序,速给分!!
编写一个无表头结点的单链表函数,单链表结构为:
struct link
{int data;
strut link *next;
}
输入数据以0为结束条件


[解决办法]
#include <iostream>
using namespace std;

//结构的定义部分
typedef int ElemType;
typedef struct LNode{
ElemType data;
LNode *next;
} *LinkList;

//结构的生成操作
void InitList(LinkList &L)
{
L=NULL;
}

//结构的清理操作
void ClearList(LinkList &L)
{
while(L)
{
LNode *P=L;
L=L-> next;
delete P;
}
}

//结构的销毁操作
void DestroyList(LinkList &L)
{
ClearList(L);
}

//结构的状态查看
bool ListEmpty(LinkList L)
{
return L==NULL;
}

int ListLength(LinkList L)
{
LNode *P=L;
int len=0;
while(P)
{
len++;
P=P-> next;
}
return len;
}

void ListOutput(LinkList L)
{
cout < < "( ";
LNode *P=L;
while(P&&P-> next)
{
cout < <P-> data < < ", ";
P=P-> next;
}
if(P) cout < <P-> data;
cout < < ") " < <endl;
}

void ListInfo(LinkList L)
{
cout < < " ";
ListOutput(L);
if(ListEmpty(L)) cout < < " Empty:Yes " < <endl;
else cout < < " Empty:No " < <endl;
cout < < " Length= " < <ListLength(L) < <endl;
}

//结构的查改增删
bool GetElem(LinkList L,int pos,ElemType &e)
{
if(pos <1) return false;
LNode *P=L;
int i=1;
while(P&&i <pos)
{
i++;
P=P-> next;
}
if(P)
{
e=P-> data;
return true;
}
return false;
}

bool ElemEqual(ElemType e1,ElemType e2)
{
return e1==e2;
}

bool ElemPlus(ElemType e1,ElemType e2)
{
return e1> e2;
}

bool ElemMinus(ElemType e1,ElemType e2)
{
return e1 <e2;
}

LNode *LocateElem(LinkList L,ElemType e,bool (*compare)(ElemType,ElemType))
{
LNode *P=L;
while(P&&!(*compare)(P-> data,e)) P=P-> next;
return P;
}

bool PutElem(LinkList &L,int pos,ElemType e)
{
if(pos <1) return false;
LNode *P=L;
int i=1;
while(P&&i <pos)
{
i++;
P=P-> next;
}
if(P)
{
P-> data=e;
return true;
}
return false;
}

bool ListInsert(LinkList &L,int pos,ElemType e)
{
if(pos <0) return false;
LNode *P=L;
int i=1;
while(P&&i <pos)
{
i++;
P=P-> next;
}
if(pos> 0&&!P) return false;
LNode *Q=new LNode;
Q-> data=e;
if(pos){
Q-> next=P-> next;
P-> next=Q;
}
else{
Q-> next=L;
L=Q;
}
return true;
}

bool ListDelete(LinkList &L,int pos,ElemType &e)
{
if(!L||pos <1) return false;
LNode *P=L;
if(pos==1) L=L-> next;
else{
LNode *Q=L;
int i=1;
while(Q&&i <pos-1)


{
i++;
Q=Q-> next;
}
if(!Q||!Q-> next) return false;
P=Q-> next;
Q-> next=P-> next;
}
e=P-> data;
delete P;
return true;
}

//结构的功能测试
int main()
{
cout < < "无头结点的单链表结构的测试: " < <endl;

LinkList A;

cout < < "*************************************** " < <endl;
cout < < "生成操作:InitList(A) " < <endl;
InitList(A);
ListInfo(A);

cout < < "*************************************** " < <endl;
cout < < "插入操作:ListInsert(A,0,2) " < <endl;
ListInsert(A,0,2);
ListInfo(A);

cout < < "*************************************** " < <endl;
cout < < "插入操作:ListInsert(A,1,7) " < <endl;
ListInsert(A,1,7);
ListInfo(A);

cout < < "*************************************** " < <endl;
cout < < "插入操作:ListInsert(A,2,8) " < <endl;
ListInsert(A,2,8);
ListInfo(A);

cout < < "*************************************** " < <endl;
cout < < "插入操作:ListInsert(A,2,21) " < <endl;
ListInsert(A,2,21);
ListInfo(A);

cout < < "*************************************** " < <endl;
cout < < "插入操作:ListInsert(A,4,15) " < <endl;
ListInsert(A,4,15);
ListInfo(A);

cout < < "*************************************** " < <endl;
cout < < "删除操作:ListDelete(A,5,e) " < <endl;
ElemType e;
ListDelete(A,5,e);
ListInfo(A);
cout < < " e= " < <e < <endl;

cout < < "*************************************** " < <endl;
cout < < "更新操作:PutElem(A,4,39) " < <endl;
PutElem(A,4,39);
ListInfo(A);

cout < < "*************************************** " < <endl;
cout < < "查找操作:GetElem(A,2,e) " < <endl;
GetElem(A,2,e);
ListInfo(A);
cout < < " e= " < <e < <endl;

cout < < "*************************************** " < <endl;
cout < < "定位操作:LocateElem(A,15,ElemEqual) " < <endl;
ListInfo(A);
cout < < " Address= " < <LocateElem(A,15,ElemEqual) < <endl;

cout < < "*************************************** " < <endl;
cout < < "定位操作:LocateElem(A,6,ElemPlus) " < <endl;
ListInfo(A);
LNode *R=LocateElem(A,6,ElemPlus);
cout < < " Address= " < <R < <endl;
if(R) cout < < " " < <R-> data < <endl;

cout < < "*************************************** " < <endl;
cout < < "定位操作:LocateElem(A,8,ElemMinus) " < <endl;
ListInfo(A);
cout < < " Address= " < <LocateElem(A,8,ElemMinus) < <endl;

cout < < "*************************************** " < <endl;
cout < < "清空操作:ClearList(A) " < <endl;
ClearList(A);
ListInfo(A);

cout < < "*************************************** " < <endl;
cout < < "销毁操作:DestroyList(A) " < <endl;
DestroyList(A);
ListInfo(A);

cout < < "按任意键,结束... ";
cin.get();



return 0;
}
[解决办法]
为什么要一个不带表头 的呢? 操作比较麻烦的
[解决办法]
#include <stdio.h>
#include <stdlib.h>

#define LEN sizeof(struct link)
struct link
{
int data;
struct link *next;
};

struct link *create()
{
int num;

struct link *head = NULL;
struct link *p;
scanf( "%d ", &num);
while(num != 0)
{
struct link *node = (struct link *)malloc(LEN);
node-> next = NULL;
node-> data = num;
if (head == NULL)
{
head = node;
p = head;
}
else
p-> next = node;
p = node;
scanf( "%d ", &num);
}
return(head);
}
void print(struct link *head)
{
struct link *p = head;

while(p != NULL)
{
printf( "%-3d ",p-> data);
p = p-> next;
}
}
int main()
{
struct link *head1;

printf( "input a link(with 0 end):\n ");
head1=create();

printf( "output link is:\n ");
print(head1);

printf( "\n ");

return 0;
}

LZ快点结帖。。。。^_^

[解决办法]
根据你的要求,写的一个简单的例子,
包括建立和输出:


#include <stdio.h>
#include <stdlib.h>

typedef struct link
{
int data;
struct link *next;
}node;

node *create(node **h)
{
int i;
*h=(node *)malloc(sizeof(node));
node *tmp=*h, *t;
printf( "Please input data, (0 = exit): ");
scanf( "%d ", &i);
while(i != 0)
{
tmp-> data = i;
t = tmp;
tmp = (node *)malloc(sizeof(node));
t-> next=tmp;
printf( "Please input data, (0 = exit): ");
scanf( "%d ", &i);
}
t-> next=NULL;
free(tmp);
}

void print(node *h)
{
while(h!=NULL)
{
printf( "%d-> ", h-> data);
h = h-> next;
}
printf( "NULL\n ");
}

int main(int argc, char* argv[])
{
node *head=NULL;
create(&head);
print(head);

system( "pause ");
return 0;
}

读书人网 >C语言

热点推荐