社区
C语言
帖子详情
请问如何用单向链表存5个数据
水中飞月
优质创作者: Java技术领域
领域专家: 后端开发技术领域
2003-09-26 02:35:14
我有一个单向链表如下
typedef struct elemenT{
int data;
struct elemenT *next;
}element;
我想用此链表存
1,2,3,4,5
这样五个数据,该如何写代码,请高手指教!!
...全文
179
4
打赏
收藏
请问如何用单向链表存5个数据
我有一个单向链表如下 typedef struct elemenT{ int data; struct elemenT *next; }element; 我想用此链表存 1,2,3,4,5 这样五个数据,该如何写代码,请高手指教!!
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
4 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
CsdnPlayer
2003-09-26
打赏
举报
回复
mark
Andy84920
2003-09-26
打赏
举报
回复
/*线性表的插入与删除,排序操作. */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h> /*为toupper();提供原型*/
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType;
typedef struct
{
ElemType *elem; /*存储空间基址*/
int length; /*当前长度*/
int listsize; /*当前分配的存储容量*/
}List;
void Show_Choose_Menu ( void); /*显示菜单*/
void Display_List ( List L); /*显示线性表内容*/
List Construct_List ( List L); /*初始化*/
List Insert_List ( List L); /*线性表插入操作*/
List Delete_List ( List L); /*线性表删除操作*/
void Sort_List ( List L); /*线性表排序操作*/
List Sort_Method_A(List L); /*升序排列*/
List Sort_Method_D(List L); /*降序排列*/
List To_Empty_List ( List L); /*清空线性表*/
void swap(ElemType *a,ElemType *b);
int main (void)
{
char ch;
List L;
Show_Choose_Menu();
printf("Please Input The First Letter Of The Word: ");
scanf(" %c",&ch);
while ( (ch = toupper(ch)) != 'Q')
{
switch (ch)
{
case 'C': L= Construct_List(L); break;
case 'I': L= Insert_List(L); break;
case 'D': L= Delete_List(L); break;
case 'S': Sort_List(L); break;
case 'T': L= To_Empty_List (L); break;
default : printf("\nYour Input Has Error!Again!\n"); break;
}
Show_Choose_Menu();
printf("Please Choose The Operation By The First Letter: ");
scanf(" %c",&ch);
}
printf(" Thank You For Using My List Program!\n");
printf(" BYE!\n");
return 0;
}
void Show_Choose_Menu(void)
{
printf(" \n Construt_List To_Empty_List Quit\n");
printf(" Insert_List Delete_List Sort_List\n");
}
List Construct_List ( List L)
{
L.elem=(ElemType *) malloc (LIST_INIT_SIZE * sizeof(ElemType));
if (!L.elem) exit(1);
L.length = 0;
L.listsize = LIST_INIT_SIZE;
Display_List(L);
return L;
}
void Display_List(List L)
{
int i;
if(L.length == 0) printf("Now. No Value Exist In The List.Please Insert!");
else {
printf("Now After Your Operation.The List Is :\n ");
for(i=0;i<L.length;i++)
printf("%d ",*(L.elem+i));
}
}
List Insert_List(List L) /*可以实现连续插入,但我没有这样做*/
{
int i;
ElemType value;
ElemType *newbase,*q,*p;
printf("Please Input The Insert Number And Value: ");
while(2 != scanf("%d%d",&i,&value) || i<1 || i>L.length+1)
{
printf("Input Error! Input Again Please: ");
continue;
}
if(L.length>=L.listsize){
newbase=(ElemType *) realloc(L.elem,
(L.listsize+LISTINCREMENT) * sizeof(ElemType));
if (!newbase) exit(1);
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
q=&(L.elem[i-1]);
for (p=&(L.elem[L.length-1]);p>=q; --p)
*(p+1) = *p;
*q = value;
++L.length;
printf("The Value %d Had Insert At %d \n",value,i);
Display_List(L);
return L;
}
List Delete_List(List L)
{
int i;
ElemType value;
ElemType *p,*q;
printf("Please Input You Want Delete's Value's Number: ");
while(1 != scanf("%d",&i) ||(i<1) || (i>L.length))
{
printf("Your Input Error. Input Again: ");
continue;
}
p = &(L.elem[i-1]);
value = *p;
q = L.elem+L.length-1;
for(++p;p<=q;++p)
*(p-1) = *p;
--L.length;
printf("The Value %d Had Delete At %d \n",value,i);
Display_List(L);
return L;
}
void Sort_List(List L) /*注意,这里要复制一个表结构,否则会有意想不到的结果*/
{
char c;
int i;
List Lc;
Lc.elem = (ElemType *) malloc (L.length * sizeof(ElemType));
Lc.length = Lc.listsize = L.length;
for (i=0; i<Lc.length; i++)
*(Lc.elem+i) = *(L.elem+i);
printf("Please Choose Sort Method, A or D: ");
scanf(" %c",&c);
if(c == 'a') Lc= Sort_Method_A(Lc);
else Lc=Sort_Method_D(Lc);
Display_List(Lc);
}
List Sort_Method_A(List L)
{
int i,j;
for (i=0;i<L.length;i++)
for(j=0;j<i;j++)
if(*(L.elem+i) > *(L.elem+j))
swap(L.elem+i,L.elem+j);
return L;
}
List Sort_Method_D(List L)
{
int i,j;
for (i=0;i<L.length;i++)
for(j=0;j<i;j++)
if(*(L.elem+i) < *(L.elem+j))
swap(L.elem+i,L.elem+j);
return L;
}
List To_Empty_List (List L)
{
free (L.elem);
L.length=0;
Display_List (L);
return L;
}
void swap(ElemType *a,ElemType *b)
{
ElemType temp;
temp = *b;
*b = *a;
*a = temp;
}
Andy84920
2003-09-26
打赏
举报
回复
我有一个单向链表如下
typedef struct elemenT{
int data;
struct elemenT *next;
}element;
这应该是你的链表的节点结构定义吧?
单链表定义如下:
typedef struct
{
elemenT *first; //指向链表的第一个结点
elemenT *last; //指向链表的最后一个结点
int length; //链表的长度
} List;
doer_ljy
2003-09-26
打赏
举报
回复
#include "stdio.h"
#include "stdlib.h"
typedef struct elemenT{
int data;
struct elemenT *next;
}element;
void main()
{
elemenT *root,*pt,*ppt;
int i,j;
root = (element *)malloc(sizeof(element));
root->data = 1;
root->next = NULL;
pt = root;
for(i=1;i<5;i++)
{
ppt = (element *)malloc(sizeof(element));
pt->next = ppt;
ppt->data = i+1;
ppt->next = NULL;
pt = ppt;
}
pt = root;
for(;pt != NULL;pt = pt->next ) printf("%d",pt->data );
}
Python实现-无头单向非循环链表
无头单向非循环链表结构简单,一般不会单独用来
存
数据
。实际中更多是作为其他
数据
结构的子结构,如哈希桶、图的邻接表等等 无头单向非循环链表 对于任意一个
数据
元素a(i)来说,储
存
本身的
数据
.(这个域叫
数据
域) ...
Lesson3--顺序表_链表.pdf
静态顺序表只适用于确定知道需要
存
多少
数据
的场景。静态顺序表的定长数组导致N定大了,空间开多了浪费,开少了不够用。所以现实中基本都是使用动态顺序表,根据需要动态的分配空间大小。动态顺序表的实现主要包括...
请问
什么是
单向链表
,如何判断两个
单向链表
是否相交
单向链表
(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始; 链表是使用指针进行构造的列表;又称为结点列表,因为链表是由一个个结点组装起来的;其中每个结点都有...
java
单向链表
双向链表_
单向链表
和双向链表的区别的意义 - Java篇
101)
单向链表
只有后一节点指针,在节点删除,移动的时候,需要暂
存
前一节点,删除的时候将前一节点和后一节点连接,因为比双向链表少维护一个前节点,只在删除的时候暂
存
,所以比
单向链表
节省资源,但是增加...
数据
结构——
单向链表
和双向链表的实现(C语言版)
这篇文章关于链表的介绍,还有
单向链表
和双向链表的C语言实现,内容干货满满,建议边看边上手敲代码!通过这篇文章,相信你已经对链表这个
数据
结构有了一定的了解,可以开始刷一些链表的OJ题目。如果只是看了一遍,...
C语言
70,040
社区成员
243,246
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章