單鏈表操作(資料結構實驗一),單鏈表資料結構實驗
實驗內容
1 初始化一個帶表頭結點的單鏈表。
2 從表頭不斷插入結點建立一個帶表頭結點的單鏈表。設表中元素的類型為整型,元素值從鍵盤輸入。
3 從表尾不斷插入結點建立一個帶表頭結點的單鏈表。設表中元素的類型為整型,元素值從鍵盤輸入。
4 列印一個帶表頭結點的單鏈表。
5 清空一個帶表頭結點的單鏈表。
代碼:(只是把各個函數寫好,並給出了調用方式,其他根據實驗要求改就行了)
#include<stdio.h>#include<stdlib.h>#include<malloc.h>#define M 100typedef int Etype; //定義單鏈表結點值的類型為整型typedef struct Node{ Etype data; //單鏈表中的資料域 struct Node *link; //單鏈表的指標域}Node;typedef Node *List; //定義單鏈表List BuildList1();List BuildList2();void PrintList(List first);void clear(List *first);//頭插法建立鏈表List BuildList1(){ Node *L; Etype x,n; L = (Node *)malloc(sizeof(Node)); //申請頭結點空間 L->link = NULL; //初始化一個空鏈表 printf("請輸入元素個數:\n"); scanf("%d",&n); printf("請輸入元素:\n"); //x為鏈表資料域中的資料 for(int i=0;i<n;i++) { scanf("%d",&x); Node *p; p = (Node *)malloc(sizeof(Node)); //申請新的結點 p->data = x; //結點資料域賦值 p->link = L->link; //將結點插入到表頭L-->|2|-->|1|-->NULL L->link = p; } return L;}//尾插法建立鏈表List BuildList2(){ Node *L,*r; Etype n,x; L = (Node *)malloc(sizeof(Node)); //申請頭結點空間 L->link = NULL; //初始化一個空鏈表 printf("請輸入元素個數:\n"); scanf("%d",&n); r = L; //r始終指向終端結點,開始時指向頭結點 printf("請輸入元素:\n"); for(int i=0;i<n;i++) { scanf("%d",&x); Node *p; p = (Node *)malloc(sizeof(Node)); //申請新的結點 p->data = x; //結點資料域賦值 r->link = p; //將結點插入到表頭L-->|1|-->|2|-->NULL r = p; } r->link = NULL; return L;}//輸出鏈表void PrintList(List first){ Node *Li; if(first==NULL) { printf("鏈表為空白!\n"); return; } Li=first->link; while(Li!=NULL) { printf("%d ",Li->data); Li=Li->link; } printf("\n");}//清空鏈表void clear(List *first){ Node *p=*first; while(*first) { p=(*first)->link; free(*first); *first=p; }}int main(){ Node *L; L=BuildList1(); //調用BuiltdList1(前插法)建立單鏈表演算法 PrintList(L); //列印單鏈表 clear(&L);//清空單鏈表 L=BuildList2(); //調用BuiltdList2(後插法)建立單鏈表演算法 PrintList(L); //列印單鏈表 clear(&L);//清空單鏈表}
資料結構上機實驗(單鏈表)
東西真麼多才10分啊。。。。
這個程式主要是單鏈表的操作!
#include<iostream>
#include<iomanip>
using namespace std;
struct node{
double data;
struct node *next;
};
struct node *create();
struct node *rescreate();
void printlist(struct node *head);
int len(struct node *head);
void insertnode(struct node *head,int i,double x) ;
void delnode(struct node *head,double x);
void delnode2(struct node *head,int i);
void sort(struct node *head);
int main()
{
struct node *p;
p=rescreate();
printlist(p);
//cout<<endl<<len(p)<<endl;
insertnode(p,3,-100);
cout<<endl;
printlist(p);
delnode2(p,4);
cout<<endl;
printlist(p);
sort(p);
cout<<endl<<"After sort\n";
printlist(p);
system("pause");
return 0;
}
struct node *create()
{
struct node *head,*oldp,*p,*s;
double newnum;
head=new struct node;
oldp=head;
cin>>newnum;
while(newnum!=-32768){
p=new struct node;
p->data=newnum;
oldp->next=p;
oldp=p;
cin>>newnum;
}
s=head;
head=head->next;
delete s;
p->next=head;
return head;
}
struct node *rescreate()
{
struct node *tail,*oldp,*p;
double newnum;
tail=NULL;
oldp=tail;
cin>>newnum;
while(newnum!=-32768){
p=new struct node;
p->data=newnum;
p->next=oldp;
oldp=p;
cin>>newnum;
}
return p;
}
void printlist(struct node *head)
{
while(head){
cout<<setw(5)<<head->data;
head=head->next;
}
}
int len(struct......餘下全文>>
資料結構單鏈表實驗(c語言版)
正好我大二的實驗還在。。。但是還是想說一句,伸手黨不適合做IT,建議樓主還是要自己多加練習,不會可以問,網上有很多樂意幫你解決問題的人。
#include "stdio.h" #include "string.h"#include "ctype.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h"#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0#define MAXSIZE 20 /* 儲存空間初始分配量 */typedef int Status;/* Status是函數的類型,其值是函數結果狀態碼,如OK等 */typedef int ElemType;/* ElemType類型根據實際情況而定,這裡假設為int */Status visit(ElemType c){ printf("%d ",c); return OK;}typedef struct Node{ ElemType data; struct Node *next;}Node;typedef struct Node *LinkList; /* 定義LinkList *//* 初始化順序線性表 */Status InitList(LinkList *L) { *L=(LinkList)malloc(sizeof(Node)); /* 產生頭結點,並使L指向此頭結點 */ if(!(*L)) /* 儲存分配失敗 */ return ERROR; (*L)->next=NULL; /* 指標域為空白 */ return OK;}/* 初始條件:順序線性表L已存在。操作結果:若L為空白表,則返回TRUE,否則返回FALSE */Status ListEmpty(LinkList L){ if(L->next) return FALSE; else return TRUE;}/* 初始條件:順序線性表L已存在。操作結果:將L重設為空白表 */Status ClearList(LinkList *L){ LinkList p,q;p=(*L)->next; /* p指向第一個結點 */while(p) /* 沒到表尾 */{q=p->next;free(p);p=q;}(*L)->next=NULL; /* 頭結點指標域為空白 */return OK;}/* 初始條件:順序線性表L已存在。操作結果:返回L中資料元素個數 */int ListLength(LinkList L){ int i=0; LinkList p=L->next; /* p指向第一個結點 *&#......餘下全文>>