求C语言单链表 源代码

作者&投稿:父汤 (若有异议请与网页底部的电邮联系)
c语言 单链表源代码~

#include "stdafx.h"
#include
#include
typedef char ElemType;
struct LNode
{
ElemType data;
struct LNode *next;
};

//***********************************************************置空表setnull()
void setnull(struct LNode **p)
{
*p=NULL;
}

//************************************************************求长度length()
int length(struct LNode **p)
{
int n=0;
struct LNode *q=*p;
while (q!=NULL)
{
n++;
q=q->next;
}
return(n);
}

//*************************************************************取结点get()
ElemType get(struct LNode **p,int i)
{
int j=1;
struct LNode *q=*p;
while (j<i && q!=NULL) /**//*查找第i个结点*/
{
q=q->next;j++;
}
if (q!=NULL) /**//*找到了第i个结点*/
return(q->data);
else
{
printf("位置参数不正确!
");
return NULL;
}
}

//************************************************************按值查找locate()
int locate(struct LNode **p,ElemType x)
{
int n=0;
struct LNode *q=*p;
while (q!=NULL && q->data!=x) /**//*查找data域为x的第一个结点*/
{
q=q->next;
n++;
}
if (q==NULL) /**//*未找到data域等于x的结点*/
return(-1);
else /**//*找到data域等于x的结点*/
return(n+1);
}

//**********************************************************插入结点insert()
void insert(struct LNode **p,ElemType x,int i)
{
int j=1;
struct LNode *s,*q;
s=(struct LNode *)malloc(sizeof(struct LNode)); /**//*建立要插入的结点s*/
s->data=x;
q=*p;
if (i==1) /**//*插入的结点作为头结点*/
{
s->next=q;
*p=s;
}
else
{
while (jnext!=NULL) /**//*查找第i-1个结点*/
{
q=q->next;j++;
}
if (j==i-1) /**//*找到了第i-1个结点,由q指向它*/
{
s->next=q->next; /**//*将结点s插入到q结点之后*/
q->next=s;
}
else
printf("位置参数不正确!
");
}
}

//*********************************************************删除结点del()
void del(struct LNode **p,int i)
{
int j=1;
struct LNode *q=*p,*t;
if (i==1) /**//*删除链表的头结点*/
{
t=q;
*p=q->next;
}
else
{
while (jnext!=NULL) /**//*查找第i-1个结点*/
{
q=q->next;j++;
}

if (q->next!=NULL && j==i-1) /**//*找到第i-1个结点,由q指向它*/
{
t=q->next; /**//*t指向要删除的结点*/
q->next=t->next; /**//*将q之后的结点删除*/
}
else printf("位置参数不正确!
");
}
if (t!=NULL) /**//*在t不为空时释放该结点*/
free(t);
}

//********************************************************显示链表display()
void display(struct LNode **p)
{
struct LNode *q;
q=*p;
printf("单链表显示:");
if (q==NULL) /**//*链表为空时*/
printf("链表为空!");
else if (q->next==NULL) /**//*链表只有一个结点时*/
printf("%c
",q->data);
else { /**//*链表存在一个以上的结点时*/
while (q->next!=NULL) /**//*显示前面的结点*/
{
printf("%c→",q->data);q=q->next;
}

printf("%c",q->data); /**//*显示最后一个结点*/
}

printf("
");
}

void main()
{
struct LNode *head;
setnull(&head);
insert(&head,'a',1);
insert(&head,'b',2);
insert(&head,'a',2);
insert(&head,'c',4);
insert(&head,'d',3);
insert(&head,'e',1);
display(&head);
printf("单链表长度=%d
",length(&head));
printf("位置:%d 值:%c
",3,get(&head,3));
printf("值:%c 位置:%d
",'a',locate(&head,'a'));
printf("删除第1个结点:");
del(&head,1);
display(&head);
printf("删除第5个结点:");
del(&head,5);
display(&head);
printf("删除开头3个结点:");
del(&head,3);
del(&head,2);
del(&head,1);
display(&head);
}

/**//*
运行结果:
单链表显示:e→a→a→d→b→c
单链表长度=6
位置:3 值:a
值:a 位置:2
删除第1个结点:单链表显示:a→a→d→b→c
删除第5个结点:单链表显示:a→a→d→b
删除开头3个结点:单链表显示:b
*/

下面是我写的代码:

头文件(SLNode.h)

#include

typedef int DataType;

typedef struct node
{
DataType data;
struct node *next;
}SLNode;

void SLNode_Initiate(SLNode **head)
{
if((*head=(SLNode *)malloc(sizeof(SLNode)))==NULL)
exit(1);
(*head)->next=NULL;
}

int SLNode_Length(SLNode *head)
{
int i=0;
SLNode *p;
p=head->next;
while(p!=NULL)
{
i++;
p=p->next;
}
return i;
}

int SLNode_Insert(SLNode *head,int pos,DataType add)
{
SLNode *p,*s;
int i;
if(pos>SLNode_Length(head)||pos<0)
{
printf("the pos in not exsit");
return 0;
}
p=head;
for(i=0;i<pos;i++)
p=p->next;
if((s=(SLNode *)malloc(sizeof(SLNode)))==NULL)
return 0;
s->data=add;
s->next=p->next;
p->next=s;
return 1;
}

int SLNode_Delete_Pos(SLNode *head,int del)
{
SLNode *p,*s;
int i;
if(del>=SLNode_Length(head) || del<0)
{
printf("the pos not esxit
");
return 0;
}
p=head;
for(i=0;i<del;i++)
p=p->next;
s=p->next;
p->next=s->next;
free(s);
return 1;
}

int SLNode_Delete_Data(SLNode *head,DataType del)
{
SLNode *p,*s;
int length=SLNode_Length(head);
p=head;
while(p->next!=NULL)
{
s=p->next;
if(s->data==del)
{
p->next=s->next;
free(s);
}
p=p->next;
}
if(length==SLNode_Length(head))
{
printf("can't find the number
");
return 0;
}
return 1;
}

int SLNode_Get(SLNode *head,int pos,DataType *get)
{
SLNode *p=head;
int i;
if(pos>=SLNode_Length(head) || pos<0)
{
printf("thd pos is not exsit
");
return 0;
}
for(i=0;i<=pos;i++)
p=p->next;
*get=p->data;
return 1;
}

void SLNode_Destroy(SLNode **head)
{
SLNode *p,*s;
p=*head;
while(p!=NULL)
{
s=p;
p=p->next;
free(s);
}
*head=NULL;
}

void SLNode_Print(SLNode *head)
{
SLNode *p;
p=head->next;
while(p!=NULL)
{
printf("%d",p->data);
p=p->next;
}
}

CPP文件:

#include
#include"SLNode.h"

void Menu()
{
puts("*************************************");
puts("A: Add node
");
puts("B: Delete node by pos
");
puts("C: Delete node by data
");
puts("D: Get node by pos
");
puts("E: Print node
");
puts("X: Exit the programe");
puts("*************************************");
}

int main()
{
char select;
int insert;
int get;
int del;
int pos;
SLNode *node;
SLNode_Initiate(&node);
Menu();
while(scanf("%c",&select))
{
fflush(stdin);
switch(select)
{
case 'A':
printf("please enter a number which is insert and it's pos:");
scanf("%d%d",&insert,&pos);
SLNode_Insert(node,pos,insert);
break;
case 'B':
printf("please enter the pos of the number which you want to delete:");
scanf("%d",&pos);
SLNode_Delete_Pos(node,pos);
break;
case 'C':
printf("please enter the number which you want to delete:");
scanf("%d",&del);
SLNode_Delete_Data(node,del);
break;
case 'D':
printf("please enter the pos of the number which you want to get:");
scanf("%d",&pos);
SLNode_Get(node,pos,&get);
break;
case 'E':
SLNode_Print(node);
break;
case 'X':
SLNode_Destroy(&node);
return 0;
default:
printf("please select a correct select
");
}
printf("

");
fflush(stdin);
system("pause");
system("cls");
Menu();
}
return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct people
{
    char name[10];
    int age;
    struct people * next;
};
int main()
{
    struct people * head=NULL;
    struct people * prev , * current;
    int flag=1;
    while(flag!=0)
    {
        printf("请输入学生姓名,年龄:(年龄输入0结束所有输入工作)
");
        current=(struct people *)malloc(sizeof(struct people));
        if(head==NULL)
            head=current;
        else
            prev->next=current;
        current->next=NULL;
        scanf("%s",&current->name);
        scanf("%d",&current->age);
        prev=current;
        flag=current->age;
    }
    printf("Output:
");
    if(head==NULL)
        printf("无资料。
");
    else
    {
        current=head;
        while(current->next!=NULL)
        {
            printf("姓名:%s
年龄:%d

",current->name,current->age);
            current=current->next;
        }
    }
}


至于排序,断开旧链表,将前后指针链接到新的节点就好

如果还有问题欢迎再问哈



#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
using namespace std;
int pow(int i,int j)
{
    int sum=1;
    for(int k=1; k<=j; k++)
    {
        sum*=i;
    }
    return sum;
}
int jinzhi_16(char *number)
{
    char *p=number;
    int sum=0;
    int len=strlen(number);
    int i=0;
    int lone;
    while(*p)
    {
        if(toascii(*p)>=65&&toascii(*p)<=70)
        {
            lone=toascii(*p)-55;
        }
        else
        {
            lone=*p-'0';
        }
        sum+=pow(16,len-i-1)*lone;
        i++;
        *p++;
    }
    return sum;
}
int jinzhi_1_10(char *number,int N)
{
    char *p=number;
    int sum=0;
    int len=strlen(number);
    int i=0;
    int lone;
    while(*p)
    {
        lone=*p-'0';
        sum+=pow(N,len-i-1)*lone;
        i++;
        *p++;
    }
    return sum;
}
int is_reverse(char *number)
{
    int len=strlen(number);
    char *save_number=(char *)malloc(sizeof(char) *(strlen(number)));
    strcpy(save_number,number);
    reverse(save_number,save_number+len-1);
    if(strcmp(save_number,number)==0)
    {
        return true;
    }
}
int return_is_16(char *number)
{
    char *p=number;
    while(*p)
    {
        if(toascii(*p)>=65&&toascii(*p)<=91)
        {
            return true;
        }
        *p++;
    }
}
int main()
{
    int N;
    cin>>N;
    getchar();
    char *number=(char *)malloc(sizeof(char) * 32);
    gets(number);
    int num;
    if(N==16)
    {
        num=jinzhi_16(number);
    }
    else
    {
        num=jinzhi_1_10(number,N);
    }
    int count_step=0;
   
    cout<<"STEP="<<count_step;
    return 0;
}



印台区19536374718: 求C语言单链表 源代码 -
符花膜固: #include<stdio.h>#include<stdlib.h>#include<string.h> struct people { char name[10]; int age; struct people * next; }; int main() { struct people * head=NULL; struct people * prev , * current; int flag=1; while(flag!=0) { printf("请输入学生姓名,年龄:...

印台区19536374718: c语言 单链表源代码 -
符花膜固: /* 输入5个整数:35 46 53 23 8835 46 53 23 88 Press any key to continue*/#include <stdlib.h>#include <stdio.h> typedef int DataType; typedef struct node { DataType data; struct node *next; }*LinkList,Node; LinkList CreateList(int n) { // 创建链表链...

印台区19536374718: 用C语言编程(创建一个单向链表) -
符花膜固: *creat a list*/ #include "stdlib.h" #include "stdio.h" struct list { int data; struct list *next; }; typedef struct list node; typedef node *link; void main() { link ptr,head; int num,i; ptr=(link)malloc(sizeof(node)); ptr=head; printf("please input 5 numbers==>...

印台区19536374718: 求数据结构(c语言)单链表相关代码 -
符花膜固: #include<stdio.h>#include<malloc.h> typedef char datatype; struct listnode { datatype data; listnode *next; }; typedef listnode * linklist; linklist createlist() { linklist head; listnode *p; char ch; head=NULL; while((ch=getchar())!='\n') { p=(listnode*)malloc(...

印台区19536374718: 求C语言单链表实例 -
符花膜固: typedef int status; typedef int Elemtype; const int ok=1; const int error=0; const int overflow=-2; const int TRUE=1; const int FALSE=0; typedef struct LNode{ Elemtype data; struct LNode *next; }LNode,*Linklist; //定义单链表的结点类型; status initlist...

印台区19536374718: 需要用c程序编写单链表并且能在计算机上用VisualC++输入输出的源代码. -
符花膜固: /*功能:编写打印出一个单链表的所有元素*/#include#include typedef struct node{ int data; struct node *next; } *LinkedList;//尾插法(含头结点)创建单链表 LinkedList LinkedListCreate(int a[5]) { int i; LinkedList L,tail,p; //动态内存分配 L=(struct ...

印台区19536374718: C语言编程:26个英文字母建立单链表,可插入删除查找和求长度 -
符花膜固: #include#include typedef struct LNode {char character; struct LNode*next; }LNode,*PLNode; PLNode CreateList()/*创建单链表*/ {PLNode P,head,q; int i; head=(PLNode)malloc(sizeof(LNode)); p=head; p->next=NULL; for(i=0;i {q=(PLNode)...

印台区19536374718: 用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 ...

印台区19536374718: 完成单链表的插入、查询、删除等操作,最后输出链表,请编程实现.(C语言) -
符花膜固: #include#includestruct node{ int data; struct node *next;}; struct node *creat_linklist(){ struct node *head ,*tail,*p;int x; head=tail=NULL; printf("\n请输入一个整数: "); scanf("%d",&x); while(x!=0) { p=(struct node *)malloc(sizeof(struct node)); ...

印台区19536374718: 编写一个C语言程序 实现单链表的基本操作 -
符花膜固: #define ListSize 100 typedef int DataType; typedef struct { DataType list[ListSize]; int length; }SeqList; void InitList(SeqList *L) /*将线性表初始化为空的线性表只需要把线性表的长度length置为0*/ { L->length=0; /*把线性表的长度置为0*/ } int ListEmpty...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网