在数据结构中怎样进行单链表的输入输出?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在数据结构中怎样进行单链表的输入输出?相关的知识,希望对你有一定的参考价值。
不要太复杂的,我只要建立一个单链表,比如输入1,2,3,4再输出即可
内容包括链表的创建,增加、删除节点,链表的逆序、排序和销毁等。
#include<stdio.h>#include<stdlib.h>
typedef struct node
int data;
node* pNext;
Node;
//链表的操作,以有头节点为例,无头节点类似
Node* head = NULL;
//创建链表,头结点data=0,pNext=NULL;
bool createNodeList()
head = (Node*) malloc(sizeof(Node));
if(NULL == head)
return false;
else
head->data = 0;
head->pNext = NULL;
return true;
//增加节点
bool addNode(Node* node)
if(NULL == head)
return false;
Node* p = head->pNext;
Node* q = head;
while(NULL != p)
q = p;
p = p->pNext;
q->pNext = node;
node->pNext = NULL;
return true;
//删除节点
bool deleteNode(int index)
if(NULL == head)
return false;
Node* p = head->pNext;
int length = 0;
while(NULL != p)
length ++;
p = p->pNext;
if(length < index)
return false;
else
Node* q = head;
p = head;
for(int i=0;i<index;i++)
q = p;
p = p->pNext;
Node* t = p->pNext;
q->pNext = t;
free(p);
return true;
//逆序
void reverseNodeList()
if(NULL == head)
return;
//如果链表长度为1
if(head->pNext == NULL)
return;
Node* p = head->pNext;
Node* q = p->pNext;
Node* t = NULL;
while(NULL != q)
t = q->pNext;
q->pNext = p;
p = q;
q = t;
head->pNext->pNext = NULL;
head->pNext = p;
//排序(降序)
void sort()
//冒泡排序
Node* pHead = head;
if(head == NULL)
return;
if(pHead->pNext == NULL)
return;
Node* pi = pHead->pNext;
Node* pj = pi->pNext;
for(;pi != NULL;pi=pi->pNext)
for(pj = pi->pNext;pj != NULL;pj=pj->pNext)
if(pj->data>pi->data)
int tmp = pj->data;
pj->data = pi->data;
pi->data = tmp;
//销毁
void destroyNodeList()
if(NULL == head)
return;
if(NULL == head->pNext)
free(head);
head = NULL;
return;
Node* p = head->pNext;
while(NULL != p)
Node* tmp = p;
p = p->pNext;
free(tmp);
free(head);
head = NULL;
void main()
createNodeList();
Node* node1 = (Node*)malloc(sizeof(Node));
node1->data = 1;
node1->pNext = NULL;
Node* node2 = (Node*)malloc(sizeof(Node));
node2->data = 2;
node2->pNext = NULL;
addNode(node1);
addNode(node2);
reverseNodeList();
Node* node3 = (Node*)malloc(sizeof(Node));
node3->data = 3;
node3->pNext = NULL;
addNode(node3);
sort();
deleteNode(2);
destroyNodeList();
参考技术A
我有一个完整的代码给你。如附件
更多技术文章可以关注我的微博,名字:成都睿尔科技
具体代码如下:
//------------------------------( 链表 )-------------------------------------#include <stdio.h>
#include <stdlib.h>
#define SUCCESS 1
#define FLASE 0
typedef struct node
int iData;
struct node *pNext;
Node,*LinkList;
//初始化链表
//返回类型为指向结构体的指针
LinkList Init_LinkList(LinkList pHead)
pHead=NULL;
return pHead;
//返回链表长度
int Get_Length(LinkList pHead)
int iLength = 0;
Node* pFlag = NULL;
pFlag = pHead;
while(pFlag!=NULL)
iLength++;
pFlag=pFlag->pNext;
return iLength;
//插入结点
//成功插入返回链表头指针,不成功提示错误,返回头指针
//pHead:结点头指针
//iLocate:位置
//iData:数据
LinkList Inset_LinkList(LinkList pHead , int iLocate , int iData)
int iCount = 0;
Node* pNode = NULL;
Node* pFlag = NULL;
pNode = (Node* )malloc(sizeof(pNode));
if(iLocate==1)
pNode->iData = iData;
pNode->pNext = pHead;
pHead = pNode;
return pHead;
else if(iLocate<=(Get_Length(pHead)+1))
pFlag = pHead;
for ( iCount=1; iCount<iLocate-1; iCount++)
pFlag = pFlag->pNext;
pNode->iData = iData;
pNode->pNext = pFlag->pNext;
pFlag->pNext = pNode;
printf("Insert Success\\n");
return pHead;
else
free(pNode);
printf("Locate is Error.Insert Failre\\n");
return pHead;
//根据数据删除结点
//iData为要删除的结点
LinkList Delete_Data_LinkList(LinkList pHead,int iData)
LinkList pFlag1,pFlag2;
pFlag1 = pHead;
pFlag2 = pHead;
if(pHead==NULL)
printf("del a empty LinkList\\n");
return pHead;
if(pHead->iData==iData)
pHead=pFlag1->pNext;
// free(pFlag1);
printf("Del Succuess\\n");
return pHead;
pFlag1=pFlag1->pNext;
while(pFlag1->pNext!=NULL)
if(pFlag1->iData==iData)
pFlag2->pNext=pFlag1->pNext;
// free(pFlag1);
printf("Del Success");
return pHead;
else
pFlag1=pFlag1->pNext;
pFlag2=pFlag2->pNext;
printf("no such data\\n");
return pHead;
//根据位置删除结点
//iLocate为需要删除的位置
LinkList Delete_Locate_LinkList(LinkList pHead,int iLocate)
LinkList pFlag=pHead;
int iCount;
if(pFlag==NULL)
printf("the LinkList is empty\\n");
return pHead;
else if(iLocate==1)
pHead=pHead->pNext;
printf("test1\\n");
printf("del Success");
// free(pFlag);
return pHead;
else if (iLocate <= Get_Length(pHead))
for ( iCount=1; iCount<=iLocate-2; iCount++)
pFlag = pFlag->pNext;
pFlag->pNext = pFlag->pNext->pNext;
// free(pFlag->pNext);
printf("Del Success");
return pHead;
else
printf("Locate error\\n");
return pHead;
//输入数据函数
int Input_iData(void)
int iData;
printf("\\nplease input data : ");
scanf("%d",&iData);
return iData;
//输入位置函数
int Input_iLocate(void)
int iLocate;
printf("\\nplease input Locate : ");
scanf("%d",&iLocate);
if (iLocate>0)
return iLocate;
else
printf("Locate Error.\\n ");
return FLASE;
//打印所有结点
int Printf_Node(LinkList pHead)
LinkList pNode;
pNode=pHead;
while(pNode!=NULL)
printf("%d\\t",pNode->iData);
pNode = pNode->pNext;
return SUCCESS; //成功返回SUCCESS
void Run_LinkList(void)
int iChoose;
int bRun;
bRun = 1;
//LinkList temp;
LinkList pHead=NULL;
// pHead = Init_LinkList(pHead);
while (bRun)
printf("1.Input data into LinkList.\\n");
printf("2.Delete data form LinkList by locate.\\n");
printf("3.Delete data form LinkList by data.\\n");
printf("4.Printf all data form LinkList.\\n");
printf("5.Quit Program.\\n\\n");
printf("Please choose your mind : ");
scanf("%d",&iChoose);
printf("\\n");
switch (iChoose)
case 1 : pHead=Inset_LinkList(pHead,Input_iLocate(),Input_iData());
break;
case 2 : pHead=Delete_Locate_LinkList(pHead,Input_iLocate());
break;
case 3 : pHead=Delete_Data_LinkList(pHead,Input_iData());
break;
case 4 : Printf_Node(pHead);
break;
case 5 : bRun = 0;
break;
default :printf("Your choose maybe is error,please try again.\\n");
break;
printf("\\n\\n");
getchar();
int main(void)
Run_LinkList();
return 0;
参考技术B //创建一个单链表
#include <stdio.h>
#include <stdlib.h>
typedef struct lnode * pointer;
struct lnode
int data;
struct lnode * next;
;
typedef pointer lklist;
void create(lklist &l)
int x;
lklist r;
l=(lklist)malloc(sizeof(struct lnode));
r=l;
printf("Input the elemment: ");
scanf("%d", &x);//输入元素
while(x != 0) //以0作为结束标志
lklist s;
s=(pointer)malloc(sizeof(struct lnode));
s->data=x;
r->next=s;
r=s;
scanf("%d", &x);
r->next=NULL;
void output(lklist l) //输出单链表的元素
lklist p;
p=l->next;
while(p!=NULL)
printf("%d ", p->data);
p=p->next;
追问
可以加一下你的QQ吗?我今年大二了,计算机专业可啥也不懂,想好好学一下可自己看得迷糊,实在是想找个高手请教一下1942567571,万分感谢!
这个程序编译是出了一点小差错
我也才是大二,你是哪个学校的啊?我刚加你了
本回答被提问者采纳 参考技术C #include<iostream>using namespace std;
#include<stdlib.h>
typedef struct Lnode//定义单链表结点
int data;
struct Lnode *next;
Lnode;
void createlistT(Lnode *&C,int a[],int n)//用尾插法建立单链表
int i;
Lnode *s,*r;
C=(Lnode*)malloc(sizeof(Lnode));
C->next=NULL;
r=C;
for(i=0;i<n;i++)
s=(Lnode*)malloc(sizeof(Lnode));
s->data=a[i];
r->next=s;
r=s;
r->next=NULL;
void createlistH(Lnode *&C,int a[],int n)//用头插法建立单链表
int i;
Lnode *s;
C=(Lnode*)malloc(sizeof(Lnode));
C->next=NULL;
for(i=0;i<n;i++)
s=(Lnode*)malloc(sizeof(Lnode));
s->data=a[i];
s->next=C->next;
C->next=s;
void output(Lnode *L)//输出单链表中内容
Lnode *p;
p=L->next;
while(p!=NULL)
cout<< p->data<<endl ;
p=p->next;
int main()
int a[10]=1,2,3,4,5,6,7,8,9,10;
Lnode *L;//定义一个头指针
createlistH(L,a,10);
output(L);
createlistT(L,a,10);
output(L);
return 0;
用了头插 尾插进行简单输入输出
c语言关于链表的一道题
6.链表的拆分和排序
题目描述
输入N个整数顺序建立一个单链表,将该单链表拆分成两个子链表,第一个子链表存放了所有的偶数,第二个子链表存放了所有的奇数。两个子链表中数据按照从小到大排序。
输入
第一行输入整数N;;
第二行依次输入N个整数。
输出
第一行分别输出偶数链表与奇数链表的元素个数;
第二行依次输出偶数子链表的所有数据;
第三行依次输出奇数子链表的所有数据。
示例输入
10
1 3 22 8 15 999 9 44 6 1001
示例输出
4 6
6 8 22 44
1 3 9 15 999 1001
#include<stdlib.h>
//整数单链表
struct numList
int num;
numList* next;
;
//函数原型声明
numList* create(int n);
int add(numList* head, int n);
void sort(numList* head);
void output(numList* head);
void main()
char* p;
p=NULL;
int count;
int evenCount = 0; //奇数个数
int oddCount = 0; //偶数个数
numList* list;
numList* evenList;
numList* oddList;
numList* pNode;
printf("输入整数个数及各整数值:\n");
scanf("%d",&count);
list = create(count);
evenList = (numList*)malloc(sizeof(numList));
evenList->next = NULL;
oddList = (numList*)malloc(sizeof(numList));
oddList->next = NULL;
pNode = list->next;
while(pNode != NULL)
if((pNode->num & 1) == 0)
//如果是偶数
evenCount++;
add(evenList, pNode->num);
else
//如果是奇数
oddCount++;
add(oddList, pNode->num);
pNode = pNode->next;
//对偶数和奇数链表排序
sort(evenList);
sort(oddList);
//输出
printf("%d %d\n",evenCount, oddCount);
output(evenList);
output(oddList);
/*************************************************
【函数名称】 create
【功能】 创建一个具有n个节点的单链表
【参数】 节点数
【返回值】 指向头节点的指针
*************************************************/
numList* create(int n)
numList* head;
numList* pCur;
numList* pNext;
head = (numList*)malloc(sizeof(numList));
if(head == NULL)
return NULL;
head->next = NULL;
pCur = head;
while(n>0)
pNext = (numList*)malloc(sizeof(numList));
if(pNext == NULL)
return NULL;
if(scanf("%d",&pNext->num)==0)
break;
pNext ->next = NULL;
pCur->next = pNext;
pCur = pNext;
n--;
return head;
/*************************************************
【函数名称】 add
【功能】 向链表末尾添加一个节点
【参数】 指向链表头节点的指针及添加元素的值
【返回值】 插入成功返回1,失败则返回0
*************************************************/
int add(numList* head, int n)
if(head == NULL)
return 0;
numList* p;
numList* pNewNode;
p = head;
while(p->next != NULL)
//得到尾既节点指针
p = p->next;
pNewNode = (numList*)malloc(sizeof(numList));
if(pNewNode == NULL)
return 0;
pNewNode->num = n;
pNewNode->next = NULL;
p->next = pNewNode;
return 1;
/*************************************************
【函数名称】 sort
【功能】 使用简单选择排序对节点元素升序排列
【参数】 指向链表头节点的指针及添加元素的值
【返回值】 无
*************************************************/
void sort(numList* head)
if(head == NULL)
return;
int temp;
numList* pStart;
numList* pCur;
for(pStart = head->next; pStart != NULL; pStart = pStart->next)
for(pCur = pStart->next; pCur != NULL; pCur = pCur->next)
if(pStart->num > pCur->num)
temp = pStart->num;
pStart->num = pCur->num;
pCur->num = temp;
/*************************************************
【函数名称】 output
【功能】 输出链表节点元素
【参数】 指向链表头节点的指针及添加元素的值
【返回值】 无
*************************************************/
void output(numList* head)
if(head == NULL)
return;
numList *p;
p = head->next;
while(p != NULL)
printf("%d ",p->num);
p = p->next;
printf("\n");
参考技术A //---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
typedef struct node
int data;
struct node *next;
node;
typedef struct
node *head;
node *end;
int cnt;
list;
void prt_list(list *a)
node *t=a->head;
while (t)
printf("%d ",t->data);
t=t->next;
putchar('\n');
void list_insert(list *a,int n)
node *t=malloc(sizeof(node));
t->data=n;
t->next=NULL;
if (a->end==NULL) a->head=a->end=t;
else
a->end->next=t;
a->end=t;
++a->cnt ;
void pickup_insert(list *a,int d)
node *s,*t=malloc(sizeof(node));
t->data=d;
t->next=NULL;
if (a->head==NULL)
a->head=a->end=t;
else if (d<a->head->data)
t->next=a->head;
a->head=t;
else if (d>a->end->data)
a->end->next=t;
a->end=t;
else
s=a->head;
while (s&&d>s->next->data)
s=s->next;
t->next=s->next;
s->next=t;
++a->cnt;
void list_pickup(list *a,list *even,list *odd)
node *t;
for (t=a->head; t!=NULL; t=t->next)
if (t->data%2) pickup_insert(odd,t->data);
else pickup_insert(even,t->data);
void delete_list(list *a)
node *t;
for (t=a->head; a->head!=NULL; t=a->head)
a->head=t->next;
free(t);
int main(int argc, char* argv[])
list even=0,odd=0,lt=0;
int i,n,a;
scanf("%d",&n);
for (i = 0; i < n; i++)
scanf("%d",&a);
list_insert(<,a);
list_pickup(<,&even,&odd);
printf("%d %d\n",even.cnt,odd.cnt);
prt_list(&even);
prt_list(&odd);
delete_list(&even);
delete_list(&odd);
return 0;
//---------------------------------------------------------------------------
以上是关于在数据结构中怎样进行单链表的输入输出?的主要内容,如果未能解决你的问题,请参考以下文章
实用数据结构:怎样把两个单链表A B(本身都是有序表)合并为C并且C为有序表