⑴ 用c语言实现对单链表的基本操作
#include<stdio.h>
#include<stdlib.h>
typedefintDataType;
typedefstructnode{
DataTypemember;
structnode*next;
}*LinkList,*pNode;
//初始化链表
LinkListGetEmptyList(){
LinkListhead=(pNode)malloc(sizeof(structnode));
head->member=0;
head->next=NULL;
returnhead;
}
//在非增链表中插入结点
voidInsertNode(LinkListhead,DataTypex){
pNodep,q;
for(p=head;p->next!=NULL;p=p->next){
if(p->next->member<=x){
q=(pNode)malloc(sizeof(structnode));
q->member=x;
q->next=p->next;
p->next=q;
return;
}
}
q=(pNode)malloc(sizeof(structnode));
q->member=x;
q->next=p->next;
p->next=q;
}
//新结点插入为首结点
voidPushNode(LinkListhead,DataTypex){
pNodep=(pNode)malloc(sizeof(structnode));
p->member=x;
p->next=head->next;
head->next=p;
}
//删除结点
intDeleteNode(LinkListhead,DataTypex){
pNodep,q;
for(p=head;p!=NULL;p=p->next){
if(p->next->member==x){
q=p->next;
p->next=q->next;
free(q);
return1;//成功删除member(第一个)为x的结点
}
}
return0;//没有找到member为x的结点
}
//查找结点
intFindNode(LinkListhead,DataTypex){
pNodep;
for(p=head->next;p!=NULL;p=p->next){
if(p->member==x)return1;//找到了
}
return0;//没有找到
}
//销毁链表
voidDestroyList(LinkListhead){
pNodeq,p=head;
while(p){
q=p;
p=q->next;
free(q);
}
head=NULL;
}
//遍历链表
voidShowList(LinkListhead){
pNodep=head->next;
while(p!=NULL){
printf("%d",p->member);
p=p->next;
}
printf(" ");
}
intmain(){
DataTypex,res;
LinkListhead=GetEmptyList();
printf("输入一个整数('q'toquit):");
while(scanf("%d",&x)==1){
InsertNode(head,x);//创建非增链表
printf("输入一个整数('q'toquit):");
}
fflush(stdin);
ShowList(head);
printf("输入待查找的整数:");
scanf("%d",&x);
res=FindNode(head,x);
if(res)printf("找到了。 ");
elseprintf("没找到! ");
printf("输入待删除的整数:");
scanf("%d",&x);
res=DeleteNode(head,x);
if(res)printf("成功删除。 ");
elseprintf("没找到数据为:%d的结点! ",x);
ShowList(head);
DestroyList(head);
return0;
}
⑵ C语言链表的简单操作
if(insert = NULL) 这里应该是 if(insert == NULL)
=是赋值,==是比较
⑶ C语言链表操作
包括链表的创建删除添加和释放操作!!
#include<stdio.h>
#include<stdlib.h>
struct node *create();
void print_list(struct node *head);
struct node * insert_node(struct node *h,int x,int y);
struct node * delete_node(struct node *h,int z);
void shifang(struct node *head);
struct node
{
char data;
struct node *next;
};
void main()
{
struct node *head;
int x,y,z;
head=create();
print_list(head);
printf("\n输入插入结点的位置的值和插入的数值:");
scanf("%d%d",&x,&y);
head=insert_node(head,x,y);
print_list(head);
printf("\n输入要删除的结点:");
scanf("%d",&z);
head=delete_node(head,z);
print_list(head);
printf("\n释放链表.\n");
}
struct node *create() //建立链表函数
{
printf("请输入各节点(以-1结尾):\n");
int x;
//定义指针*head,*tail,*s;
struct node *head,*tail,*s;
//head和tail初始化,生成一个头结点
head=tail=(struct node *)malloc(sizeof(struct node));
//在循环中,生成新结点、赋值、连接、尾指针后移
scanf("%d",&x);
while(x!=-1)
{
s=(struct node *)malloc(sizeof(struct node));
s->data=x;
tail->next=s;
tail=s;
scanf("%d",&x);
}
//尾结点的指针域赋NULL
tail->next=NULL;
return head;
}
void print_list(struct node *head) //输出链表函数
{
//定义工作指针*p并赋初值p=head->next;即指向第一个结点
struct node *p;
p=head->next;
//判断链表是否为空,空:输出空表的信息,否则:输出所有结点
if(p==NULL)
printf("The list is NULL.");
else
//在循环中输出当前结点,工作指针后移
{
printf("head->");
while(p!=NULL)
{
printf("%d->",p->data);
p=p->next;
}
printf("end.");
}
}
struct node * insert_node(struct node *h,int x,int y) //添加结点函数
{
struct node *p,*q,*s;
//生成要插入的新结点
s=(struct node *)malloc(sizeof(struct node));
s->data=y;
q=h;
p=h->next;
//查找要插入结点的位置
while((p!=NULL)&&(p->data!=x))
{
q=p;
p=p->next;
}
//插入结点
q->next=s;s->next=p;
return(h);
}
struct node * delete_node(struct node *h,int z) //删除结点函数
{
struct node *p,*q;
q=h;
p=h->next ;
//查找要删除结点的位置
if(p!=NULL)
{
while((p!=NULL)&&(p->data!=z))
{
q=p;
p=p->next;
}
//释放结点
if(p->data ==z)
{
q->next=p->next ;
free(p);
}
}
return(h);
}
void shifang(struct node *head) //释放链表函数
{
struct node *p;
//逐个释放结点
while(head!=NULL)
{
p=head;
head=head->next;
free(p);
}
}
⑷ 用C语言实现建立一个单链表的过程,并实现打印链表中每一个元素,写出完整程序
这是个很简单的链表创建和输出
#include<stdio.h>
#include<stdlib.h>
typedef struct linkednode
{
char data;
struct linkednode *next;
}node,*link_list;//链表节点的结构及重命名
link_list creat()//创建一个链表返回类型是链表的首地址
{
link_list L;
node *p1,*p2;
char data;
L=(node*)malloc(sizeof(node));//开辟存储空间
p2=L;
while((data=getchar())!=' ')//输入回车键时结束输入
{
p1=(node*)malloc(sizeof(node));
p1->data=data;
p2->next=p1;
p2=p1;
}
p2->next=NULL;
return L;
}
void print(link_list L)//把链表输出
{
node *p;
p=L->next;
while(p!=NULL)
{
printf("%c",p->data);
p=p->next;
}
printf(" ");
}
void main()
{
link_list L=NULL;
char x;
printf("请输入链表节点: ");
L=creat();
print(L);
}
⑸ c语言中链表的概念和简单的实现
链表
链表概述
链表是一种常见的重要的数据结构。它是动态地进行存储分配的一种结构。它可以根据需要开辟内存单元。链表有一个“头指针”变量,以head表示,它存放一个地址。该地址指向一个元素。链表中每一个元素称为“结点”,每个结点都应包括两个部分:一为用户需要用的实际数据,二为下一个结点的地址。因此,head指向第一个元素:第一个元素又指向第二个元素;……,直到最后一个元素,该元素不再指向其它元素,它称为“表尾”,它的地址部分放一个“NULL”(表示“空地址”),链表到此结束。
单向链表
单向链表的每个结点中除信息域以外还有一个指针域,用来指出其后续结点,单向链表的最后一个结点的指针域为空(NULL)。单向链表由头指针唯一确定,因此单向链表可以用头指针的名字来命名,例如头指针名为head的单向链表称为表head,头指针指向单向链表的第一个结点。
简单实现为:
#define NULL 0
typedef int DATATYPE
typedef struct node
{DATATYPE info;
node *next;
}LINKLIST;
双向链表
每个结点中只包括一个指向下个结点的指针域,这种链表称为单向链表。如果要在单向链表一个指针所指的当前位置插入一个新结点,就必须从链表头指针开始逐个遍历直到当前指针所指结点的前一结点,修改这个结点的指针。双向链表的每个结点中包括两个指针域,分别指向该结点的前一个结点和后一个结点。在双向链表中由任何一个结点都很容易找到其前面的结点和后面的结点,而不需要在上述的插入(及删除)操作中由头结点开始寻找。
简单实现为:
typedef struct node
{ DATATYPE info;
node *priv, *next;
}DINKLIST;
循环链表
单向链表的最后一个结点的指针域为空(NULL)。如果将这个指针里利用起来,以指向单向链表的第一个结点,就组成一个单向循环链表。
这里有一篇好文章:http://myweb.yzu.e.cn/toby88/c/cstudy/shenru/jiegou/lianbiao.htm
⑹ C语言链表的使用方法
D
答案D设置完,p就从链表中丢掉了。
p就是一个指向结构体node的指针。
p->next就是p包含的执行下一个node的指针,在本题,就是q。
⑺ 用C语言编程实现单链表的基本操作
运行结果如下:
完整代码如下:
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode
{
char data;
LNode *next;
}* LNodePtr;
LNodePtr CreateList()
{
//初始化头节点
LNodePtr head = (LNodePtr)malloc(sizeof(LNode));
head->data = 0;
head->next = NULL;
LNodePtr tNode;//临时节点
char data;
while(true)
{
scanf("%c",&data);
if(data == '