求C语言单链表创建及访问程序代码

求C语言单链表创建及访问程序代码,第1张

下面是我写的代码:

头文件(SLNode.h)

#include<stdlib.h>

typedef int DataType

typedef struct node

{

DataType data

struct node *next

}SLNode

void SLNode_Initiate(SLNode **head)

{

if((*head=(SLNode *)malloc(sizeof(SLNode)))==NULL)

exit(1)

(*head)->next=NULL

}

int SLNode_Length(SLNode *head)

{

int i=0

SLNode *p

p=head->next

while(p!=NULL)

{

i++

p=p->next

}

return i

}

int SLNode_Insert(SLNode *head,int pos,DataType add)

{

SLNode *p,*s

int i

if(pos>SLNode_Length(head)||pos<0)

{

printf("the pos in not exsit")

return 0

}

p=head

for(i=0i<posi++)

p=p->next

if((s=(SLNode *)malloc(sizeof(SLNode)))==NULL)

return 0

s->data=add

s->next=p->next

p->next=s

return 1

}

int SLNode_Delete_Pos(SLNode *head,int del)

{

SLNode *p,*s

int i

if(del>=SLNode_Length(head) || del<0)

{

printf("the pos not esxit\n")

return 0

}

p=head

for(i=0i<deli++)

p=p->next

s=p->next

p->next=s->next

free(s)

return 1

}

int SLNode_Delete_Data(SLNode *head,DataType del)

{

SLNode *p,*s

int length=SLNode_Length(head)

p=head

while(p->next!=NULL)

{

s=p->next

if(s->data==del)

{

p->next=s->next

free(s)

}

p=p->next

}

if(length==SLNode_Length(head))

{

printf("can't find the number\n")

return 0

}

return 1

}

int SLNode_Get(SLNode *head,int pos,DataType *get)

{

SLNode *p=head

int i

if(pos>=SLNode_Length(head) || pos<0)

{

printf("thd pos is not exsit\n")

return 0

}

for(i=0i<=posi++)

p=p->next

*get=p->data

return 1

}

void SLNode_Destroy(SLNode **head)

{

SLNode *p,*s

p=*head

while(p!=NULL)

{

s=p

p=p->next

free(s)

}

*head=NULL

}

void SLNode_Print(SLNode *head)

{

SLNode *p

p=head->next

while(p!=NULL)

{

printf("%d",p->data)

p=p->next

}

}

CPP文件:

#include<stdio.h>

#include"SLNode.h"

void Menu()

{

puts("*************************************")

puts("A: Add node\n")

puts("B: Delete node by pos\n")

puts("C: Delete node by data\n")

puts("D: Get node by pos\n")

puts("E: Print node\n")

puts("X: Exit the programe")

puts("*************************************")

}

int main()

{

char select

int insert

int get

int del

int pos

SLNode *node

SLNode_Initiate(&node)

Menu()

while(scanf("%c",&select))

{

fflush(stdin)

switch(select)

{

case 'A':

printf("please enter a number which is insert and it's pos:")

scanf("%d%d",&insert,&pos)

SLNode_Insert(node,pos,insert)

break

case 'B':

printf("please enter the pos of the number which you want to delete:")

scanf("%d",&pos)

SLNode_Delete_Pos(node,pos)

break

case 'C':

printf("please enter the number which you want to delete:")

scanf("%d",&del)

SLNode_Delete_Data(node,del)

break

case 'D':

printf("please enter the pos of the number which you want to get:")

scanf("%d",&pos)

SLNode_Get(node,pos,&get)

break

case 'E':

SLNode_Print(node)

break

case 'X':

SLNode_Destroy(&node)

return 0

default:

printf("please select a correct select\n")

}

printf("\n\n")

fflush(stdin)

system("pause")

system("cls")

Menu()

}

return 0

}

// typedef int* MM当你定义MM a,b的时候a,b都是int *类型的了,和define是类似的,还是有点区别的

// define MM int* 则当你定义MM a,b的时候,宏替换后,成为int *a,b所以a是int*,而b则仍然是int

// 在这点上这两个用法和功能是非常类似的.

//则typedef struct node *link就好理解了, 相当于为struct node*换个别名link

// link next就是struct node* next

typedef struct node{

ListItem element

link next

}Node

这个也是一样的,通过typedef将struct node换个别名Node,而node知道什么意思吧,就是这个结构体的标签而已

说了这么多 应该懂了吧 按理说书上有typedef的

last question:

ListInsert(int k,int x,List L),数据结构上的吧 作者写的是伪码(让你更好理解的不标准代码)


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9905502.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-03
下一篇2023-05-03

发表评论

登录后才能评论

评论列表(0条)

    保存