求一份实现单链表的基本 *** 作的c语言程序,包括输入元素,谢谢。

求一份实现单链表的基本 *** 作的c语言程序,包括输入元素,谢谢。,第1张

#include<stdlibh>

#include "nodedefh"

#define CreateLinklistWay 1   // 0表示头插法创建链表,1表示尾插法创建链表

#if !CreateLinklistWay

/

 函数名称:linklist CreateLinklistHead()

 函数功能:利用头插法创建链表

 参    数:无

 返 回 值:创建完链表后的链表头结点

 说    明:无

/

extern linklist CreateLinklistHead()

{

int x, i, nodeNum;

linklist head, temp;  // 头结点与临时结点

head = (linklist )malloc(sizeof(linklist));  // 生成表头结点

head->next = NULL;   // 给表头结点的指针域赋值

printf("请输入链表中结点的个数:");

scanf("%d", &nodeNum);

for(i=1; i<=nodeNum; i++)

{

printf("请输入第 %d 个结点的数据:", i);

scanf("%d", &x);

temp = (linklist )malloc(sizeof(linklist));  // 生成新的结点

temp->data = x;    // 对新结点的数据域赋值

// 将新结点插到头结点之后

temp->next = head->next; 

head->next = temp;

}

return head;  // 返回新建链表的头结点

}

#else

/

 函数名称:linklist CreateLinklistRear()

 函数功能:利用尾插法创建链表

 参    数:无

 返 回 值:创建完链表后的链表头结点

 说    明:无

/

extern linklist CreateLinklistRear()

{

int x, i, nodeNum;

linklist head, rear, temp;  // 定义头结点、尾结点和临时结点

head = (linklist )malloc(sizeof(linklist));  // 生成表头结点,表头结点不存放数据

head->next = NULL;  // 将表头结点的指针域赋值为NULL

rear = head;  // 将表头结点赋值给表尾结点

printf("请输入链表中结点的个数:");

scanf("%d", &nodeNum);

for(i=1; i<=nodeNum; i++)

{

printf("请输入第 %d 个结点的数据:", i);

scanf("%d", &x);

temp = (linklist )malloc(sizeof(linklist));  // 生成新的结点

temp->data = x;   // 新增结点的数据域

temp->next = NULL; // 新增结点的指针域(由于是尾插法,所以插入的结点都在尾部,即指针域为NULL)

rear->next = temp; // 使前一个结点指向新增结点(head->next=temp)

rear = temp;  // 将新增结点赋值给尾结点(尾插法,插入的结点在尾部)(rear=head->next)

}

//rear->next = NULL;  // 将尾结点的指针域赋值为空(为了方便检验链表是否为空链表)

return head;  // 返回头结点

}

#endif

看起来比较乱,运行之后在看就很清楚了

#include <stdioh>

#include <stdlibh>

#include <iostream>

typedef int ElemType;

typedef struct LNode {

ElemType date;

struct LNode next;

}linklist,link;

/构造链表//////////////////////////////////////

void IinitList(link &L)

{

if(L)delete L;

L= (link)malloc(sizeof(LNode)) ;

if (!L) exit(1);

L->next=NULL;

cout<<"链表已经建立\n";

}

//////////////////////////////////////////////////////

// /删除结点/// //////////////////////////////////////////////

int listdelete(link &L,int i,ElemType &e)

{

link p,q; int j;

p=L;j=0;

while(p->next&&j<i-1)

{

p=p->next;++j;

}

q=p->next;

p->next=q->next;

e=q->date;free(q);

cout<<"链表已经删除\n";

return 1;

}

////////////////////////////////////////////// /////////

// /插入结点/////////////// ///////////////////////

int listinsert(link &L,int i,ElemType e)

{

link p,q;

int j;

p=L;j=0;

while(p&&j<i-1)

{

p=p->next;++j;

}

q= (link)malloc(sizeof(LNode));

q->date=e;

q->next=p->next;

p->next=q; cout<<"链表已经插入\n";

return 1;

}

/////////////////////////////////////////////////////

////显示数据///////// ////////////////////////////////

void show(link l)

{ link p; int j;

p=l;j=0;

cout<<"链表的值为:\n";

while(p->next)

{

cout<<p->next->date<<endl;

p=p->next;

}

}

//////////////////////// /////////////////////////////////

//////销毁链表////// ////////////////////////////////////////

void destorylinst(link &L)

{

while(L)

{ link p=L;

L=L->next;

free(p) ;

}

L=NULL;

}

////// 打印表头///////////////////////////////////////

void print()

{

cout<<"------------------------\n";

cout<<"------------------------\n";

}

////////////////////////////////////////////////////////

///////////////////////////////////////////////////////

////查找结点//// ////////////////////////////////////////

void lookfor(link l,int e)

{

if(l==NULL)

cout<<"链表未建立,请先构造链表\n" ;

else{

link p; int i=0,j=0;

p=l->next;

cout<<"你查找值的位置是:\n " ;

while(p)

{ if(p->date==e)

{ j++;

cout<<i+1<<endl;

}

p=p->next; i++;

}cout<<"查找完毕\n";

if(j==0)

cout<<"你查找的值不在链表中 、\n";

} }

void putline(link &l)

{

if(l==NULL ||l->next==NULL )

cout<<"链表未建立或是空的,请先构造链表\n" ;

else{

link p,q;

p=l->next;

while(p!=NULL)

{

q=p->next;

while(q!=NULL)

{

if(p->date>q->date)

{ ElemType t;

t=p->date;

p->date=q->date;

q->date=t;

}

q=q->next;

}

p=p->next;

} cout<<"链表已经排序 \n";

}

}

