请教单链表的创建、添加、删除和遍历!C语言实现~~

ATMCash4423 2011-11-06 08:14:52
请不要让我看书,我看了,还是不明白。
请不要让我去网上找代码,我找了,还是看不明白。

能不能浪费您一点时间,用C简单实现一个单链表的创建、添加、删除和遍历!
最重要的,请写下详细的注释!
比如声明变量时请说明其用途!

跪谢啊~~这些天我学链表都头疼啊~~~~
...全文
453 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ATMCash4423 2011-11-09
  • 打赏
  • 举报
回复

// lianbiao.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
//#include <vld.h>
#include <malloc.h>
#include <windows.h>

struct lb
{
int num;
lb *next;
};

lb *head;

lb *init_list()
{
head = (lb *)malloc(sizeof(lb));
head->num = 0;
head->next = NULL;
return head;
}

lb *add(lb *head,int num)
{
lb *c = head; //当前节点
lb *p = NULL; //新节点

while (c->next != NULL)
{
c = c->next;
}
p = (lb *)malloc(sizeof(lb));
p->num = num;
p->next = NULL;
c->next = p;

return head;
}

lb *showlist(lb *head)
{
lb *c;
for (c = head->next;c->next != NULL;c = c->next)
{
printf("%d\n",c->num);
}
printf("%d\n",c->num);
return head;
}

void deletelist(lb *head)
{
lb *p;
lb *c;
for (c = head;c->next != NULL;c = p)
{
p = c->next;
free(c);
}
free(c);
}

lb *insertlist(lb *head,int num,int data)
{
lb *c;
lb *p;
int count = 1;
for (c = head->next;c->next != NULL;c = c->next)
{
count++;
if (count == num)
{
p = (lb *)malloc(sizeof(lb));
p->num = data;
p->next = c->next;
c->next = p;
}
}
if(count < num)
printf("链表中没有指定的位置,或者不允许从最后插入。");
return head;
}

int _tmain(int argc, _TCHAR* argv[])
{
init_list();
for (int i = 1;i < 6; ++i)
{
add(head,i);
}
insertlist(head,3,10);

showlist(head);


deletelist(head);

system("pause");
return 0;
}



这是我搞了一晚上自己写的~~虽然没看各位的,但还是谢谢各位啊!
Dead_Cicle 2011-11-08
  • 打赏
  • 举报
回复
无非指针域,数字域。

头指针->加链,断链,接链。保证指针域带着数字域。

理解上还是要带着想象思维。看短代码,或者伪代码。先理解概念,别着急看长串的代码。
NeilHappy 2011-11-08
  • 打赏
  • 举报
回复
刚才格式不对。看这个
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define mStu (Stu *)malloc(sizeof(Stu))


typedef struct student
{
int age;
struct student *next;
}Stu;



int main()
{
int count=0;
Stu *head=NULL;//声明头结点
Stu *ListCreat(int *p,Stu *head);//建立链表
Stu *ListOrder(int count,Stu *head);//进行排序
void Print(Stu *head);//打印链表
Stu *ListAdd(Stu *head);//插入链表
Stu *ListDelete(Stu *head);//删除链表

head=ListCreat(&count,head);
Print(head);printf("\n");
head=ListOrder(count,head);
Print(head);printf("\n");
head=ListAdd(head);
Print(head);printf("\n");
head=ListDelete(head);
Print(head);
getch();
}


Stu *ListCreat(int *p,Stu *head)
{
char answer=0;
Stu *New=mStu, *Old=NULL;
do
{
(*p)++;
printf("Please input a age:");
fflush(stdin);
scanf("%d",&New->age);
if(head==NULL)
head=New;
else
{
Old->next=New; //这里不给Old分配内存行吗??答:可以的,因为第一次不会执行这个else 语句 然后Old就会指向New的空间,第二次Old就会有指向的空间了,不会出现内存错误
}
Old=New;
New->next=NULL;
New=mStu;
printf("Do you want to input again?(y or n)");
fflush(stdin);
scanf(" %c",&answer);
}while(tolower(answer)=='y');
return head;
}
void Print(Stu *head)
{
Stu *p1=head;
while(p1!=NULL)
{
printf("%d ",p1->age);
p1=p1->next;
}
}


