把我的 C 作业贴出来 实验一 线性表

把我的作业贴出来
实验一 线性表
一、实验目的
1.熟悉线性表的顺序和链式存储结构
2.掌握线性表的基本运算
3.能够利用线性表的基本运算完成线性表应用的运算

二、实验目的
1.设有一个线性表E={e1, e2, … , en-1, en},设计一个算法,将线性表逆置,即使元素排列次序颠倒过来,成为逆线性表E'={ en , en-1 , … , e2 , e1 },要求逆线性表占用原线性表空间,并且用顺序表和单链表两种方法表示,分别用两个程序来完成。(文件夹:习题9_3)

2.已知由不具有头结点的单链表表示的线性表中,含有三类字符的数据元素(字母、数字和其他字符),试编写算法构造三个以循环链表表示的线性表,使每个表中只含有同一类的字符,且利用原表中的结点空间,头结点可另辟空间。(文件夹:习题9_12)



第一题
/*顺序表的结构类型定义.h*/
typedef char datatype;
/*const int maxsize=1024;*/
#define maxsize 1024
typedef struct
{ datatype data[maxsize];
int last;
}sequenlist;

#include <stdio.h>
#include <stdlib.h>
#include <stdafx.h>
#include"顺序表结构类型定义.h"

int main()
{
sequenlist*L;
L=creat(L);
puts("原字符串:/n");
print(L);
invert(L);/*调用顺序表逆值的函数*/
puts("逆置后的字符串:/n");
print(L);

system("PAUSE");
}
/*输出顺序表.H*/
#include <stdio.h>
#include <stdlib.h>
#include <stdafx.h>
#include"顺序表结构类型定义.h"

void print(sequenlist*L)
{
int i;
for(i=0;i<L->last;i++)
/*cout<<L->data[i]<<" ";
cout<<endl;*/
printf("%c ",L->data[i]);
puts("/n");
return;
}

/*建立顺序表.H*/
#include"顺序表结构类型定义.h"
#include <conio.h>
#include <stdafx.h>
sequenlist* creat(sequenlist*L)
{
char ch;
L=(sequenlist*)malloc(sizeof(sequenlist));
L->last=0;
puts("请输入一字符串(输入“*”结束):/n");
while((ch=getchar())!='*')
{
L->data[L->last]=ch;
L->last++;
}
return L;
}
/*该程序不能忽略回车符,在 * 前有回车的话,也会在相应位置处输出一个回车*/



//第二题
/*单链表结构类型定义.h*/
typedef char datatype;
typedef struct node
{
datatype data;
struct node *next;
}linklist;
/*单链表逆置主文件.cpp*/
#include <stdafx.h>
#include <stdio.h>
#include <stdlib.h>
#include"单链表结构类型定义.h"
int main()
{
linklist*head;
head=creat(head);/*原来用C++写的函数是采用引用的方式来传递值的,C不支持,增加了返回值*/

puts("您输入的字符串是:");
print(head);

invert(head);/*调用单链表逆置的函数*/

puts("逆置后的字符串是:");
print(head);

getch();
return 1;
}
/*建立单链表.h*/
#include <stdio.h>
#include <stdlib.h>
#include <stdafx.h>
#include"单链表结构类型定义.h"
linklist* creat(linklist*head)
/*采用尾插法建立具有头结点的单链表*/
{
char ch;
linklist *s,*r;

head=(linklist*)malloc(sizeof(linklist));
r=head;

puts("请输入字符串(输入“*”结束):");

while((ch=getchar())!='*')
{
s=(linklist*)malloc(sizeof(linklist));
s->data=ch;
r->next=s;
r=s;
}
r->next=NULL;
return head;
}
/*输出单链表.h*/
#include <stdio.h>
#include <stdlib.h>
#include <stdafx.h>
#include"单链表结构类型定义.h"
void print(linklist *head)
{
linklist*p=head->next;

while(p!=NULL)
{
/*cout<<p->data<<" ";*/
printf("%c ",p->data);
p=p->next;
}

/*cout<<endl;*/
puts("/n");
}
/*单链表逆置*/
#include <stdafx.h>
#include <stdio.h>
#include <stdlib.h>
#include"单链表结构类型定义.h"
void invert(linklist*head)
{
linklist*p,*q;

p=head->next;
head->next=NULL;

while(p)
{
q=p;
p=p->next;
q->next=head->next;
head->next=q;
}
return;
}
/*
void invert(linklist*head)
{
linklist*p,*q,*r;
p=head->next;
q=p->next;
while(q!=NULL)
{
r=q->next;
q->next=p;
p=q;
q=r;
}
head->next->next=NULL;
head->next=p;
}*/
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值