/////////////////////////////// //////////////////

///////////////////////////////////////////////////

//////测试函数///// /////////////////////

void main()

{ link L=NULL; int k;

while(1)

{

cout<<"按0退出\n"<<"按1建立\n"<<"按2插入\n"<<"按3删除\n"

<<"按4清空链表\n"<<"按5查找\n"<<"按6进行排续\n" ;

print();

int a,i,j;

cin>>a;

switch(a)

{ case 0: if(L!=NULL)

destorylinst(L) ;

exit(1);

case 1:

IinitList(L);

k=0;

print();

show(L) ;

cout<<"空的链表\n";

cout<<"链表长度为: "<<k<<endl;

print();

cout<<"是否要给链表插入值:y----n\n";

char yy;

yy=getchar();

if(yy=='y')

{

cout<<"请输入值!按回车键后输入下一个,输入0再按回车结束\n";

int bb;

cin>>bb;

while(bb!=0)

{ k++;

listinsert(L,k,bb) ;

cin>>bb;

}

print();

show(L) ; cout<<"链表长度为: "<<k<<endl;

}

else break;

print();

break;

case 2:

if(L!=NULL)

{

cout<<"输入位置:\n";

cin>>i;

while(i>k+1 || i<1)

{

cout<<"位置错误,重新输入插入位置\n" ;

cin>>i;

}

cout<<"输入植;\n";

cin>>j;

listinsert(L,i,j) ;

k++;

print();

show(L);

cout<<"链表长度为:"<<k<<endl;

print();

}

else

{ cout<<"链表不存在,请先建链表\n";

print(); }

break;

case 3:

if(L!=NULL)

{

cout<<"输入位置:\n";

cin>>i;

while(i>k || i<1)

{

cout<<"位置错误,重新输入删除位置\n" ;

cin>>i;

}

listdelete (L,i,j);

cout<<"你删除的是:\n";

cout<<j<<endl ;

k--; print();

show(L);

cout<<"链表长度为:"<<k<<endl;

print();

}

else {

cout<<"链表不存在,请先建链表\n";

print();

}

break;

case 4:

destorylinst(L) ;

cout<<"链表已经清空\n";

print();

break;

case 5:

print();

cout<<"输入要查找的值;\n";

int z;

cin>>z;

lookfor(L,z);

print();

break;

case 6:

putline(L);

if(L!=NULL)

show(L);

print();

break;

default:

break ;

}

}

delete L;

}

# include <iostreamh> struct node {

char data;

node next;

};

node create();

void showList(node head);

int main()

{

node head;

head=create();

showList(head);

return 0; } node create()

{ node head=NULL;

node pEnd=head;

node pS;

char temp;

cout<<"Please input a string end with'#':"<<endl;

do

{ cin>>temp;

if(temp!='#'){

pS=new node;

pS->data=temp;

pS->next=NULL;

if(head==NULL){

head=pS;}

else{

pEnd->next=pS;

}

pEnd=pS;

}

}while(temp!='#');

return head;

}

void showList(node head){

node pRead=head ;

cout<<"The data of link list are: "<<endl;

while(pRead!=NULL)

{ cout<<pRead->data;

pRead=pRead->next;

}

cout<<endl;

}

#include <stdioh>

#include <stdlibh>

struct node

{

int num;

struct node next;

};

/建立链表/

struct node creat(int n)

{

int x, i;

struct node head, p, r;

head=(struct node)malloc(sizeof(struct node));

r=head;

printf("请输入数字\r\n");

for(i=0; i<n; i++)

{

scanf("%d", &x);

p=(struct node)malloc(sizeof(struct node));

p->num=x;

r->next=p;

r=p;

}

r->next=NULL;

return(head);

}

/删除重复结点/

void delet(struct node head)

{

struct node p, q, r;

p=head->next;

while(p!=NULL)

{

q=p;

while(q->next!=NULL)

{

r=q->next;

if(r->num==p->num)

{

if(r->next!=NULL)

{

q->next=r->next;

free(r);

}

else

{

q->next=NULL;

free(r);

}

}

else

{

q=r;

}

}

p=p->next;

}

}

/排序/

void sort(struct node head)

{

struct node p, q, small;

int temp;

for(p=head->next; p->next!=NULL; p=p->next)

{

small=p;

for(q=p->next; q!=NULL ;q=q->next)

{

if(q->num<small->num)

small=q;

}

if(small!=p)

{

temp=small->num;

small->num=p->num;

p->num=temp;

}

}

}

/输出/

void output(struct node head)

{

struct node pt;

pt=head->next;

while(pt!=NULL)

{

printf("%d\r\n", pt->num);

pt=pt->next;

}

}

main()

{

int n;

struct node head;

printf("输入数字的个数n\r\n");

scanf("%d", &n);

head=creat(n);

printf("输入的数字\r\n");

output(head);

delet(head);

printf("删除重复结点后输出数字\r\n");

output(head);

sort(head);

printf("排序后输出数字\r\n");

output(head);

free(head);

}

希望能对你有帮助,俺也学C不到两个月,共同进步啊!

以上就是关于求一份实现单链表的基本 *** 作的c语言程序,包括输入元素,谢谢。全部的内容,包括:求一份实现单链表的基本 *** 作的c语言程序,包括输入元素,谢谢。、用C++或C语言编写一个数据链表程序、用C/C++做一个关于单链表的完整程序。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址:https://54852.com/zz/9693795.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存