Stu *ListOrder(int count,Stu *head)
{
int temp=0;
Stu *p1=NULL,*p2=NULL;
for(int i=0;i<count;i++)
{
for(p1=head,p2=head->next;p2!=NULL;p1=p1->next,p2=p2->next)
{
if(p1->age > p2->age)
{
temp=p1->age;
p1->age=p2->age;
p2->age=temp;
}
}
}
return head;
}


Stu *ListAdd(Stu *head)
{
char answer1=0;
Stu *add=NULL;
Stu *p1=NULL,*p2=NULL;
do
{
add=mStu;
p1=head;
p2=NULL;//注意,这里对add,p1,p2做再次初始化非常重要
printf("\nPlease add a list:");
fflush(stdin);
scanf("%d",&add->age);
while(p1!=NULL && p1->age < add->age)
{
p2=p1;
p1=p1->next;
}
if(p1==NULL)
{
p2->next=add;
add->next=NULL;
}
else if(p2==NULL)
{
add->next=head;
head=add;
}
else
{
p2->next=add;
add->next=p1;
}
printf("Do you want to add again??");
fflush(stdin);
scanf("%c",&answer1);
}while(tolower(answer1)=='y');
return head;
}


Stu *ListDelete(Stu *head)
{
char answer2=0;
Stu *Del=NULL,*p1=NULL,*p2=NULL;
do
{
Del=mStu;
p1=head;
p2=NULL;//注意,这里也很重要
printf("Which list do you want to delete??");
scanf("%d",&Del->age);
fflush(stdin);
while(p1!=NULL && p1->age!=Del->age)
{
p2=p1;
p1=p1->next;
}
if(p2==NULL)
head=head->next;
else if(p1==NULL)
printf("\nThere is no this data exit!!! Input Error!!!");
else
{
p2->next=p1->next;//删除p1结点
free(p1);
}
printf("\nDo you want to delete again???(y or n)");
scanf("%c",&answer2);
}while(tolower(answer2)=='y');
return head;
}
NeilHappy 2011-11-08
  • 打赏
  • 举报
回复
这是我学C语言的时候写的代码,希望对楼主有帮助

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define mStu (Stu *)malloc(sizeof(Stu))


typedef struct student
{
int age;
struct student *next;
}Stu;



int main()
{
int count=0;
Stu *head=NULL;//声明头结点
Stu *ListCreat(int *p,Stu *head);//建立链表
Stu *ListOrder(int count,Stu *head);//进行排序
void Print(Stu *head);//打印链表
Stu *ListAdd(Stu *head);//插入链表
Stu *ListDelete(Stu *head);//删除链表

head=ListCreat(&count,head);
Print(head);printf("\n");
head=ListOrder(count,head);
Print(head);printf("\n");
head=ListAdd(head);
Print(head);printf("\n");
head=ListDelete(head);
Print(head);
getch();
}


Stu *ListCreat(int *p,Stu *head)
{
char answer=0;
Stu *New=mStu, *Old=NULL;
do
{
(*p)++;
printf("Please input a age:");
fflush(stdin);
scanf("%d",&New->age);
if(head==NULL)
head=New;
else
{
Old->next=New; //这里不给Old分配内存行吗??答:可以的,因为第一次不会执行这个else 语句 然后Old就会指向New的空间,第二次Old就会有指向的空间了,不会出现内存错误
}
Old=New;
New->next=NULL;
New=mStu;
printf("Do you want to input again?(y or n)");
fflush(stdin);
scanf(" %c",&answer);
}while(tolower(answer)=='y');
return head;
}
void Print(Stu *head)
{
Stu *p1=head;
while(p1!=NULL)
{
printf("%d ",p1->age);
p1=p1->next;
}
}


Stu *ListOrder(int count,Stu *head)
{
int temp=0;
Stu *p1=NULL,*p2=NULL;
for(int i=0;i<count;i++)
{
for(p1=head,p2=head->next;p2!=NULL;p1=p1->next,p2=p2->next)
{
if(p1->age > p2->age)
{
temp=p1->age;
p1->age=p2->age;
p2->age=temp;
}
}
}
return head;
}


Stu *ListAdd(Stu *head)
{
char answer1=0;
Stu *add=NULL;
Stu *p1=NULL,*p2=NULL;
do
{
add=mStu;
p1=head;
p2=NULL;//注意,这里对add,p1,p2做再次初始化非常重要
printf("\nPlease add a list:");
fflush(stdin);
scanf("%d",&add->age);
while(p1!=NULL && p1->age < add->age)
{
p2=p1;
p1=p1->next;
}
if(p1==NULL)
{
p2->next=add;
add->next=NULL;
}
else if(p2==NULL)
{
add->next=head;
head=add;
}
else
{
p2->next=add;
add->next=p1;
}
printf("Do you want to add again??");
fflush(stdin);
scanf("%c",&answer1);
}while(tolower(answer1)=='y');
return head;
}


