c语言编写链表
Ⅰ 如何用c语言编写一个链表
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
struct Node
{
int data;//数据域
struct Node * next;//指针域
};
/*************************************************************************************
*函数名称:Create
*函数功能:创建链表.
*输入:各节点的data
*返回值:指针head
*************************************************************************************/
struct Node * Create()
{
struct Node *head,*p1,*p2;
head = NULL;
p1 = p2 = (struct Node *)malloc(sizeof(struct Node));
printf("Input the linklist (Input 0 to stop):\n");
scanf("%d",&p1->data);
while(p1->data!=0)
{
if(head == NULL){
head = p1;
}else{
p2->next = p1;
p2 =p1;
}
p1 = (struct Node *)malloc(sizeof(struct Node));
scanf("%d",&p1->data);
}
p2->next = NULL;
return head;
}
/*************************************************************************************
*函数名称:insert
*函数功能:在链表中插入元素.
*输入:head 链表头指针,p新元素插入位置,x 新元素中的数据域内容
*返回值:无
*************************************************************************************/
void insert(struct Node * head,int p,int x)
{
struct Node * tmp = head;
struct Node * tmp2 ;
int i ;
for(i = 0;i<p;i++)
{
if(tmp == NULL)
return ;
if(i<p-1)
tmp = tmp->next;
}
tmp2 = (struct Node *)malloc(sizeof(struct Node));
tmp2->data = x;
tmp2->next = tmp->next;
tmp->next = tmp2;
}
/**************************************************************************************
*函数名称:del
*函数功能:删除链表中的元素
*输入:head 链表头指针,p 被删除元素位置
*返回值:被删除元素中的数据域.如果删除失败返回-1
**************************************************************************************/
int del(struct Node * head,int p)
{
struct Node * tmp = head;
int ret , i;
for(i = 0;i<p;i++)
{
if(tmp == NULL)
return -1;
if(i<p-1)
tmp = tmp->next;
}
ret = tmp->next->data;
tmp->next = tmp->next->next;
return ret;
}
/**************************************************************************************
*函数名称:print
*函数功能:打印链表中的元素
*输入:head 链表头指针
*返回值:无
**************************************************************************************/
void print(struct Node *head)
{
struct Node *tmp;
for(tmp = head; tmp!=NULL; tmp = tmp->next)
printf("%d ",tmp->data);
printf("\n");
}
/**************************************************************************************
*函数名称:main
*函数功能:主函数创建链表并打印链表。
**************************************************************************************/
int main(){
struct Node * head = Create();
print(head);
return 0;
}
Ⅱ 怎样创建一个线性链表(C语言)
/*线性链表的构建*/
#include<stdio.h>
#include<stdlib.h>
typedefstructLnode
{
intdata;
structLnode*next;
}Lnode;
intmain()
{
Lnode*H,*p1,*p2,*p3,*p4;
H=(Lnode*)malloc(sizeof(Lnode));
p1=(Lnode*)malloc(sizeof(Lnode));
p2=(Lnode*)malloc(sizeof(Lnode));
p3=(Lnode*)malloc(sizeof(Lnode));
p4=(Lnode*)malloc(sizeof(Lnode));
p1->data=132;
p1->next=p2;
p2->data=942;
p2->next=p3;
p3->data=158;
p3->next=182;
p4->data=231;
p4->next=NULL;
printf("%d,%d ",p1->data,p3->data);
printf("%d",p1->next->data);
return0;
}
Ⅲ 用C语言编写一个链表
看完你下面的追问 其实 意思是
让你把一个已有的 单链表
变成反向的单链表 对吧
Ⅳ C语言如何创建单链表
C语言创建单链表如下:
#include"stdio.h"
#include"stdlib.h"
#include"malloc.h"
#include "iostream.h"
typedef struct node
{
intdata;
node * next;
}node , * List;
void create(int n)
{
int c;
List s,L;
L=(List)malloc(sizeof(node));
L->next=NULL;
printf("请输入第1个数据:");
scanf("%d",&c);
L->data=c;
for(int i=2;i<=n;i++)
{
s=(List)malloc(sizeof(node));
printf("请输入第%d个数据:",i);
scanf("%d",&c);
s->data=c;
s->next=L;
L->next =s;
}
printf("链表创建成功!");
}
void main()
{
int n;
printf("请你输入链表的个数:");
scanf("%d",&n);
create(n);
}
Ⅳ C语言 关于链表的创建
#include<stdio.h>
#include<stdlib.h>
typedefintelemtype;
typedefstructLnode{
elemtypedata;
Lnode*next;
}Lnode;
Lnode*CreatList(Lnode*Head){
Head=(Lnode*)malloc(sizeof(Lnode));
Head->next=NULL;
Lnode*p=Head;
printf("请输入元素的个数:");
inti,n;
scanf("%d",&n);
for(i=0;i<n;++i){
p->next=(Lnode*)malloc(sizeof(Lnode));
printf("请输入第%d个元素:",i+1);
scanf("%d",&p->next->data);
p=p->next;
}
p->next=NULL;
returnHead;
}
voidAllList(Lnode*head){
Lnode*p=head->next;
while(p){
printf("%d",p->data);
p=p->next;
}
printf("
");
}
intmain(){
Lnode*head=NULL;
head=CreatList(head);
AllList(head);
return0;
}
Ⅵ c语言!!!程序设计:建立一个学生信息链表,包括学号,姓名,成绩.(实现添加,删除,查询,排序,平均)
代码如下:
/*用c语言链表编写一个学生信息系统程序,要求输出学生的学号,姓名,性别,学号,姓名,成绩(实现添加,删除,查询,排序,平均)*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int n=5;
/*
* nodeEntry : 节点数据类型
* nodeADT : 节点结构
* linkADT : 链表结构
*/
typedef struct Student
{
int num;
char name[30];
char sex;
float score1;//语文
float score2;//数学
float score3;//英语
//struct Student *next;
}Student;
typedef struct linkCDT {
nodeADT head;
}*linkADT;
/*
* InitLink : 初始化链表
* CreateNode : 创建节点
* AppendLink : 添加数据
*/
nodeADT CreateNode(Student entry) {
nodeADT p=(nodeADT)malloc(sizeof*p);
p->entry=entry,p->next=0;
return p;
}
/*
SortLink : 排序链表
//按学号排序
void SortLinkID(linkADT link) {
nodeADT pHead,pRear,p,tp;
if (!link) return;
for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {
for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)
if (pHead->entry.num>=p->entry.num)
tp->next=p->next,p->next=pHead,pHead=p,p=tp;
if (!pRear) link->head=pHead;
else pRear->next=pHead;
pRear=pHead;
}
//按英语成绩排序
void SortLinkEnglish(linkADT link) {
nodeADT pHead,pRear,p,tp;
if (!link) return;
for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {
for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)
if (pHead->entry.score3>=p->entry.score3)
tp->next=p->next,p->next=pHead,pHead=p,p=tp;
if (!pRear) link->head=pHead;
else pRear->next=pHead;
pRear=pHead;
}
}
//按姓名的字典序进行排序
void SortLinkName(linkADT link) {
nodeADT pHead,pRear,p,tp;
if (!link) return;
for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {
for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)
if (pHead->entry.name[0]>=p->entry.name[0])
tp->next=p->next,p->next=pHead,pHead=p,p=tp;
if (!pRear) link->head=pHead;
else pRear->next=pHead;
pRear=pHead;
}
}
//按姓名的长度进行排序
void SortLinkNameLength(linkADT link) {
nodeADT pHead,pRear,p,tp;
if (!link) return;
for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {
for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)
if (strlen(pHead->entry.name)>=strlen(p->entry.name))
tp->next=p->next,p->next=pHead,pHead=p,p=tp;
if (!pRear) link->head=pHead;
else pRear->next=pHead;
pRear=pHead;
}
循环链表是与单链表一样
是一种链式的存储结构,所不同的是,循环链表的最后一个结点的指针是指向该循环链表的第一个结点或者表头结点,从而构成一个环形的链。
循环链表的运算与单链表的运算基本一致。所不同的有以下几点:
1、在建立一个循环链表时,必须使其最后一个结点的指针指向表头结点,而不是象单链表那样置为NULL。此种情况还使用于在最后一个结点后插入一个新的结点。
2、在判断是否到表尾时,是判断该结点链域的值是否是表头结点,当链域值等于表头指针时,说明已到表尾。而非象单链表那样判断链域值是否为NULL。
以上内容参考:网络-链表
Ⅶ 如何用C语言创建一个链表,实现增、删、改、查
#include
Ⅷ 用C语言编程实现单链表的基本操作
第二个显示为什么?为什么什么东西都没有、
#include<stdio.h>
typedef
struct
sample
{
char
ch;
struct
sample
*next;
}LNode;
LNode
*Createlist(LNode
*head)
//创建单链表,头插入法
{
LNode
*p;
if((head=(LNode
*)malloc(sizeof(LNode)))==NULL)
printf("aply
error\n");
head->next=NULL;
head->ch
=
'\0';
int
i=
0;
printf("请一次输入A-Z:\n");
for(i
=
0
;
i
<
26;
++i)
{
p=(LNode
*)malloc(sizeof(LNode));
if(!p)
printf("aply
error\n");
scanf("%c",&p->ch);
p->next
=
head->next;
head->next=p;
}
return
head;
}
LNode
*EncryptList(LNode*
head)
{
LNode
*p=
head,*q
=
head->next,*r
=
head->next;
while(p->next)
{
p
=
p->next;
}
int
i
=
0
;
for(i
=
0
;
i
<
3;
i++)
{
p->next
=
r;
p
=
p->next;
q
=
q->next;
r
=
q;
}
p->next
=NULL;
head->next
=
q;
return
head;
}
void
ListPrint(LNode
*head)
{
LNode
*p
=
head->next;
while(p->next!=NULL)
{
printf("%c\t",p->ch);
p=p->next;
}
printf("%c\n",p->ch);
}
int
main(void)
{
LNode
*head;
head
=
Createlist(head);//链表初始化
ListPrint(head);
//打印单链表数据
head
=
EncryptList(head);
ListPrint(head);
return
0;
}
看看。