C语言单向链表的创建,输入,插入和删除的实现

如题所述

/* 线性表-链表的操作: 只提供核心语句 */
#include "stdio.h"
#include "stdlib.h"
#define N 100
typedef int ElemType;/* 链表的存储结构 */
typedef struct LNode{
ElemType data;
struct LNode *next; } LNode,*LinkList;/* 链表的基本操作 *//******* 1.初始化链表 ******/
void InitList(LinkList *L)
{ *L=(LinkList)malloc(sizeof(LNode));
(*L)->next=NULL; }/******* 2.销毁链表 ******/
void DestroyList(LinkList *L)
{ LinkList p;
while(*L!=NULL)
{ p=*L;
*L=(*L)->next;
free(p); }
}
/******* 10.在顺序表第n个位置插入元素e ******/
void ListInsert(LinkList *L, int n, ElemType e)
{ LinkList p,q,new; int i;
q=*L; p=(*L)->next; i=1;
while (p!=NULL && i<n) { q=p; p=p->next; i++; }
new=(LinkList)malloc(sizeof(LNode));
new->data=e;
q->next=new; new->next=p; }/******* 11.删除链表中第n个位置的元素 ******/
void ListDelete(LinkList *L, int n, ElemType *e)
{ LinkList p,q; int i;
q=*L; p=(*L)->next; i=1;
while (p!=NULL && i<n) { q=p; p=p->next; i++; }
*e=p->data;
q->next=p->next; free(p); }/******* 12.遍历链表并输出 ******/
void ListTraverse(LinkList L)
{ LinkList p;
printf("\nList:\t");
p=L->next;
while (p!=NULL)
{ printf("%d\t",p->data);
p=p->next; }
}/******* A(2).后接法建立顺序链表 ******/
void CreateList2(LinkList *L, ElemType a[], int n)
{ LinkList p,new; int i;
p=*L;
for(i=0; i<n; i++)
{ new=(LinkList)malloc(sizeof(LNode));
new->data=a[i];
p->next=new; p=p->next; }
p->next=NULL;
}
/* 主函数 */
main()
{ LinkList La; ElemType a[]={1,3,5,7,9},x;
InitList(&La);//初始化链表
CreateList2(&La,a,5);//建立链表并用数组赋值
ListTraverse(La);//遍历链表
ListInsert(&La, 3, 100);//第三个位置插入100
ListTraverse(La);//遍历链表
ListDelete(&La,5,&x);//删除第五个元素,用x返回
ListTraverse(La);//遍历链表 DestroyList(&La);//销毁链表
} 这是我们刚学的,保证简洁准确
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-08-16
#include<malloc.h>
#include<stdio.h>
#define null 0
struct student
{
long Number;
char Name[20];
long Score;
struct student *Next;
}; int n=0;/*n为全局变量,用来计算链表的结点个数*/ /*-----------------------------------------*/
/*--------------创建结点函数Creat()--------*/
/*-----------------------------------------*/
struct student *Creat()
{ struct student *p1;
struct student *p2;
struct student *head=null;
p1=p2=(struct student *)malloc(sizeof(struct student));/*开辟一段可用内存单元*/
printf("please input the student's Number Name and the Score:\n");
scanf("%ld%s%ld",&p2->Number,p2->Name,&p2->Score); while(p2->Number!=0)
{
n++; if(n==1) /*是否开辟的是第一个结点*/
head=p2;
else
p1->Next=p2; p1=p2;
p2=(struct student *)malloc(sizeof(struct student));
printf("Input the Number the Name and the Score:\n");
scanf("%ld%s%ld",&p2->Number,p2->Name,&p2->Score);
}
p1->Next=null;
return(head);
}
/*------------------------------------------*/
/*--------------查看链表内容函数View()------*/
/*------------------------------------------*/
View(struct student *head)
{
struct student *p;
p=head;
while(p->Next!=null)
{
printf("%ld %s %ld\n",p->Number,p->Name,p->Score);
p=p->Next;
}
printf("%ld %s %ld\n",p->Number,p->Name,p->Score);
} /*-------------------------------------------------*/
/*--------------插入结点函数(前插)Insert()-------*/
/*-------------------------------------------------*/
Insert(struct student *head,int Num) /*head为链表头指针,Num插入链表位置*/
{
int t=1;
struct student *p1,*p2;
p1=head;
if (Num>n||Num<0)
{
printf("input error!!!\n");
return 0;
} while(t<Num-1) /*找到要插入结点的前一个结点*/
{
p1=p1->Next;
t++;
}
p2=(struct student *)malloc(sizeof(struct student));
printf("Input the Number the Name and the Score:\n");
scanf("%ld%s%ld",&p2->Number,p2->Name,&p2->Score);
p2->Next=p1->Next;
p1->Next=p2;
n++; }
/*------------------------------------------*/
/*------------ 删除结点函数Delnode()--------*/
/*-----------------------------------------*/
Delnode(struct student *head,int node)
{
int t=1;
struct student *p1,*p2;
p2=head;
if (node>n||node<1)
{
printf("error!!! The node is not exist!");
return 0;
}
while(t<node-1) /*找到要删除结点的前一个结点*/
{
p2=p2->Next;
t++;
}
p1=p2->Next->Next; /*找到要删除结点的后一个结点*/
free(p2->Next); /*释放要删除的结点空间(删除)*/
p2->Next=p1; /*前一结点指向后一结点*/
n--;
} /*-------------------------------------------------*/
/*--------------逆序重组链表Invert()-------*/
/*-------------------------------------------------*/
struct student *Invert(struct student *head)
{
struct student *p1,*p2;
p1=head;
p2=p1->Next;
head=p2->Next;
p1->Next=null;
while(head->Next!=null)
{
p2->Next=p1;
p1=p2;
p2=head;
head=head->Next;
}
head->Next=p2;
p2->Next=p1;
return head;
} main()
{
int number1,number2;
struct student *head;
head=Creat();
View(head); printf("the n that you want to insert:\n");
scanf("%d",&number1);
Insert(head,number1);
View(head); printf("the node that you want to DELETE:\n");
scanf("%d",&number2);
Delnode(head,number2);
View(head); printf("Inverte the list:\n");
View(Invert(head)); getch();
} ----欢迎来到------c++部落------------Hello Word!--------
无论你是初学者还是专家,只要你热爱编程、交流、分享,c++部落因为你而精彩~
第2个回答  2013-08-16
数组作为存放同类数据的集合,给我们在程序设计时带来很多的方便,增加了灵活性。但数组也同样存在一些弊病。如数组的大小在定义时要事先规定,不能在程序中进行调整,这样一来,在程序设计中针对不同问题有时需要3 0个大小的数组,有时需要5 0个数组的大小,
难于统一。我们只能够根据可能的最大需求来定义数组,常常会造成一定存储空间的浪费。
我们希望构造动态的数组,随时可以调整数组的大小,以满足不同问题的需要。链表就是我们需要的动态数组。它是在程序的执行过程中根据需要有数据存储就向系统要求申请存储空间,决不构成对存储区的浪费。
链表是一种复杂的数据结构,其数据之间的相互关系使链
7.4.1 单链表
图7 - 3是单链表的结构。

