C语言链表基本操作源代码

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
*/
void revers(struct node *head) {
f=!f; if (head->next==NULL)
{ printf("空链表\n"); return ;
} struct node *p; struct node *q; p=head->next; q=p->next; if (q==NULL) {
printf("%d ",p->x); fputc(p->x+'0',fp); fputc('\n',fp); } k++; p=p->next; } printf("\n"); fclose(fp); return k; } /* 本函数实现线性链表的打印,每读一个节点打印一个节点。入口参数:head 是链表的头 结点。
q=p->next; p->next=q->next; free(q); printf("删除成功\n"); return ; } else p=p->next; } printf("你要删除的数据不存在\n"); } /* 本函数实现删除指定数值,通过线性遍历链表,直到找到待删除数据。入口参: c 是待删 除的数据,head 是链表的头结点。 */ void deleteall(struct node *head)
return ; } while (q) {
p->next=q->next; q->next=head->next; head->next=q; q=p->next; } printf("倒转完成!\n"); } /* 本函数实现将链表逆转。入口参数:head 是链表的头结点。
*/ bool cmp(int a,int b) {
p=p->next; } q=(struct node*)malloc(sizeof(struct node)); q->x=c; q->next=p->next; p->next=q; } else if (!f) {
p=head; while (p->next && (p->next)->x>c) {
printf("*查找 输入 3
*\n");
printf("*删除 输入 4
*\n");
printf("*全部删除 输入 5
*\n");
printf("*打印 输入 6
*\n");
printf("*链表长度 输入 7
*\n");
printf("*反转 输入 8
*\n");
printf("*退出 输入 0
*/ int display(struct node *head,int f) {
FILE *fp; fp=fopen("output.txt","w"); if (head->next==NULL) {
printf("空链表\n"); return 0; } struct node *p; p=head->next; int k=0; while (p) { if (f){
char x; struct node *next; }; void Insert(int n,int c,struct node *head) { struct node *p; struct node *q; if (head->next==NULL) {
q=(struct node*)malloc(sizeof(struct node)); q->x=c; q->next=NULL; head->next=q; return; } p=head->next; if (n==1) { q=(struct node*)malloc(sizeof(struct node)); q->x=c; q->next=p; head->next=q; return ; } int k=1; while(p) {
struct node *p; struct node *q;
if (head->next==NULL) {
printf("链表为空\n"); return ; } p=head->next; if (p->x==c) { head->next=p->next; delete(p); printf("删除成功\n"); return ; } while (p->next) { if ((p->next)->x==c) {
*\n");
printf("***********************\n");
scanf("%d",&c);
switch (c) { case 1:
printf("请输入你要插入的位置\n"); scanf("%d",&n); printf("请输入你要插入的数据\n"); scanf(" %d",&s); Insert(n,s,head); break; case 2: printf("请输入你要插入的数据\n"); scanf(" %d",&s); Insert(s,head); break; case 3: printf("请输入你要查找的数据\n"); scanf(" %d",&s); search(s,head); break; case 4: printf("请输入你要删除的数据\n"); scanf(" %d",&s); delete1(s,head); break; case 5: deleteall(head); break; case 6: display(head,1); break; case 7: k=display(head,0); printf("链表长度为%d\n",k); break; case 8: revers(head); break; } } return 0; }
p=p->next; } q=(struct node*)malloc(sizeof(struct node)); q->x=c; q->next=p->next; p->next=q; } } /* 本函数实现从末尾插入数据,入口参数,c 代表待插入数据的值,head 是链表的头结点。 */ void search(int c,struct node *head) { struct node *p; int f=0; if (head->next==NULL) { printf("链表为空\n"); return ; } p=head->next; int k=1; while (p) { if (p->x==c) {
head->next=NULL;
int k;
prset(head);
while (c)
{
printf("***********************\n");
printf("*请选择你要的操作 *\n");
// printf("*按位置插入 输入 1 *\n");
printf("*插入 输入 2
*\n");
int len;
Leabharlann Baidu
int k=0;
fgets(name,N,fp);
len=strlen(name);
for (int i=0;i<len;i++)
a[i]=name[i]-'0';
sort(a,a+len,cmp);
struct node *p;
struct node *q;
p=head;
for (int i=0;i<len-1;i++){
// shiy1.cpp : Defines the entry point for the console application. //线性链表基本操作:删除,排序,查找,逆序,长度
#include "stdafx.h"
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <string> #define N 40 using namespace std; int f=1; struct node {
{ struct node *p; while (head->next) { p=head->next; head->next=p->next; free(p); } printf("删除成功!\n");
} /*
本函数实现清空线性链表,每读一个节点删除一个节点。入口参数:head 是链表的头结 点。
printf("要查找的数据在第%d 位\n",k); f=1;//return; } k++; p=p->next; }
if(!f) printf("你要查找的数据不在\n"); return ; } /* 顺序遍历链表的节点,将每个节点的值和要查找的值比较,直到找到待查找的数。入口
参数,c 是要查找的数据的值,head 是链表的头结点。 */ void delete1(int c,struct node *head) {
return a<b; }
void prset(struct node *head) {
FILE *fp; int a[40]; memset(a,0,sizeof(a)); char name[40]; char name1[40]; printf("请输入存放你要初始化链表的数据的文件名\n"); gets(name); fp=fopen(name,"r");
if (!(p->next)) break; if (n==k+1) {
q=(struct node*)malloc(sizeof(struct node)); q->x=c; q->next=p->next; p->next=q; return ; } else { p=p->next; k++; } } if (n>=k) { q=(struct node*)malloc(sizeof(struct node)); q->x=c; q->next=NULL; p->next=q; } return ; } /* 本函数实现从指定位置插入数据。三个入口参数,n 代表插入的位置,c 代表待插入数的 值,head 是链表的头结点。 */ void Insert(int c,struct node *head) { struct node *p; struct node *q; if (head->next==NULL) { q=(struct node*)malloc(sizeof(struct node)); q->x=c; q->next=NULL; head->next=q; } else if(f) { p=head; while (p->next && (p->next)->x<c) {
q=(struct node*)malloc(sizeof(struct node));
q->x=a[i];
q->next=NULL;
p->next=q;
p=q;
}
fclose(fp);
}
int main()
{
int c=1;
int n;
int s;
struct node *head;
head=(struct node*)malloc(sizeof(struct node));
相关文档
最新文档