链表:
1.带头结点的链表
2.不带头结点的链表
3.链表元素的插入(含头结点)
4.链表元素的查找(含头结点)
5.链表元素的删除(含头结点)
废话不多说,直接上代码,均经过编译有效:
//*****************************************************************************************
//链表
//
//含有头结点
//*****************************************************************************************
#include <stdio.h>
#include <stdlib.h>
typedef struct List
{
int data;
struct List *next;
}list;
list *InitListHead()
{
list *l = (list *)malloc(sizeof(list));
list *head = l;
l->next = NULL;
for(int i=1;i<5;i++)
{
list *new = (list *)malloc(sizeof(list));
scanf("%d",&new->data);
new->next = NULL;
l->next = new;
l = l->next;
}
return head;
}
void DisplayHead(list *l)
{
list *temp = l;
printf("**************************** \n");
while(temp->next!=NULL)
{
temp = temp->next;
printf("%d \n",temp->data);
}
}
int main(void)
{
list *l = InitListHead();
DisplayHead(l);
return 0;
}
//*****************************************************************************************
//链表
//
//不含有头结点
//*****************************************************************************************
#include <stdio.h>
#include <stdlib.h>
typedef struct List
{
int data;
struct List *next;
}list;
list *InitList()
{
list *l = (list *)malloc(sizeof(list));
list *temp = l;
scanf("%d",&list->data);//没有头结点,都是含数据节点
l->next = NULL;
for(int i=1;i<5;i++)
{
list *new = (list *)malloc(sizeof(list));
scanf("%d",&new->data);
new->next = NULL;
l->next = new;
l = l->next;
}
return temp;
}
void Display(list *l)
{
list *temp = l;
while(temp!=NULL)
{
printf("%d",temp->data);
temp = temp->next;
}
}
int main(void)
{
List *l = Initlist();
Display(l);
return 0;
}
//*****************************************************************************************
//链表
//
//含有头结点,插入
//*****************************************************************************************
#include <stdio.h>
#include <stdlib.h>
typedef struct List
{
int data;
struct List *next;
}list;
list *InitListHead()
{
list *l = (list *)malloc(sizeof(list));
list *head = l;
l->next = NULL;
for(int i=1;i<5;i++)
{
list *new = (list *)malloc(sizeof(list));
scanf("%d",&new->data);
new->next = NULL;
l->next = new;
l = l->next;
}
return head;
}
void DisplayHead(list *l)
{
list *temp = l;
while(temp->next!=NULL)
{
temp = temp->next;
printf("%d \n",temp->data);
}
}
int InsertData(list *l,int num,int data)
{
list *temp = l;
list *new = (list *)malloc(sizeof(list));
new->data = data;
new->next = NULL;
for(int i=1;i<num;i++)
{
if(temp == NULL)
{
printf("there doesn't have enough list number \n");
return -1;
}
temp = temp->next;
}
new->next = temp->next;
temp->next = new;
return 1;
}
int main(void)
{
list *l = InitListHead();
DisplayHead(l);
return 0;
}
//*****************************************************************************************
//链表
//
//含有头结点,查找
//*****************************************************************************************
#include <stdio.h>
#include <stdlib.h>
typedef struct List
{
int data;
struct List *next;
}list;
list *InitListHead()
{
list *l = (list *)malloc(sizeof(list));
list *head = l;
l->next = NULL;
for(int i=1;i<5;i++)
{
list *new = (list *)malloc(sizeof(list));
scanf("%d",&new->data);
new->next = NULL;
l->next = new;
l = l->next;
}
return head;
}
void DisplayHead(list *l)
{
list *temp = l;
printf("********************************** \n");
while(temp->next!=NULL)
{
temp = temp->next;
printf("%d \n",temp->data);
}
}
int FindData(list *l,int data)//通过数据查找
{
list *temp = l;
int num = 0;
while(temp != NULL)
{
temp = temp->next;
num++;
if(temp->data == data)
{
return num;
}
}
printf("doesn't find the data in the list \n");
return -1;
}
int FindNum(list *l,int num)
{
list *temp = l;
for(int i=0;i<num;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("there are no enough num in the list \n");
return -1;
}
}
return temp->data;
}
int main(void)
{
list *l = InitListHead();
int num;
DisplayHead(l);
FindData(l,3);
printf("************************\n we search the num get the data %d \n",FindNum(l,2));
return 0;
}
//*****************************************************************************************
//链表
//
//含有头结点,删除
//*****************************************************************************************
#include <stdio.h>
#include <stdlib.h>
typedef struct List
{
int data;
struct List *next;
}list;
list *InitListHead()
{
list *l = (list *)malloc(sizeof(list));
list *head = l;
l->next = NULL;
for(int i=1;i<6;i++)
{
list *new = (list *)malloc(sizeof(list));
scanf("%d",&new->data);
new->next = NULL;
l->next = new;
l = l->next;
}
return head;
}
void DisplayHead(list *l)
{
list *temp = l;
printf("***************************** \n");
while(temp->next!=NULL)
{
temp = temp->next;
printf("%d \n",temp->data);
}
}
int DelData(list *l,int data)
{
list *temp = l;
while(temp != NULL)
{
temp = temp->next;
if(temp->data == data)
{
list *p = temp->next;
temp->data = temp->next->data;
temp->next = temp->next->next;
free(p);
return 1;
}
}
return -1;
}
int DelNum(list *l,int num)
{
list *temp =l;
if(num<1)
{
printf("input num is an illegal num \n");
return -1;
}
for(int i=0;i<num-1;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("there are no enough num \n");
return -1;
}
}
list *p = temp->next;
temp->next = temp->next->next;
free(p);
return 1;
}
int main(void)
{
list *l = InitListHead();
DisplayHead(l);
DelData(l,2);
DisplayHead(l);
DelNum(l,2);
DisplayHead(l);
return 0;
}
PS:本文不具有权威性,仅为个人学习笔记或有感而发,如有错误欢迎指正。