Stu *ListDelete(Stu *head)
{
char answer2=0;
Stu *Del=NULL,*p1=NULL,*p2=NULL;
do
{
Del=mStu;
p1=head;
p2=NULL;//注意,这里也很重要
printf("Which list do you want to delete??");
scanf("%d",&Del->age);
fflush(stdin);
while(p1!=NULL && p1->age!=Del->age)
{
p2=p1;
p1=p1->next;
}
if(p2==NULL)
head=head->next;
else if(p1==NULL)
printf("\nThere is no this data exit!!! Input Error!!!");
else
{
p2->next=p1->next;//删除p1结点
free(p1);
}
printf("\nDo you want to delete again???(y or n)");
scanf("%c",&answer2);
}while(tolower(answer2)=='y');
return head;
}
hackbuteer1 2011-11-08
  • 打赏
  • 举报
回复
这个有详细的注释,楼主可以参考一下的。。。
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct student
{
int id; //学号
char name[20]; //姓名
struct student *next;
}student;
student *head = NULL;
int length; //链表的长度

void create()
{
student *p1,*p2;
length=0;
p1=(student *)malloc(sizeof(student));
p1->id=-1;
if(head==NULL)
head=p1;
printf("请输入学生的学号、姓名:\n");
while(1) //学号为0的时候退出
{
p2=(student *)malloc(sizeof(student));
scanf("%d %s",&p2->id,p2->name); //输入学生信息
if(p2->id==0)
{
printf("链表创建完成!\n");
break;
}
length++; //链表的长度
p1->next=p2;
p2->next=NULL;
p1=p1->next;
}
return ;
}
void display()
{
student *p=head->next;
printf("链表中所有的学生信息如下:\n");
while(p!=NULL)
{
printf("%d %s\n",p->id,p->name);
p=p->next;
}
return ;
}

void search()
{
int num;
student *p=head->next;
printf("需要查找的学生学号为:");
scanf("%d",&num);
while(p!=NULL)
{
if(p->id==num)
{
printf("学生学号为%d的学生的信息如下:\n",num);
printf("%d %s/n",p->id,p->name);
return;
}
p=p->next;
}
if(p==NULL)
printf("无此记录!\n");

return ;
}
void insert()
{
int num,i;
student *p,*q;
p=head;
printf("请输入你要插入位置: ");
scanf("%d",&num);

if(num>length)
{
printf("找不到要插入的位置\n");
return ;
}
else
{
printf("请输入你要插入的学生的信息:\n");
q=(student *)malloc(sizeof(student));
scanf("%d %s",&q->id,q->name);
while(p!=NULL)
{
if(p->id==q->id)
{
printf("该学号已经存在,无法插入!\n");
return ;
}
p=p->next;
}
p=head;
for(i=0;i <num;i++)
p=p->next;
q->next=p->next;
p->next=q;
length++;
printf("插入成功!\n");
return ;
}
}
void Delete()
{
int num;
student *p,*q;
q=head,p = head->next;

printf("请输入要删除的学生的学号:\n");
scanf("%d",&num);

while(p!=NULL)
{
if(p->id==num)
{
q->next=p->next;
free(p);
length--;
printf("删除成功!\n");

return ;
}
p=p->next;
q=q->next;
}
if(p==NULL)
{
printf("找不到要删除的编号!\n");
return ;
}
}
void menu()
{
printf("________________________________________________________________\n");
printf("| 0、 退出系统 |\n");
printf("| 1、 建立链表 |\n");
printf("| 2、 显示链表 |\n");
printf("| 3、 查找链表中的某个元素 |\n");
printf("| 4、 删除链表中指定学号的结点 |\n");
printf("| 5、 指定的位置上插入一个新结点 |\n");
printf("________________________________________________________________\n");

return ;
}
int main(void)
{
int a;
menu();
while(1)
{
printf("请选择相应的功能:");

cin>>a;
switch(a)
{
case 0:
return 0;
case 1:
create(); //从键盘输入学生信息
menu();
break;
case 2:
if(head)
{
display();
menu();
}
else
{
printf("链表为空,请先建立链表!\n");
menu();
}
break;
case 3:
if(head)
{
search();
menu();
}
else
{
printf("链表为空,请先建立链表!\n");
menu();
}
break;
case 4:
if(head)
{
Delete();
menu();
}
else
{
printf("链表为空,请先建立链表!\n");
menu();
}
break;
case 5:
if(head)
{
insert();
menu();
}
else
{
printf("链表为空,请先建立链表!\n");
menu();
}
break;
default:
break;
}
}
return 0;
}
ForestDB 2011-11-07
  • 打赏
  • 举报