单链表有一个头节点h e a d,指向链表在内存的首地址。链表中的每一个节点的数据类型为结构体类型,节点有两个成员:整型成员(实际需要保存的数据)和指向下一个结构体类型节点的指针即下一个节点的地址(事实上,此单链表是用于存放整型数据的动态数组)。链表按此结构对各节点的访问需从链表的头找起,后续节点的地址由当前节点给出。无论在表中访问那一个节点,都需要从链表的头开始,顺序向后查找。链表的尾节点由于无后续节点,其指针域为空,写作为N U L L。
图7 - 3还给出这样一层含义,链表中的各节点在内存的存储地址不是连续的,其各节点的地址是在需要时向系统申请分配的,系统根据内存的当前情况,既可以连续分配地址,也可以跳跃式分配地址。
看一下链表节点的数据结构定义:
struct node
{
int num;
struct node *p;
} ;
在链表节点的定义中,除一个整型的成员外,成员p是指向与节点类型完全相同的指针。
在链表节点的数据结构中,非常特殊的一点就是结构体内的指针域的数据类型使用了未定义成功的数据类型。这是在C中唯一规定可以先使用后定义的数据结构。
? 单链表的创建过程有以下几步:
1 ) 定义链表的数据结构。
2 ) 创建一个空表。
3 ) 利用m a l l o c ( )函数向系统申请分配一个节点。
4 ) 将新节点的指针成员赋值为空。若是空表,将新节点连接到表头;若是非空表,将新
节点接到表尾。
5 ) 判断一下是否有后续节点要接入链表,若有转到3 ),否则结束。
? 单链表的输出过程有以下几步
1) 找到表头。
2) 若是非空表,输出节点的值成员,是空表则退出。
3 ) 跟踪链表的增长,即找到下一个节点的地址。
4) 转到2 )。
[例7-5] 创建一个存放正整数(输入- 9 9 9做结束标志)的单链表,并打印输出。
#include <stdlib.h> /包*含ma l l o c ( ) 的头文件*/
#include <stdio.h>
struct node /*链表节点的结构* /
{
int num;
struct node *next;
} ;
m a i n ( )
{
struct node *creat(); / *函数声明* /
void print();
struct node *head; / * 定义头指针* /
head=NULL;/*建一个空表*/
head=creat(head);/*创建单链表*/
print(head);/*打印单链表*/
}
/******************************************/
struct node*creat(structnode*head)函/数*返回的是与节点相同类型的指针*/
{
struct node*p1,*p2;
p1=p2=(structnode*)malloc(sizeof(structnode));申请/*新节点*/
scanf("%d",&p1->num);/*输入节点的值*/
p1->next=NULL;/*将新节点的指针置为空*/
while(p1->num>0)/*输入节点的数值大于0*/
{
if(head==NULL)head=p1;/*空表,接入表头*/
elsep2->next=p1;/*非空表,接到表尾*/
p2=p1;
p1=(structnode*)malloc(sizeof(structnode));申/请*下一个新节点*/
scanf("%d",&p1->num);/*输入节点的值*/
}
return head;/*返回链表的头指针*/
}
/*******************************************/
void print(struct node*head)输/*出以head为头的链表各节点的值*/
{
struct node *temp;
temp=head;/*取得链表的头指针*/
while(temp!=NULL)/*只要是非空表*/
{
printf("%6d",temp->num);/*输出链表节点的值*/
temp=temp->next;/*跟踪链表增长*/
}
}
在链表的创建过程中,链表的头指针是非常重要的参数。因为对链表的输出和查找都要从链表的头开始,所以链表创建成功后,要返回一个链表头节点的地址,即头指针。