社区
C语言
帖子详情
请问如何用单向链表存5个数据
水中飞月
优质创作者: Java技术领域
领域专家: 后端开发技术领域
2003-09-26 02:35:14
我有一个单向链表如下
typedef struct elemenT{
int data;
struct elemenT *next;
}element;
我想用此链表存
1,2,3,4,5
这样五个数据,该如何写代码,请高手指教!!
...全文
200
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 );
}
C语言实现
单向链表
本章使用C语言实现了
数据
结构中的
单向链表
,难度也不高,但值得作为学习
数据
结构的人去亲自实现一下。
【
数据
结构】动图详解
单向链表
顺序表中间及头部的插入与删除,需要对原有
数据
进行移动,时间复杂度为O(N),成本较高使用realloc进行增容时需要申请新空间,释放旧空间,拷贝
数据
,消耗较高。由于我们无法知道用户实际需要多少空间,在增容时往往可能会有大量的空间剩余。那么这些问题要如何解决呢?通过链表。 链表是一种物理
存
储结构上非连续、非顺序的
存
储结构,
数据
元素的逻辑顺序是通过链表中的指针链接次序实现的。每当我们需要新增
数据
时,我们只需申请一个新结点用来保
存
数据
,然后用指针将链表与新结点链接起来即可,并不需要进行
数据
拷贝。
【
数据
结构】之
单向链表
、双端(单向)链表、循环(单向)链表
单向链表
、双端(单向)链表、循环(单向)链表示意(简)图:
单向链表
:只有一个指针指向头结点。 双端(单向)链表:有一个指针指向头结点,还有一个指针指向尾节点。 循环(单向)链表:指向形成一个闭环。 双端(单向)链表的Java实现(简单示例): 注:
单向链表
、双端(单向)链表、循环(单向)链表的实现思路几乎一样,这里以实现双端(单向)链表示例。 /** * 双端链表 简...
数据
结构——
单向链表
我自己的
单向链表
的相关笔记
C语言
70,038
社区成员
243,245
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章