编写打印出一个单链表的所有元素的程序

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

编写打印出⼀个单链表的所有元素的程序
//Building a linked list in C
//The following program shows how a simple, linear linked list can be constructed in C, using dynamic memory allocation and pointers. #include "stdafx.h"
#include<stdlib.h>
#include<stdio.h>
struct list_el {
int val;
struct list_el * next;
};
typedef struct list_el item;
void main() {
item * curr, * head;
int i;
head = NULL;
for(i=1;i<=10;i++) {
curr = (item *)malloc(sizeof(item));
curr->val = i;
curr->next = head;
head = curr;
}
curr = head;
while(curr)
{
printf("%d\t", curr->val);
curr = curr->next ;
}
getchar();
}
输出 10 9 8 7 6 5 4 3 2 1
单链表的添加,删除,打印操作
代码
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node * next;
}LLIST;
LLIST *list_add(LLIST **p,int i);
void list_remove(LLIST **p);
LLIST **list_search(LLIST **n,int i);
void list_print(LLIST *n);
LLIST *list_add(LLIST **p,int i)
{
if(p==NULL)
return NULL;
LLIST *n=(LLIST *)malloc(sizeof(LLIST));
if(n==NULL)
return NULL;
n->next=*p;
*p=n; //头指针移动到开始处
n->data=i;
return *p;
}
void list_remove(LLIST **p)
{
if(p!=NULL&&*p!=NULL)
{
LLIST *n=*p;
*p=(*p)->next;
free(n);
}
}
LLIST **list_search(LLIST **n,int i)
{
if(n==NULL)
return NULL;
while(*n!=NULL)
{
if((*n)->data==i)
{
return n;
}
n=&(*n)->next;
return NULL;
}
}
void list_print(LLIST *n)
{
if(n==NULL)
{
printf("list is empty\n");
}
while(n!=NULL)
{
printf("print %p %p %d\n",n,n->next,n->data); n=n->next;
}
}
int main(void)
{
LLIST *n=NULL;
list_add(&n,0);
list_add(&n,1);
list_add(&n,2);
list_add(&n,3);
list_add(&n,4);
list_print(n);
printf("------------------end-------------------\n");
list_remove(&n);
list_remove(&n->next);
list_remove(list_search(&n,1));
list_remove(&n->next);
list_remove(&n);
list_print(n);
printf("------------------end-------------------\n");
getchar();
return0;
}
单链表的延伸操作:
代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct llist
{
char *str;
struct llist *next;
};
int main(void)
{
char line[1024];
struct llist *head=NULL;
struct llist *newa=NULL;
int flag=0;
while(fgets(line,1024,stdin)!=NULL)
{
newa=(struct llist *)malloc(sizeof(struct llist));
newa->next=head;
head=newa;
newa->str= strdup(line); //如果是line的话,所有的str都是最后⼀个输⼊的值 flag++;
if(flag>10)
break;
}
while(head!=NULL)
{
printf("%s",head->str);
head=head->next;
}
getchar();
return0;
}
单链表的逆置操作
代码
#include <stdlib.h>
#include <malloc.h>
#define NULL 0
#define OK 1
typedef int ElemType;
typedef int Status;
//-----单链表的存储结构-----//
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
void CreastList_L(LinkList &L,int n){
//创建带头结点的单链表L
LNode *p,*q;
int i;
L=(LNode*)malloc(sizeof (LNode));
L->next=NULL; //先建⽴⼀个带头结点的单链表
p=L;
for (i=1;i<=n;i++){
q=(LNode*)malloc(sizeof(LNode)); //⽣成新结点 printf("Input the %dth data:",i);
scanf("%d",&q->data); //输⼊元素值
q->next=NULL;
p->next=q;
p=q;
}
}
void ListInverse_L(LinkList &L){
//单链表的就地逆置
LNode *p,*q;
p=L->next;
L->next=NULL;
while(p!=NULL){
q=p->next; //没搞懂
p->next=L->next;
L->next=p;
p=q;
}
}
void PrintList(LinkList &L){
//输出单链表
LNode *p=L->next;
while(p!=NULL){
printf("%d",p->data);
p=p->next;
}
}
void main(){
int n;
LinkList La;
printf("Input the list num:");
scanf("%d",&n);
CreastList_L(La,n);
printf("Before Inverse the list is:");
PrintList(La);
ListInverse_L(La);
printf("\nAfter Inverse the list is:");
PrintList(La);
printf("\n");
getchar();
}。

相关文档
最新文档