小白不会创建单链表
我想了很久,我觉得我挺认真看小甲鱼的视频了,我不会也有自己百度,但是还是不太懂。。{:10_266:}{:10_266:}#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 20
typedef struct List
{
int data;
struct List *next;
}List;
List *creatList(List *head)
{
List *s,*r;
head=(List *)malloc(sizeof(List));
head->next=0;
int i;
srand(time(0));
r=head;
for(i=0;i<N;i++)
{
s=(List *)malloc(sizeof(List));
s->data=rand()%100;printf("%d ",s->data);
r->next=s;
r=s;
}
r->next='\0';
return head;
}
main(void)
{
List *head;
head=creatList(head);
while(head)
{
printf("%d ",head->data);
head=head->next;
}
}
到底哪里不对了,我快崩了 问一下,你用的是C语言吗?我看你写的有的数据赋值我不是很理解,我用C语言的语法给你改的
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 20
typedef struct List
{
int data;
struct List *next;
}List;
List *creatList(void) //反正要返回头结点就别当参数给进来了
{
int i;
List *head, *s, *r; //声明一个头结点
head=(List *)malloc(sizeof(List));
head->next=NULL; //指针指向空是NULL,不是0
srand(time(0));
r=head;
for(i=0;i<N;i++)
{
s=(List *)malloc(sizeof(List));
s->data=rand()%100;
//printf("%d ",s->data); 这里打印没意义的,你要也可以
r->next=s;
r=s;
}
r->next=NULL;//同理
return head;
}
main(void)
{
List *head = NULL;
List *p;
head=creatList();
p = head->next;
while(p) // 判断头结点指向,不为空就是有结点
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
#include<stddef.h>
#include<stdbool.h>
typedef struct Node
{
int data;/*数据域*/
struct Node * pNext;/*指针域*/
}NODE,* PNODE;
PNODE create_list();/*创建链表*/
void traverse_list(PNODE pHEad);/*遍历*/
bool delete_list(PNODE pHead,int,int*);/*删除*/
void sort_list(PNODE);/*排序*/
bool is_empty_list(PNODE pHead);/*判断是否为空*/
int len_list(PNODE pHead);/*链表长度*/
bool inser_list(PNODE pHead,int pos,int val);/*插入元素*/
int main()
{
int a,b;/*方便insert_list函数插入数据*/
int val;/*方便后面删除元素实验*/
PNODE pHead = NULL; /*等价于 struct Node* pHead = NULL*/
pHead = create_list();/*创建一个非循环单链表,并将该链表的头节点的地址赋给pHead*/
traverse_list(pHead);/*遍历*/
if(is_empty_list(pHead))
{
printf("The list is empty!\n");
exit(-1);/*用户输入0,终止程序*/
}
else
{
printf("The list is existent!\n");
}
/*输出长度*/
int len = len_list(pHead);
printf("The list'length is %d\n",len);
/*遍历链表*/
sort_list(pHead);
traverse_list(pHead);
/*插入元素*/
printf("Input the num:");/*val*/
scanf("%d",&a);
printf("选择位置:");/*pos*/
scanf("%d",&b);
inser_list(pHead,a,b);
printf("插入新元素后:");
traverse_list(pHead);
/*删除元素*/
printf("Input the num which you what to delete:");
scanf("%d",&val);
if(delete_list(pHead,2,&val))
{
printf("删除成功\n");
}
else
{
printf("删除失败!你查找的元素不存在\n");
}
traverse_list(pHead);
return 0;
}
/*构造链表*/
PNODE create_list()
{
int len,val;
PNODE pHead = (PNODE)malloc(sizeof(NODE));/*分配一个不存放数据的头节点*/
if(NULL == pHead)
{
printf("Failed!\n");
exit(-1);
}
PNODE pNULL = pHead;
pNULL->pNext = NULL;
printf("Input the num of linked list nodes:");
scanf("%d",&len);
printf("\n");
for(int i=0;i<len;i++)
{
printf("input the num of %d node:",i+1);
scanf("%d",&val);
PNODE pNew = (PNODE)malloc(sizeof(NODE));/*动态分配内存*/
if(pNew == NULL)
{
printf("Failed!\n");
exit(-1);/*终止程序*/
}
else
{
pNew->data = val;
pNULL->pNext = pNew;
pNew->pNext = NULL;
pNULL = pNew;
}
}
return pHead;
}
/*遍历*/
void traverse_list(PNODE pHead)
{
PNODE p = pHead->pNext;
while(NULL != p)
{
printf("%d ",p->data);
p = p->pNext;
}
printf("\n");
return;
}
/*判断是否为空*/
bool is_empty_list(PNODE pHead)
{
if(NULL == pHead->pNext)
{
return true;
}
else
{
return false;
}
}
/*链表长度*/
int len_list(PNODE pHead)
{
PNODE p = pHead->pNext;
int len = 0;
while(NULL != p)
{
++len;
p = p->pNext;
}
printf("\n");
return len;
}
/*冒泡排序*/
void sort_list(PNODE pHead)
{
int r;/*交换数据*/
int i,j;/*控制循环,避免指针越界问题*/
int len = len_list(pHead);
PNODE p,q;
for(i=0,p=pHead->pNext;i<len-1;i++,p=p->pNext)
{
for(j=i+1,q=p->pNext;j<len;j++,q=q->pNext)
{
if(p->data > q->data)
{
r = p->data;
p->data = q->data;
q->data = r;
}
}
}
return;
}
/*插入元素*/
/*在pHead所指向的链表的第pos个节点出插入一个新节点,该节点的值为val,并且pos的值是从1开始的*/
bool inser_list(PNODE pHead,int pos,int val)
{
int i = 0;
PNODE p = pHead;
while(NULL != p && i<pos-1)
{
p = p->pNext;
++i;
}
if(i>pos-1 || NULL == p)
{
return false;
}
/*此时完成p指向pos-1个节点*/
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if (NULL == pNew)
{
printf("Error:动态分配内存失败!\n");
exit(-1);
}
else
{
pNew->data = val;
PNODE q = p->pNext;
p->pNext = pNew;
pNew->pNext = q;
}
return true;
}
bool delete_list(PNODE pHead,int pos,int* pVal)
{
int i = 0;
PNODE p = pHead;
while(NULL != p->pNext && i<pos-1)
{
p = p->pNext;
++i;
}
if(i>pos-1 || NULL == p->pNext)
{
return false;
}
PNODE q = p->pNext;
*pVal = q->data;
/*删除p节点后面的节点*/
p->pNext = p->pNext->pNext;
free(q);
q = NULL;
return true;
}
页:
[1]