回复
C的指针过关了没??
HFred 2011-11-07
  • 打赏
  • 举报
回复
其实光看书真的很难明白,是一些比较抽象的东西,自己可以动手画画,在网上找些链表讲解的视频动画,会比较容易理解,当你自己试着去做,虽然刚开始不会,慢慢去修改自己的程序,调试,完善,到最终成功,保证你就会了,也有了自己的理解,想忘都忘不了
  • 打赏
  • 举报
回复
顶,同求....
無_1024 2011-11-06
  • 打赏
  • 举报
回复
这个随便百度就有了 自己百度去吧
wintree 2011-11-06
  • 打赏
  • 举报
回复
这东西自己理解!!!!
yimingxinshou 2011-11-06
  • 打赏
  • 举报
回复
有些函数的名称和参数设置和课本上不太一样,是根据自己习惯写的,包涵啊
# include<stdio.h>
# include<stdlib.h>
# define OVERFLOW -2
# define OK 1
# define ERROR 0
typedef int ElemType;
//
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
//
void main()
{
int n,e,i,j,k;
LinkList L;
LNode S;
L=&S;
LinkList CreatList(int n);
int GetElem(LinkList L,int i);
int ListInsert(LinkList L,int i,ElemType e);
int ListDelete(LinkList &L,int i);
void GetList(LinkList L);
printf("请输入链表中元素的个数:\n");
scanf("%d",&n);
L=CreatList(n);
printf("输出单链表:\n");
GetList(L);
printf("请输入要输出元素的位置:\n");
scanf("%d",&i);
GetElem(L,i);
printf("请输入要插入的元素的位置:\n");
scanf("%d",&j);
printf("请输入要插入的元素的值:\n");
scanf("%d",&e);
ListInsert(L,j,e);
printf("输出单链表:\n");
GetList(L);
printf("请输入要删除的元素的位置:\n");
scanf("%d",&k);
ListDelete(L,k);
printf("输出单链表:\n");
GetList(L);

}
//
LinkList CreatList(int n)
{
LinkList p,q;
LinkList L;
int i,x;
L=(LinkList)malloc(sizeof(LNode));
q=L;
L->next=NULL;
printf("请依次输入链表中的数值:\n");
for(i=0;i<n;i++)
{
scanf("%d",&x);
p=(LinkList)malloc(sizeof(LNode));
p->data=x;
p->next=NULL;
q->next=p;
q=p;
}
return L;
}
//
int GetElem(LinkList L,int i)
{
LinkList p;
int j=1,e;
p=L->next;
while(p&&j<i)
{
p=p->next;
++j;
}
if(!p||j>i)
return ERROR;
e=p->data;
printf("要求输出的元素为:\n%d\n",e);
return OK;
}
//
int ListInsert(LinkList L,int i,ElemType e)
{
LinkList p,s;
int j=0;
p=L;
while(p&&j<i-1)
{
p=p->next;
++j;
}
if(!p||j>i-1)
return ERROR;
s=(LinkList)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s;
return OK;
}
//
int ListDelete(LinkList &L,int i)
{
LinkList p,q;
int j=0,e;
p=L;
while(p->next&&j<i-1)
{
p=p->next;
++j;
}
if(!(p->next)||j>i-1)
return ERROR;
q=p->next;
p->next=q->next;
e=q->data;
free(q);
printf("删除的元素为:\n%d\n",e);
return OK;
}
//
void GetList(LinkList L)
{
LinkList p;
p=L->next;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
ryfdizuo 2011-11-06
  • 打赏
  • 举报
回复
哎,,,这些都是单链表的最基本操作啊。
强迫自己看书,一遍看不懂就多看几遍,都是这么过来的。
ATMCash4423 2011-11-06
  • 打赏
  • 举报
回复
顶上去!
whoami1978 2011-11-06
  • 打赏
  • 举报
回复
帮顶,我也想学

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