数据结构 用c语言实现
1、编写程序initsqlist.c实现初始化顺序表L.2、编写程序creatsqlist.c依次向顺序表L中插入‘a’,‘b’,‘A’,‘c’,‘D’,‘d’,‘F’,...
1、 编写程序initsqlist.c实现初始化顺序表L.
2、 编写程序creatsqlist.c依次向顺序表L中插入‘a’,‘b’,‘A’,‘c’,‘D’,‘d’,‘F’,‘e’,‘h’元素。
3、 编写程序visitsqlist.c输出顺序表L。
4、 编写程序getlensqlist.c输出顺序表L的长度。
5、 编写程序insertsqlist.c实现在第3个位置上插入’f’元素。
6、 编写程序deletesqlist.c实现删除第1个位置上的元素。
7、 编写程序deletelem.c实现删除所有元素值在[A,Z]之间的所有元素。(算法时间效率要求尽量高) 展开
2、 编写程序creatsqlist.c依次向顺序表L中插入‘a’,‘b’,‘A’,‘c’,‘D’,‘d’,‘F’,‘e’,‘h’元素。
3、 编写程序visitsqlist.c输出顺序表L。
4、 编写程序getlensqlist.c输出顺序表L的长度。
5、 编写程序insertsqlist.c实现在第3个位置上插入’f’元素。
6、 编写程序deletesqlist.c实现删除第1个位置上的元素。
7、 编写程序deletelem.c实现删除所有元素值在[A,Z]之间的所有元素。(算法时间效率要求尽量高) 展开
1个回答
展开全部
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct list
{
char ch;
struct list *next;
}List;
List *create_list(List *data);
List *insert_list(List *head, List *data);
void print_list(List *head);
void get_list_length(List *head);
void insert_third_withf(List *head);
void delete_first_value(List *head);
void delete_within_A_Z(List *head);
int main()
{
List *head = NULL;
List tmp;
memset(&tmp, 0x00, sizeof(tmp));
head = create_list(&tmp);
tmp.ch = 'a';
insert_list(head, &tmp);
tmp.ch = 'b';
insert_list(head, &tmp);
tmp.ch = 'A';
insert_list(head, &tmp);
tmp.ch = 'c';
insert_list(head, &tmp);
tmp.ch = 'D';
insert_list(head, &tmp);
tmp.ch = 'd';
insert_list(head, &tmp);
tmp.ch = 'F';
insert_list(head, &tmp);
tmp.ch = 'e';
insert_list(head, &tmp);
tmp.ch = 'h';
insert_list(head, &tmp);
printf("显示\n");
print_list(head);
get_list_length(head);
printf("第三个位置插入f\n");
insert_third_withf(head);
print_list(head);
printf("删除第一个元素\n");
delete_first_value(head);
print_list(head);
printf("删除A-Z\n");
delete_within_A_Z(head);
print_list(head);
return 0;
}
List *create_list(List *data)
{
List *newnode = NULL;
newnode = (List*)malloc(sizeof(List));
if(NULL == newnode)
{
printf("malloc failed !\n");
return NULL;
}
*newnode = *data;
newnode->next = NULL;
return newnode;
}
List *insert_list(List *head, List *data)
{
List *newnode = NULL;
if(NULL == head)
{
printf("list null !\n");
return NULL;
}
newnode = create_list(data);
while(head->next != NULL)
{
head = head->next;
}
head->next = newnode;
newnode->next = NULL;
return newnode;
}
void print_list(List *head)
{
List *tmp = NULL;
if(NULL == head)
{
printf("print head null !\n");
return;
}
tmp = head;
tmp = tmp->next;
while(tmp != NULL)
{
printf("ch = %c\n", tmp->ch);
tmp = tmp->next;
}
}
void get_list_length(List *head)
{
List *tmp = NULL;
int length = 0;
if(NULL == head)
{
printf("get_len_list head null !\n");
return ;
}
tmp = head;
while(tmp != NULL)
{
length = length + 1;
tmp = tmp->next;
}
printf("the list length(包括头结点) = %d\n", length);
}
void insert_third_withf(List *head)
{
List *tmp = NULL;
List *value = NULL;
List val;
int i = 1;
if(NULL == head || NULL == head->next)
{
printf("insert_third_...head null !\n");
return;
}
memset(&val, 0x00, sizeof(val));
head = head->next;
tmp = head;
while(tmp != NULL)
{
i = i + 1;
if(3 == i)
{
val.ch = 'f';
value = create_list(&val);
value->next = tmp->next;
tmp->next = value;
break;
}
tmp = tmp->next;
}
if(i < 3)
{
printf("the list is too short !\n");
}
}
void delete_first_value(List *head)
{
List *tmp = NULL;
if(NULL == head || NULL == head->next || NULL == head->next->next)
{
printf("delete_fir_val head null !\n");
return;
}
tmp = head->next;
head->next = head->next->next;
free(tmp);
}
void delete_within_A_Z(List *head)
{
List *tmp = NULL;
if(NULL == head || head->next == NULL)
{
printf("head null[dele_A_Z] !\n");
return;
}
tmp = head;
tmp->next = head->next;
while(tmp->next != NULL)
{
if(tmp->next->ch >64 && tmp->next->ch < 91)
{
if(NULL != tmp->next->next)
{
free(tmp->next);
tmp->next = tmp->next->next;
}
else
{
tmp->next = NULL;
break;
}
}
tmp = tmp->next;
}
}
//楼主只能帮你到这了
#include<string.h>
#include<stdlib.h>
typedef struct list
{
char ch;
struct list *next;
}List;
List *create_list(List *data);
List *insert_list(List *head, List *data);
void print_list(List *head);
void get_list_length(List *head);
void insert_third_withf(List *head);
void delete_first_value(List *head);
void delete_within_A_Z(List *head);
int main()
{
List *head = NULL;
List tmp;
memset(&tmp, 0x00, sizeof(tmp));
head = create_list(&tmp);
tmp.ch = 'a';
insert_list(head, &tmp);
tmp.ch = 'b';
insert_list(head, &tmp);
tmp.ch = 'A';
insert_list(head, &tmp);
tmp.ch = 'c';
insert_list(head, &tmp);
tmp.ch = 'D';
insert_list(head, &tmp);
tmp.ch = 'd';
insert_list(head, &tmp);
tmp.ch = 'F';
insert_list(head, &tmp);
tmp.ch = 'e';
insert_list(head, &tmp);
tmp.ch = 'h';
insert_list(head, &tmp);
printf("显示\n");
print_list(head);
get_list_length(head);
printf("第三个位置插入f\n");
insert_third_withf(head);
print_list(head);
printf("删除第一个元素\n");
delete_first_value(head);
print_list(head);
printf("删除A-Z\n");
delete_within_A_Z(head);
print_list(head);
return 0;
}
List *create_list(List *data)
{
List *newnode = NULL;
newnode = (List*)malloc(sizeof(List));
if(NULL == newnode)
{
printf("malloc failed !\n");
return NULL;
}
*newnode = *data;
newnode->next = NULL;
return newnode;
}
List *insert_list(List *head, List *data)
{
List *newnode = NULL;
if(NULL == head)
{
printf("list null !\n");
return NULL;
}
newnode = create_list(data);
while(head->next != NULL)
{
head = head->next;
}
head->next = newnode;
newnode->next = NULL;
return newnode;
}
void print_list(List *head)
{
List *tmp = NULL;
if(NULL == head)
{
printf("print head null !\n");
return;
}
tmp = head;
tmp = tmp->next;
while(tmp != NULL)
{
printf("ch = %c\n", tmp->ch);
tmp = tmp->next;
}
}
void get_list_length(List *head)
{
List *tmp = NULL;
int length = 0;
if(NULL == head)
{
printf("get_len_list head null !\n");
return ;
}
tmp = head;
while(tmp != NULL)
{
length = length + 1;
tmp = tmp->next;
}
printf("the list length(包括头结点) = %d\n", length);
}
void insert_third_withf(List *head)
{
List *tmp = NULL;
List *value = NULL;
List val;
int i = 1;
if(NULL == head || NULL == head->next)
{
printf("insert_third_...head null !\n");
return;
}
memset(&val, 0x00, sizeof(val));
head = head->next;
tmp = head;
while(tmp != NULL)
{
i = i + 1;
if(3 == i)
{
val.ch = 'f';
value = create_list(&val);
value->next = tmp->next;
tmp->next = value;
break;
}
tmp = tmp->next;
}
if(i < 3)
{
printf("the list is too short !\n");
}
}
void delete_first_value(List *head)
{
List *tmp = NULL;
if(NULL == head || NULL == head->next || NULL == head->next->next)
{
printf("delete_fir_val head null !\n");
return;
}
tmp = head->next;
head->next = head->next->next;
free(tmp);
}
void delete_within_A_Z(List *head)
{
List *tmp = NULL;
if(NULL == head || head->next == NULL)
{
printf("head null[dele_A_Z] !\n");
return;
}
tmp = head;
tmp->next = head->next;
while(tmp->next != NULL)
{
if(tmp->next->ch >64 && tmp->next->ch < 91)
{
if(NULL != tmp->next->next)
{
free(tmp->next);
tmp->next = tmp->next->next;
}
else
{
tmp->next = NULL;
break;
}
}
tmp = tmp->next;
}
}
//楼主只能帮你到这了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询